数据库查询优化之子查询优化
16lz
2021-12-24
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;
总结
更多相关文章
- Android(安卓)中数据库查询方法 query() 中的 select
- Android(安卓)中数据库查询方法 query() 中的参数
- Android之子线程更新UI
- 参考demo3.php, 完善数据库查询构造器其他的操作,update,delete,i
- 数据库查询构造器
- PHP:oop->重载之set/get/call/callStatic,oop事件委托,数据库查询
- 数据库查询多少条数据
- android 数据库查询记录
- android启动之子系统切换