您好,登錄后才能下訂單哦!
小編給大家分享一下數據庫中如何使用LEFT JOIN優化多個子查詢,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1.SQL1
OLD:
SELECT COUNT (1) num FROM ( SELECT t1.* FROM t_asset t1 WHERE 1 = 1 AND t1.TYPE = 0 AND ( t1.status IN (1, 10, 11, 12, 100) OR (EXISTS (SELECT b.resource_id FROM t_asset_file b WHERE t1.resource_id = b.asset_code AND t1.status IN (3, 4, 8) AND b.status IN (1, 10, 11, 12)))) AND ( EXISTS (SELECT 1 FROM t_asset_file a1 WHERE t1.resource_id = a1.asset_code AND ( a1.content_status = 1 OR a1.content_status = 4)) OR NOT EXISTS (SELECT 1 FROM t_asset_file a1 WHERE t1.resource_id = a1.asset_code)) ORDER BY t1.create_time DESC, t1.resource_id) a;
優化方向: 合并多個t_asset_file子查詢
優化方法: 使用left join 來代替子查詢,把關聯列放在group by中,將子查詢中不同條件使用case when
SELECT COUNT (1) num FROM ( SELECT t1.* FROM t_asset t1, ( SELECT asset_code, MAX (CASE WHEN status IN (1, 10, 11, 12) THEN 1 ELSE 0 END) status, MAX ( CASE WHEN content_status = 1 OR content_status = 4 THEN 1 ELSE 0 END) content_status FROM t_asset_file GROUP BY asset_code) t2 WHERE t1.resource_id = t2.asset_code(+) AND t1.TYPE = 0 AND ( t1.status IN (1, 10, 11, 12, 100) OR (t1.status IN (3, 4, 8) AND t2.status(+) = 1)) AND ( (t2.content_status(+) = 1) OR (t2.asset_code IS NULL)) ORDER BY t1.create_time DESC, t1.resource_id) a;
2.SQL2
OLD:
SELECT m.khbh, d.wzzbm, wmsys.wm_concat (m.rkid) rkids FROM m@dblink m, d@dblink d WHERE m.rkid = d.rkid AND m.rkzt = 2 AND m.ssny < '201311' AND m.zxdid IS NULL AND ( NOT EXISTS (SELECT 1 FROM m@dblink m1, d@dblink d1 WHERE m1.rkid = d1.rkid AND m1.zxdid = m.rkid AND d1.wzzbm = d.wzzbm AND m1.rkzt = 3) OR (SELECT SUM (d1.xysl) FROM m@dblink m1, d@dblink d1 WHERE m1.rkid = d1.rkid AND m1.zxdid = m.rkid AND d1.wzzbm = d.wzzbm AND m1.rkzt = 3) < d.xysl) GROUP BY m.khbh, d.wzzbm;
優化方向:合并重復的子查詢
SELECT m.khbh, d.wzzbm, wmsys.wm_concat (m.rkid) rkids FROM m@dblink m, d@dblink d, (select m1.zxdid,d1.wzzbm, sum(d1.xysl) sum_xysl from m@dblink m1,d@dblink d1 where m1.rkzt = 3 and m1.rkid = d1.rkid group by m1.zxdid,d1.wzzbm) z WHERE m.rkid = d.rkid AND m.rkzt = 2 AND m.ssny < '201311' AND m.zxdid IS NULL and z.zxdid=m.rkid and z.wzzbm=d.wzzbm AND ( (z.zxdid is null and z.wzzbm is null) OR z.sum_xysl < d.xysl) GROUP BY m.khbh, d.wzzbm;
3.SQL3
OLD:
select m.col1,d.col2,wmsys.wm_concat(m.col3) col3s from m,d where m.col3=d.col3 and m.col6=2 and m.col7<'201312' and m.col4 is null and (not exists (select 1 from m m1,d d1 where m1.col3=d1.col3 and m1.col4=m.col3 and d1.col2=d.col2 and m1.col7< '201312' and m1.col6=3) or (select sum(d1.col5) from m m1,d d1 where m1.col3=d1.col3 and m1.col4=m.col3 and d1.col2=d.col2 and m1.col7<'201312' and m1.col6=3)<d.col5) group by m.col1,d.col2;
優化方向:
(1).主查詢和子查詢涉及表相同,并且條件有很多共同點,可以考慮合并。
(2).子查詢結構相似,考慮使用left join來進行合并。
NEW:
with aa as( select m.col1,d.col2,m.col3,m.col4,d1.col5 from m,d where m.col3=d.col3 and m.col6 in(2,3) and m1.col7< '201312') select aa.col1,aa.col2, wmsys.wm_concat(aa.col3) col3s from aa, (select col4,col2,sum(d1.col5) sum_col5 from aa where col6=3 group by col4,col2) bb where aa.col3=bb.col4(+) and aa.col2=bb.col2(+) and aa.col4 is null and aa.col6=2 and (bb.col4 is null or bb.sum_col5(+)<aa.col5)
以上是“數據庫中如何使用LEFT JOIN優化多個子查詢”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。