select A.aId, (select count(1) from B where B.aId = a.aId) c from A
order by c
表A有30W+數據
表B有18W+數據
用上面sql查要10分鐘,如果去掉order by則在2秒內,求高手指導怎麼優化
有高人指點,這樣寫能控制在10秒左右,但不是理解
select A.aId, NVL(B.c,0) from A LEFT JOIN (select aId,count(1) c from B group by aId) B ON A.AID=B.AID
order by B.c