• ADADADADAD

    数据库查询优化之子查询优化的示例分析[ mysql数据库 ]

    mysql数据库 时间:2024-11-28 13:24:56

    作者:文/会员上传

    简介:

    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代替。

    数据库查询优化之子查询优化的示例分析.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: mysql数据库