查询count1 和count2 这两个值的时候拖慢了整个sql的速度,本来1秒多的时间拖成了30+秒,该怎么修改?
SELECT tg. NAME, tg.GROUP_CODE, tab.*, ( SELECT COUNT(tu.ID) FROM user_prize up, tuser tu, certificate ce, tuser_group tuser_g WHERE up.userId = tu.id AND up.certificateId = ce.id AND tu.id = tuser_g.user_id AND ce.type = "s" AND tu.ID = tab.tusID ) count1, ( SELECT COUNT(tu.ID) FROM user_prize up, tuser tu, certificate ce, tuser_group tuser_g WHERE up.userId = tu.id AND up.certificateId = ce.id AND tu.id = tuser_g.user_id AND ce.type = "p" AND tu.ID = tab.tusID ) count2 FROM ( SELECT upz.id, upz.prizeCode, upz.sourceType, upz.sourceId, date_format(upz.prizeTime, "%Y-%m-%d") prizeTime, tus.user_code, tus.WORKDATE, tus. NAME tuName, tug.group_Id, cef.type, cef. NAME cefName, tus.ID tusID FROM user_prize upz, tuser tus, certificate cef, tuser_group tug WHERE upz.userId = tus.id AND upz.certificateId = cef.id AND tus.id = tug.user_id ) tab LEFT JOIN tgroup tg ON tab.group_Id = tg.ID WHERE 1 = 1
解决方案
10
送本人吧!
30
查询效率问题可以先执行explain查看 查询计划
通过查询计划查看那个步骤的扫描行比较多,对应建立索引大多数可以解决问题
通过查询计划查看那个步骤的扫描行比较多,对应建立索引大多数可以解决问题
10
领分来喽
20
以文本方式贴出
explain select …
show index from ..
以供分析。
explain select …
show index from ..
以供分析。
10
使用exlain一看就知晓了,求送分
20
索引确实是可以提高查询速度。
但是,假如你把你的select里的 子查询,修改为left join,估计速度会更快