SQL高可用优化-优化SQL中distinct和Where条件对索引字段进行非空检查语句

最近做一个需求,关于SQL高可用优化,需要优化项目中的SQL,提升查询效率。

一、优化SQL包含distinct场景

1.1 原因
数据量:数据量越大,DISTINCT操作需要的时间和资源越多。
索引:如果查询中涉及到的列没有索引,数据库引擎可能需要全表扫描来执行DISTINCT操作,导致性能下降。
数据分布:如果数据分布不均匀,即某些值出现频率较高,DISTINCT操作会更耗时。
1.2 优化措施
(1)可以利用Set集合进行存储,Set集合会自动对数据进行去重
(2)可以用lambda表达式中distinct()方法进行去重

二、优化SQL中Where条件中索引字段是否为NULL

1.1 原因
对索引字段进行非空检查时,数据库可能会选择全表扫描而不是使用索引,因为索引中不包含NULL值,这会降低查询性能。
1.2 优化措施
在service层利用lambda表达式中进行去重

三、代码验证

1. NodeMapper

@DS("mysql1")
@Repository("NodeMapper")
public interface NodeMapper extends BaseMapper<NodeVo> {
    @Select("select distinct esn from node_vo where name like 'GTS5900%'")
    List<String> getNodeListByName(String name);

    @Select("select esn from node_vo where name like 'GTS5900%'")
    Set<String> getNodeSetByName(String name);

    @Select("select esn from node_vo where name like 'GTS5900%'")
    List<String> getNodeListNoDistinctByName(String name);

    @Select("select esn from node_vo where esn like 'msk00%' and name != ''")
    List<NodeVo> getNodeListNoEmptyNameByEsn(String esn);

    @Select("select esn from node_vo where esn like 'msk00%'")
    List<NodeVo> getNodeListByEsn(String esn);
}

2. NodeService

@Service
public class NodeService {
    @Autowired
    private NodeMapper nodeMapper;
    public List<String> getNodeListByName(String name){
        // 通过sql语句中distinct去重
        return nodeMapper.getNodeListByName(name);
    }

    public Set<String> getNodeSetByName(String name){
        // 通过set集合去重
        return nodeMapper.getNodeSetByName(name);
    }

    public List<String> getNodeListNoDistinctByName(String name){
        // 通过xxx.stream().distinct()去重
        List<String> nodeListNoDistinctByName = nodeMapper.getNodeListNoDistinctByName(name);
        return nodeListNoDistinctByName.stream().distinct().collect(Collectors.toList());
    }

    public List<NodeVo> getNodeListNoEmptyNameByEsn(String esn){
        return nodeMapper.getNodeListNoEmptyNameByEsn(esn);
    }

    public List<NodeVo> getNodeListByEsn(String esn){
        return nodeMapper.getNodeListByEsn(esn);
    }
}

3. NodeController

@RestController
public class NodeController {

    @Autowired
    private NodeService nodeService;

    @RequestMapping("getNodeListByName")
    public List<String> getNodeListByName(String name) {
        // 通过sql语句中distinct去重
        return nodeService.getNodeListByName(name);
    }

    @RequestMapping("getNodeSetByName")
    public Set<String> getNodeSetByName(String name) {
        // 通过set集合去重
        return nodeService.getNodeSetByName(name);
    }

    @RequestMapping("getNodeListNoDistinctByName")
    public List<String> getNodeListNoDistinctByName(String name) {
        // 通过xxx.stream().distinct()去重
        return nodeService.getNodeListNoDistinctByName(name);
    }

    @RequestMapping("getNodeListNoEmptyNameByEsn")
    public List<String> getNodeListNoEmptyNameByEsn(String esn) {
        // 通过sql过滤name不为null的值
        List<NodeVo> nodeListNoEmptyNameByEsn = nodeService.getNodeListNoEmptyNameByEsn(esn);
        return nodeListNoEmptyNameByEsn.stream().map(NodeVo::getEsn).collect(Collectors.toList());
    }

    @RequestMapping("getNodeListByEsn")
    public List<String> getNodeListByEsn(String esn) {
        // 通过xxx.stream().filter()过滤name不为null的值
        List<NodeVo> nodeListByEsn = nodeService.getNodeListByEsn(esn);
        return nodeListByEsn.stream().map(NodeVo::getEsn).filter(Objects::nonNull).collect(Collectors.toList());
    }
}

4.数据库数据

在这里插入图片描述

5.项目结构及源码

源码下载地址demo-springboot-mybatisplus,欢迎Star !
在这里插入图片描述
由于项目集成了SaToken框架,需要先登录,再访问测试NodeController接口
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值