高洛峰2017-04-17 16:52:13
從資料庫最佳化角度出發,最後結果就一個數count(*)大量的關聯導致記憶體消耗跟時間浪費.
給個思路吧:
將sql結果做成如下:
最後count(*)等於每一行數量相乘最後相加,
即:(數量11*數量12*...*數量16)+(數量21*...*數量26)
以減少大量的笛卡爾積.
sql:
select t_for_sale.id, nvl(t1.count1, 0), ...
left join (select id, count(*) count1 from t_user group by id) t1 on t1.id=t_for_sale.id
....
from t_for_sale