SELECT CONCAT( demandplan.spdm, demandplan.spmc, demandplan.scgg, demandplan.bzgg ) AS dmmc, LEFT(jzrq, 7) AS rq, MONTH(jzrq) AS "月份", ROUND((SUM(jf_sl) / SUM(sl)) * 100, 2) AS "达成率" FROM (SELECT ywy, yhrq, bb.yhbh, bb.pk_corp, sl, spdm, spmc, scgg, bzgg FROM cr_demandplan bb INNER JOIN (SELECT yhbh, pk_corp FROM cr_demandplan WHERE RecAs3 = "通过" GROUP BY yhbh, pk_corp) aa ON bb.yhbh = aa.yhbh AND bb.pk_corp = aa.pk_corp WHERE RecDR = 0) demandplan INNER JOIN cr_sasalesaccount ON demandplan.spdm = cr_sasalesaccount.spdm AND LEFT(yhrq, 7) = LEFT(jzrq, 7) AND demandplan.spdm = :M_spdm AND LEFT(jzrq, 7) BETWEEN :M_sdate AND :M_edate GROUP BY demandplan.spdm, rq
解决方案
20
语句比较复杂,一时看不清楚逻辑关系。
假如仅基于SQL来加索引,则建议一段一段来。
例如(SELECT
ywy,
yhrq,
bb.yhbh,
bb.pk_corp,
sl,
spdm,
spmc,
scgg,
bzgg
FROM
cr_demandplan bb
INNER JOIN
(SELECT
yhbh,
pk_corp
FROM
cr_demandplan
WHERE RecAs3 = “通过”
GROUP BY yhbh,
pk_corp) aa
ON bb.yhbh = aa.yhbh
AND bb.pk_corp = aa.pk_corp
WHERE RecDR = 0)
先看一下 explain 然后检查一下时间,假如需要优化,则可以在相对应的列上创建索引。例如针对 (SELECT
yhbh,
pk_corp
FROM
cr_demandplan
WHERE RecAs3 = “通过”
GROUP BY yhbh,
pk_corp) aa
可以创建索引 create index xx on cr_demandplan(RecAs3,yhbh,pk_corp)
假如仅基于SQL来加索引,则建议一段一段来。
例如(SELECT
ywy,
yhrq,
bb.yhbh,
bb.pk_corp,
sl,
spdm,
spmc,
scgg,
bzgg
FROM
cr_demandplan bb
INNER JOIN
(SELECT
yhbh,
pk_corp
FROM
cr_demandplan
WHERE RecAs3 = “通过”
GROUP BY yhbh,
pk_corp) aa
ON bb.yhbh = aa.yhbh
AND bb.pk_corp = aa.pk_corp
WHERE RecDR = 0)
先看一下 explain 然后检查一下时间,假如需要优化,则可以在相对应的列上创建索引。例如针对 (SELECT
yhbh,
pk_corp
FROM
cr_demandplan
WHERE RecAs3 = “通过”
GROUP BY yhbh,
pk_corp) aa
可以创建索引 create index xx on cr_demandplan(RecAs3,yhbh,pk_corp)
20
用了全表扫描了啊,type一栏里面有2个ALL描述,看到有ALL的据描述了,全表扫描没有走索引,所以比较慢,怎么样理解explain的结果数据:请参考blog地址:http://blog.csdn.net/mchdba/article/details/9190771