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;

总结

更多相关文章

  1. Android(安卓)中数据库查询方法 query() 中的 select
  2. Android(安卓)中数据库查询方法 query() 中的参数
  3. Android之子线程更新UI
  4. 参考demo3.php, 完善数据库查询构造器其他的操作,update,delete,i
  5. 数据库查询构造器
  6. PHP:oop->重载之set/get/call/callStatic,oop事件委托,数据库查询
  7. 数据库查询多少条数据
  8. android 数据库查询记录
  9. android启动之子系统切换

随机推荐

  1. 序列化/反序列化,我忍你很久了,淦!
  2. WebAssembly 正式成为 Web 的第四种语言[
  3. 一个Java对象到底占多大内存?
  4. “你入我麾下,我给你天下”
  5. JeecgBoot低代码快速开发框架,用于生产环
  6. 再肝两夜,写了个「服务器项目部署」实战PD
  7. 给你5分钟白漂:我的常用在线工具网站
  8. 2020 年 Node.js 将会有哪些新功能[每日
  9. 68.查看子进程脚本
  10. Vue.js 中的无渲染行为插槽[每日前端夜话