目录

菜鸡还没有找到工作DAY49

菜鸡还没有找到工作(DAY49)

https://i-blog.csdnimg.cn/direct/0f7d0ecea415453f82166f5b0c6f2cf6.png


with t as(
    select t1.name,t1.category,t1.product_id,sum(t2.quantity) as total_sales
    from products t1
    left join orders t2
    on t1.product_id=t2.product_id
    
    group by t1.name,t1.category,t1.product_id
    
),
tt as( select name,total_sales,category,product_id,
rank()over(partition by category order by total_sales desc,product_id) as category_rank
from t
where total_sales is not null
order by category
)
select name as product_name,total_sales,category_rank
from tt

https://i-blog.csdnimg.cn/direct/59262e7941d94de89c498de2414e9703.png


select t1.post,round(avg(timestampdiff(minute,t2.first_clockin,t2.last_clockin)/60),3) as work_hours
from staff_tb as t1
left join attendent_tb t2
on t1.staff_id=t2.staff_id
where t2.first_clockin is not null
group by t1.post
order by work_hours desc

https://i-blog.csdnimg.cn/direct/4997131fa7e441b099a00fb8701ca5fd.png


public class Solution {
    public int solve (String nums) {
        return back(nums.toCharArray(), 0);
    }
    // 递归函数
    public int back(char[] nums, int start){
        //当start走到终点时,证明已经解码完毕,直接返回1
        if(start == nums.length){
            return 1;
        }    
        //当字符为0的时候,0没对应的解码,所以直接返回0 (此路解码废掉)
        if(nums[start] == '0')
            return 0;
        //每次解码一个字符
        int res1 = back(nums,start+1);
        int res2 = 0;

        //如果当前字符等于1 或者 当前字符加上下一个字符合起来小于等于26 则可以一次解码两个字符
        if((start < nums.length-1) && (nums[start] == '1' || (nums[start] == '2' &&nums[start+1] <= '6'))){
            res2 = back(nums,start+2);
        }
        //返回结果
        return res1 + res2;
    }
}

import java.util.Arrays;

public class Solution {
    public int minMoney(int[] arr, int aim) {
        if (aim == 0) return 0;
        if (arr == null || arr.length == 0) return -1;

        int[] dp = new int[aim + 1];
        Arrays.fill(dp, aim + 1);  // 初始化为一个不可能的大值
        dp[0] = 0;  // 金额为0时需要0张货币

        for (int coin : arr) {
            if (coin <= aim) {
                dp[coin] = 1;  // 金额为coin时只需1张
            }
        }

        for (int i = 1; i <= aim; i++) {
            for (int coin : arr) {
                if (i - coin >= 0 && dp[i - coin] != aim + 1) {
                    dp[i] = Math.min(dp[i], dp[i - coin] + 1);
                }
            }
        }

        return dp[aim] == aim + 1 ? -1 : dp[aim];
    }
}

今天学习一下MYSQL

1.数据类型

https://i-blog.csdnimg.cn/direct/e481e8a9fdf94a3cb0797f91cb5549b3.png

设计原则:

优先选择最小适用类型(状态用TINYINT)

金额必须使用DECIMAL

自增主键用bigint

https://i-blog.csdnimg.cn/direct/bce972c4a1944c858830c980fe7736c1.png

设计原则:

varchar长度按需设置,避免过度预留

超过五千字符用text

敏感信息加密

https://i-blog.csdnimg.cn/direct/66186042d6154a76b6439603abb1df45.png

设计原则:

优先使用datetime

避免使用字符串

2.索引类型与优化策略

https://i-blog.csdnimg.cn/direct/e295ac1cf437426daee6a7b51a824100.png

索引数据结构

B+Tree索引(默认)

所有叶子节点在同一层

非叶子结点只存索引键

叶子节点包含所有数据(聚簇索引)或主键(二级索引)

索引设计原则

1.选择原则

where 条件列必建索引

join关键字段建索引

order by/group by字段建索引

分区度高字段优先

2.组合索引优化

https://i-blog.csdnimg.cn/direct/3b7beb32e8d64e7590d9ec81bde47f16.png

最左前缀原则​​是指:MySQL使用组合索引时,​​必须从最左边的字段开始使用​​,不能跳过前面的字段直接使用后面的字段。

3.避坑指南

避免过度索引

不使用函数索引列

整型比较效率高于字符串

is null条件可能不走索引

3.字符集与排序原则

https://i-blog.csdnimg.cn/direct/7fe10a20d0214e6297662f85c2e80d98.png

https://i-blog.csdnimg.cn/direct/5104b57b20d34fa994ade43ce02a025c.png

utf8mb4不区分大小写

utf8mb4_bin区分大小写

4.表设计核心准则

1.三范式与反范式

三范式核心思想:一个表只描述一件事

1NF:字段原子性,单值不可分

字段值必须是不可再分的原子值

设计原则:禁止使用逗号分隔值,json字段谨慎使用,地址拆成省/市/区独立字段

2NF:消除部分依赖

核心要求:非主键字段必须完全依赖整个主键,使用于存在复合主键的表

3NF:消除传递依赖

核心要求:非主键字段之间不能存在依赖关系

反范式设计场景:

适当冗余

预聚合统计

2.生产环境禁止操作

1.在线大表

2.select*

3.全表扫描

4.事务中处理HTTP请求

5.无索引的UPDATE/DELETE