典型的营销型企业网站,wordpress被改密码忘记,缩短链接生成器,贵州网站制作设计公司哪家好最近做一个需求#xff0c;关于SQL高可用优化#xff0c;需要优化项目中的SQL#xff0c;提升查询效率。 SQL高可用优化 一、优化SQL包含distinct场景二、优化SQL中Where条件中索引字段是否为NULL三、代码验证1. NodeMapper2. NodeService3. NodeController4.数据库数据5.项…最近做一个需求关于SQL高可用优化需要优化项目中的SQL提升查询效率。 SQL高可用优化 一、优化SQL包含distinct场景二、优化SQL中Where条件中索引字段是否为NULL三、代码验证1. NodeMapper2. NodeService3. NodeController4.数据库数据5.项目结构及源码 一、优化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 BaseMapperNodeVo {Select(select distinct esn from node_vo where name like GTS5900%)ListString getNodeListByName(String name);Select(select esn from node_vo where name like GTS5900%)SetString getNodeSetByName(String name);Select(select esn from node_vo where name like GTS5900%)ListString getNodeListNoDistinctByName(String name);Select(select esn from node_vo where esn like msk00% and name ! )ListNodeVo getNodeListNoEmptyNameByEsn(String esn);Select(select esn from node_vo where esn like msk00%)ListNodeVo getNodeListByEsn(String esn);
}2. NodeService
Service
public class NodeService {Autowiredprivate NodeMapper nodeMapper;public ListString getNodeListByName(String name){// 通过sql语句中distinct去重return nodeMapper.getNodeListByName(name);}public SetString getNodeSetByName(String name){// 通过set集合去重return nodeMapper.getNodeSetByName(name);}public ListString getNodeListNoDistinctByName(String name){// 通过xxx.stream().distinct()去重ListString nodeListNoDistinctByName nodeMapper.getNodeListNoDistinctByName(name);return nodeListNoDistinctByName.stream().distinct().collect(Collectors.toList());}public ListNodeVo getNodeListNoEmptyNameByEsn(String esn){return nodeMapper.getNodeListNoEmptyNameByEsn(esn);}public ListNodeVo getNodeListByEsn(String esn){return nodeMapper.getNodeListByEsn(esn);}
}3. NodeController
RestController
public class NodeController {Autowiredprivate NodeService nodeService;RequestMapping(getNodeListByName)public ListString getNodeListByName(String name) {// 通过sql语句中distinct去重return nodeService.getNodeListByName(name);}RequestMapping(getNodeSetByName)public SetString getNodeSetByName(String name) {// 通过set集合去重return nodeService.getNodeSetByName(name);}RequestMapping(getNodeListNoDistinctByName)public ListString getNodeListNoDistinctByName(String name) {// 通过xxx.stream().distinct()去重return nodeService.getNodeListNoDistinctByName(name);}RequestMapping(getNodeListNoEmptyNameByEsn)public ListString getNodeListNoEmptyNameByEsn(String esn) {// 通过sql过滤name不为null的值ListNodeVo nodeListNoEmptyNameByEsn nodeService.getNodeListNoEmptyNameByEsn(esn);return nodeListNoEmptyNameByEsn.stream().map(NodeVo::getEsn).collect(Collectors.toList());}RequestMapping(getNodeListByEsn)public ListString getNodeListByEsn(String esn) {// 通过xxx.stream().filter()过滤name不为null的值ListNodeVo nodeListByEsn nodeService.getNodeListByEsn(esn);return nodeListByEsn.stream().map(NodeVo::getEsn).filter(Objects::nonNull).collect(Collectors.toList());}
}4.数据库数据 5.项目结构及源码
源码下载地址demo-springboot-mybatisplus欢迎Star 由于项目集成了SaToken框架需要先登录再访问测试NodeController接口