当前位置:网站首页>15 tips about SQL optimization
15 tips about SQL optimization
2022-08-06 16:27:00 【InfoQ】
前言
1 避免使用select *
select * from user where id=1;select name,age from user where id=1;2 用union all代替union
(select * from user where id=1) union (select * from user where id=2);(select * from user where id=1) union all(select * from user where id=2);3 小表驱动大表
select * from orderwhere user_id in (select id from user where status=1)select * from orderwhere exists (select 1 from user where order.user_id = user.id and status=1)- in 适用于左边大表,右边小表.
- exists 适用于左边小表,右边大表.
4 批量操作
for(Order order: list){ orderMapper.insert(order):}insert into order(id,code,user_id) values(123,'001',100);orderMapper.insertBatch(list):insert into order(id,code,user_id) values(123,'001',100),(124,'002',100),(125,'003',101);5 多用limit
select id, create_date from order where user_id=123 order by create_date asc;List<Order> list = orderMapper.getOrderList();Order order = list.get(0);select id, create_date from order where user_id=123 order by create_date asc limit 1;update order set status=0,edit_time=now(3) where id>=100 and id<200 limit 100;6 in中值太多
select id,name from categorywhere id in (1,2,3...100000000);select id,name from categorywhere id in (1,2,3...100)limit 500;public List<Category> getCategory(List<Long> ids) { if(CollectionUtils.isEmpty(ids)) { return null; } if(ids.size() > 500) { throw new BusinessException("一次最多允许查询500条记录") } return mapper.getCategoryList(ids);}7 增量查询
select * from user;select * from user where id>#{lastId} and create_time >= #{lastCreateTime} limit 100;8 高效的分页
select id,name,age from user limit 10,20;select id,name,age from user limit 1000000,20;select id,name,age from user where id > 1000000 limit 20;select id,name,age from user where id between 1000000 and 1000020;9 用连接查询代替子查询
select * from orderwhere user_id in (select id from user where status=1)select o.* from order oinner join user u on o.user_id = u.idwhere u.status=110 join的表不宜过多
select a.name,b.name.c.name,d.namefrom a inner join b on a.id = b.a_idinner join c on c.b_id = b.idinner join d on d.c_id = c.idinner join e on e.d_id = d.idinner join f on f.e_id = e.idinner join g on g.f_id = f.idselect a.name,b.name.c.name,a.d_name from a inner join b on a.id = b.a_idinner join c on c.b_id = b.id11 join时要注意
- left join:求两个表的交集外加左表剩下的数据.
- inner join:求两个表交集的数据.
select o.id,o.code,u.name from order o inner join user u on o.user_id = u.idwhere u.status=1;select o.id,o.code,u.name from order o left join user u on o.user_id = u.idwhere u.status=1;12 控制索引的数量
13 选择合理的字段类型
alter table order add column code char(20) NOT NULL;alter table order add column code varchar(20) NOT NULL;- 能用数字类型,就不用字符串,因为字符的处理往往比数字要慢.
- 尽可能使用小的类型,比如:用bit存布尔值,用tinyint存枚举值等.
- 长度固定的字符串字段,用char类型.
- 长度可变的字符串字段,用varchar类型.
- 金额字段用decimal,避免精度丢失问题.
14 提升group by的效率
select user_id,user_name from ordergroup by user_idhaving user_id <= 200;select user_id,user_name from orderwhere user_id <= 200group by user_id15 索引优化
explain select * from `order` where code='002';边栏推荐
- Easyexcel导出 只显示出想要的字段
- 数据查询优化技术方案
- [C Supplement] Precautions for two-dimensional arrays as formal parameters
- navicat远程连接数据库遇到的问题 10060 unknown error
- 普林斯顿微积分读本04第三章--极限导论
- Uniapp is highly adaptive, just add mode="widthFix" to the image tag
- 13. SAP ABAP OData 服务的分页加载数据集的实现(Paging)
- dedecms搜索结果页每页显示条数不生效
- 主成分分析(PCA)与线性判别分析(LDA)的区别与联系
- 网上开户佣金万一安全吗?手机办理流程是怎样的
猜你喜欢

小程序中实现搜索功能

JVM: (4) Program counter in runtime data area

组件开发实战-数字输入框和标签页组件

Unity editor extension - top menu bar extension
![[Shader realizes the expansion effect of FishEye fisheye picture_Shader effect 16]](/img/57/e822c82009dba3b7b38f622be45059.png)
[Shader realizes the expansion effect of FishEye fisheye picture_Shader effect 16]

训练阶段yolov7主干部分结构图
![有重复字符的排列组合[回溯 & 剪枝去重 || set去重]](/img/b7/636aeb3af5603e1d945b175af56500.png)
有重复字符的排列组合[回溯 & 剪枝去重 || set去重]

直播现状:小程序直播电商解决方案,让直播更高效

Electron学习——解决npm install electron --save-dev出错/缓慢的问题

数据查询优化技术方案
随机推荐
抖音 滑块验证方案 s_v_web_id 参数分析
ASEMI整流桥GBL610参数,GBL610尺寸,GBL610特征
挖财帮开通的华泰证券VIP账户是安全的吗?怎么开
Activiti部署文件时,报错org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘process‘
什么是数据库
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
训练阶段yolov7主干部分结构图
yum、apt-get、curl、wget和pip的使用范围
一天学习一家上市公司:绝味食品
8/5 基础思维(div2 A、B、C、D)+dp+AC自动机
Easyexcel导出 只显示出想要的字段
MT6853/MT6873 视频彩铃概率性无图像
C语言函数参数中的三个点(三点 “...”)是干什么用的?(可变参数)<stdarg.h>、va_start 宏、va_arg 宏、va_end 宏
Understand the principles of xss, csrf, ddos attacks and how to avoid them
云原生正在吞噬一切,开发者该如何应对?
【freertos】013-任务通知及其实现细节
社区人物志 | 朱小力:Doris 社区新鲜力量
如何用WebGPU流畅渲染千万级2D物体:基于光追管线
std::string、const char*、QString之间相互转换
离线安装Pyenv