小編給大家分享一下數(shù)據(jù)庫(kù)中如何使用LEFT JOIN優(yōu)化多個(gè)子查詢,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)是一家專(zhuān)注于成都做網(wǎng)站、網(wǎng)站建設(shè)與策劃設(shè)計(jì),烏海海南網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:烏海海南等地區(qū)。烏海海南做網(wǎng)站價(jià)格咨詢:135182197921.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;
優(yōu)化方向: 合并多個(gè)t_asset_file子查詢
優(yōu)化方法: 使用left join 來(lái)代替子查詢,把關(guān)聯(lián)列放在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;
優(yōu)化方向:合并重復(fù)的子查詢
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)優(yōu)化方向:
(1).主查詢和子查詢涉及表相同,并且條件有很多共同點(diǎn),可以考慮合并。
(2).子查詢結(jié)構(gòu)相似,考慮使用left join來(lái)進(jìn)行合并。
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(+)以上是“數(shù)據(jù)庫(kù)中如何使用LEFT JOIN優(yōu)化多個(gè)子查詢”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
當(dāng)前題目:數(shù)據(jù)庫(kù)中如何使用LEFTJOIN優(yōu)化多個(gè)子查詢-創(chuàng)新互聯(lián)
本文地址:http://weahome.cn/article/jpshi.html