12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
mysql数据库 时间:2024-11-28 13:24:56
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
1. 案例取所有不为掌门人的员工,按年龄分组!selectageas'年龄',count(*)as'人数'fromt_empwhereidnotin(selectceofromt_deptwhereceoisnotnull)groupbyage;
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
1. 案例
取所有不为掌门人的员工,按年龄分组!
selectageas'年龄',count(*)as'人数'fromt_empwhereidnotin(selectceofromt_deptwhereceoisnotnull)groupbyage;
如何优化?
①解决dept表的全表扫描,建立ceo字段的索引:
此时,再次查询:
②进一步优化,替换not in。
上述SQL可以替换为:
selectageas'年龄',count(*)as'人数'fromempeleftjoindeptdone.id=d.ceowhered.idisnullgroupbyage;
结论: 在范围判断时,尽量不要使用not in和not exists,使用 left join on xxx is null代替。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19