zoukankan      html  css  js  c++  java
  • Mysql-explain之Using temporary和Using filesort解决方案

    1.

    执行时间-- 47s
    EXPLAIN
    SELECT
    a.pk_sr_main AS pk_id,
    c.pk_tfga_dxgl AS pk_tfid,
    a.mdjlx AS mdjlx,
    a.qhmc AS xqhmc,
    a.mhz AS mhz,
    a.mhzsfz AS mhzsfz,
    (
    SELECT
    count(1)
    FROM
    tfga_main e
    WHERE
    e.fk_sr_main = a.pk_sr_main
    AND e.sys_scbj = 0
    AND e.tsjly IN (3, 4, 6)
    AND ttfzt = 2
    AND e.sys_zdrq >= YEAR (NOW())
    ) AS ytfcs,
    c.dtfzq AS dtfzq,
    c.dtfzq AS dzt_text,
    f_lookup (9205, a.mdjlx) AS mdjlx_text,
    a.sys_xzqh AS sys_xzqh
    FROM
    sr_main a,
    tfga_dxgl c
    WHERE
    a.sys_xzqh LIKE concat(330212, '%')
    AND ddxlb IN (1, 2, 3)
    AND a.sys_scbj = 0
    AND c.dzt = 2
    AND a.pk_sr_main = c.fk_sr_main
    ORDER BY
    c.dszrq ASC
    LIMIT 50

    2.

    执行时间 --0.4s

    EXPLAIN
    SELECT
    a.pk_sr_main AS pk_id,
    a.mdjlx AS mdjlx,
    a.qhmc AS xqhmc,
    a.mhz AS mhz,
    a.mhzsfz AS mhzsfz,
    a.sys_xzqh AS sys_xzqh,
    f_lookup (9205, a.mdjlx) AS mdjlx_text,
    c.dtfzq AS dtfzq,
    c.dtfzq AS dzt_text,
    c.pk_tfga_dxgl AS pk_tfid,
    (
    SELECT
    count(1)
    FROM
    tfga_main e
    WHERE
    e.fk_sr_main = a.pk_sr_main
    AND e.sys_scbj = 0
    AND e.tsjly IN (3, 4, 6)
    AND ttfzt = 2
    AND e.sys_zdrq >= YEAR (NOW())
    ) AS ytfcs
    FROM
    sr_main a,
    tfga_dxgl c
    WHERE
    a.sys_xzqh LIKE concat(330212, '%')
    AND ddxlb IN (1, 2, 3)
    AND a.sys_scbj = 0
    AND c.dzt = 2
    AND a.pk_sr_main = c.fk_sr_main
    ORDER BY
    a.pk_sr_main ASC
    LIMIT 50

     3.总结:

    3.1order by 后的字段必须要命中索引,且多表联合查询时,order by的字段必须是数据最多的表

    3.2

    第一个sql的extra(额外信息)
    Using index condition; Using temporary; Using filesort -- Using filesort 九死一生的提示,需要尽快优化;说明mysql会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取mysql中无法利用索引完成的排序操作称为“文件排序”;
    -- Using temporary 十死无生的提示,极大影响mysql性能,需要尽快优化,使用了临时表保存中间结果,mysql在对查询结果排序时使用临时表。常见于排序order by和分组查询group by
    Using index condition; Using where
    Using index condition; Using where

    第二个sql的extra(额外信息)
    Using index condition -- Using index condition会先条件过滤索引,过滤完索引后找到所有符合索引条件的数据行,随后用 WHERE 子句中的其他条件去过滤这些数据行;
    Using index condition; Using where -- Using where表明使用了where过滤
    Using index condition; Using where

  • 相关阅读:
    wav格式
    python字符串操作
    云中Active Directory是如何工作的?
    Azure Active Directory中的特权身份管理如何运作?
    工作组下的共享设置
    重新审视虚拟桌面存储
    NAND
    如何使用PowerShell管理Windows服务
    如何应对云爆发架构?四种方法替你解忧
    配置网络策略中的 NAP 条件
  • 原文地址:https://www.cnblogs.com/tongcc/p/15020914.html
Copyright © 2011-2022 走看看