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