极限优化:这个SQL语句还可以优化吗??
当数据量超大的时候,这个语句效率极其低下,各位大虾,请看看这个SQL语句还能继续优化吗?求极限优化!!
select m.*,
(select count(1) from message B where b.msID=m.msID ) as MsgTag ,
(select count(1) from message C where c.msID=m.msID and c.deleted=1) as MsgTag_Del ,
from gather_QQ_groups M
------解决方案--------------------
SELECT m.*, C.MsgTag, C.MsgTag_Del
FROM gather_QQ_groups M
LEFT JOIN (SELECT msID, COUNT(msID) AS MsgTag, SUM(CASE WHEN deleted=1 THEN 1 ELSE 0 END) AS MsgTag_Del FROM message GROUP BY msID) C ON M.msID=C.msID