SELECT field_abcd AS field_abcd
FROM ( (SELECT ad.oi AS oioi, ad.ad AS adad
FROM dd
LEFT JOIN da ON (da.dac = dd.dac AND da.ti = '111')
LEFT JOIN ad ON (dd.oi = ad.oi AND ad.ti = '111' AND
ad.odan = 'o6' and
ad.id = 0)
left join dsd on (ad.ad = dsd.pk)
WHERE 1 = 1
AND ((dd.ti = '71570') and dd.odan = 'o6' and
dd.id = 0)
order by oioi) as v1 RIGHT JOIN (SELECT dsd.fm AS fmfm,
ad.v8 AS field_abcd,
ad.ad AS adad,
ad.oi AS oioi
FROM dd
LEFT JOIN da
ON (da.dac = dd.dac AND
da.ti = '111')
LEFT JOIN ad
ON (dd.oi = ad.oi AND
ad.ti = '71570' AND
ad.odan = 'o6' and
ad.id = 0)
left join dsd on (ad.ad = dsd.pk)
WHERE 1 = 1
AND dd.ti = '111'
and dd.odan = 'o6'
and dd.id = 0
order by oioi) as v2 ON
(v1.oioi = v2.oioi and
v1.adad = v2.adad) ) AS MID;
如上sql v1 和 v2 只有157行数据, 执行需要16s 当我对 v1 加上limit 1000000时只需要2s,
并且我更换条件从o6 改为 o4 这是,v1 和 v2 数据大约在3000行左右 也没加limit 执行速度却只有1.5s左右, 有没有人知道这是什么原因
现在我解决的方法是 v1 v2都加上order by oioi 速度更快。
先格式化好代码吧~ 难以阅读~
同意
先格式化好代码吧
首先我先说下我个人不喜欢的一点是where条件1=1为啥要有这个,我有几个同事也是...
好了,入正题,按题主描述,加上limit 1000000时,2s;不加很慢。那应当是笛卡尔积(题主可了解下)。
其次如果你用的可视化工具使用navicat的话,可以使用图中圈2的功能,会告诉你哪里慢,也就是我们所说的sql执行计划。
加油,自己找答案_
在线等
– wwxxaa 4年前