有关多表Left join的优化
SELECT
COUNT(DISTINCT T1.A1) + COUNT(DISTINCT T2.B1) +
COUNT(DISTINCT T3.C1) FROM T1
LEFT JOIN T2 on T1.A1 = T2.A1
LEFT JOIN T3 on T1.A1 = T3.A1
LEFT JOIN T4 on T3.C1 = T4.C1
GROUP BY T1.A2, T1.A3
上面这个SQL效率太差了,
T1,T2,T3,T4表达到5千条的时候,需要两,三分钟
希望大家帮忙优化一下
多谢大家帮帮忙吧!
------解决方案--------------------
LEFT JOIN T4 on T3.C1 = T4.C1
不需要,先去掉
------解决方案--------------------需要这样的连接,除了索引上优化,没什么可优化的了.
------解决方案--------------------在A1,C1上建索引就可以了,5千条不多
------解决方案--------------------
SQL code
--try:
SELECT
COUNT(DISTINCT T1.A1) + COUNT(DISTINCT T2.B1) +
COUNT(DISTINCT T3.C1) FROM T1
inner JOIN T2 on T1.A1 = T2.A1
inner JOIN T3 on T1.A1 = T3.A1
GROUP BY T1.A2, T1.A3