当前位置: 首页 > news >正文

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

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

SQL高可用优化

  • 一、优化SQL包含distinct场景
  • 二、优化SQL中Where条件中索引字段是否为NULL
  • 三、代码验证
    • 1. NodeMapper
    • 2. NodeService
    • 3. NodeController
    • 4.数据库数据
    • 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 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 {@Autowiredprivate 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 {@Autowiredprivate 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接口
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/448956.html

相关文章:

  • openharmony源码编译
  • H.264编解码工具 - NVIDIA CUDA
  • 数学建模小练习
  • Java爬虫:获取SKU详细信息的艺术
  • 心理咨询展示网站建设渠道拓展
  • naocs注册中心,配置管理,openfeign在idea中实现模块间的调用,getway的使用
  • 先进封装技术 Part02---TSV科普
  • 【数据挖掘】2023年 Quiz 1-3 整理 带答案
  • 老古董Lisp实用主义入门教程(12):白日梦先生的白日梦
  • UE5 Windows热更新解决方案思路(HotPatcher+Tomcat+RuntimeFilesDownloader)
  • 进程管理工具:非daemon进程管理工具supervisor
  • c++模拟真人鼠标轨迹算法
  • android12/13/14版本wms最新面试题:dumpsys window和sf一定会一致么?
  • Python脚本示例,你可以使用这个脚本来自动化登录网站、选择页面元素和提交表单
  • 安卓13设置动态修改设置显示版本号 版本号增加信息显示 android13增加序列号
  • 从 Oracle 集群到单节点环境(详细记录一次数据迁移过程)之三:在目标服务器上恢复数据
  • 相互作用感知的 3D 分子生成 VAE 模型 - DeepICL 评测
  • Java实现随机抽奖的方法有哪些
  • grafana加载缓慢解决方案
  • 【湖南步联科技身份证】 身份证读取与酒店收银系统源码整合———未来之窗行业应用跨平台架构
  • 多路复用和事件轮询机制
  • Android常用C++特性之std::abs
  • LabVIEW提高开发效率技巧----使用状态机架构
  • Feign:服务挂了也不会走fallback
  • 网络编程操作—函数
  • 博客摘录「 GD32的flash读、擦除、写操作」2024年9月2日
  • 【性能优化】低配starRocks常驻内存优化
  • 科研绘图系列:R语言树结构聚类热图(cluster heatmap)
  • 微信小程序操作蓝牙
  • Python爱心射线(完整代码)