数据库查询优化之子查询优化
作者:qq_43193797 时间:2024-01-27 18:25:25
1. 案例
取所有不为掌门人的员工,按年龄分组!
select age as '年龄', count(*) as '人数' from t_emp where id not in
(select ceo from t_dept where ceo is not null) group by age;
如何优化?
①解决dept表的全表扫描,建立ceo字段的索引:
此时,再次查询:
②进一步优化,替换not in。
上述SQL可以替换为:
select age as '年龄',count(*) as '人数' from emp e left join dept d on e.id=d.ceo where d.id is null group by age;
结论: 在范围判断时,尽量不要使用not in和not exists,使用 left join on xxx is null代替。
总结
来源:https://blog.csdn.net/qq_43193797/article/details/85252408
标签:mysql,数据库,子查询,优化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
文档对象模型DOM通俗讲解
2024-05-13 09:36:55
![](https://img.aspxhome.com/file/2023/0/125450_0s.gif)
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
2024-01-26 20:09:26
python中Requests请求的安装与常见用法
2022-08-15 20:36:35
Node+OCR实现图像文字识别功能
2024-04-22 13:01:41
![](https://img.aspxhome.com/file/2023/3/135973_0s.gif)
javascript+css实现俄罗斯方块小游戏
2024-04-23 09:28:06
![](https://img.aspxhome.com/file/2023/9/135519_0s.jpg)
Python 实现引用其他.py文件中的类和类的方法
2023-08-02 10:38:01
解决Win7 x64安装解压版mysql 5.7.18 winx64出现服务无法启动问题
2024-01-22 07:57:39
![](https://img.aspxhome.com/file/2023/5/117835_0s.png)
JavaScript 数组中最大最小值
2024-04-27 15:23:56
python下的opencv画矩形和文字注释的实现方法
2022-12-26 22:27:17
golang通过mysql语句实现分页查询
2024-01-23 13:30:03
![](https://img.aspxhome.com/file/2023/4/130724_0s.png)
Mysql 数据库结构及索引类型
2024-01-20 23:40:35
![](https://img.aspxhome.com/file/2023/6/108906_0s.webp)
asp 在线备份与恢复sqlserver数据库的代码
2011-03-06 11:14:00
排版自适应提升可访问性
2009-04-08 12:47:00
![](https://img.aspxhome.com/file/UploadPic/20094/8/01-80s.jpg)
谈谈网页设计中的字体应用 (1) Font Set
2009-11-24 12:55:00
![](https://img.aspxhome.com/file/UploadPic/200911/24/202939796-78s.gif)
分页存储过程(用存储过程实现数据库的分页代码)
2024-01-20 05:22:57
Django Channels 实现点对点实时聊天和消息推送功能
2022-04-03 10:37:56
Python实现学生管理系统的完整代码(面向对象)
2023-09-11 19:08:55
![](https://img.aspxhome.com/file/2023/4/100984_0s.png)
网站的视觉路径和版式设计
2008-04-15 14:35:00
![](https://img.aspxhome.com/file/UploadPic/20084/15/2008415144523556s.jpg)
用tensorflow构建线性回归模型的示例代码
2022-04-12 03:41:47
Python线性回归实战分析
2023-05-19 04:35:42
![](https://img.aspxhome.com/file/2023/5/65055_0s.png)