這篇文章主要介紹sql中如何將full join改為left join +union all,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)十年經(jīng)驗成就非凡,專業(yè)從事網(wǎng)站設(shè)計制作、網(wǎng)站制作,成都網(wǎng)頁設(shè)計,成都網(wǎng)頁制作,軟文營銷,一元廣告等。十年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18980820575,我們期待您的來電!今天收到一個需求,要改寫一個報表的邏輯,當(dāng)改完之后,再次運行,發(fā)現(xiàn)運行超時。
因為特殊原因,無法訪問客戶的服務(wù)器,沒辦法查看sql的執(zhí)行計劃、沒辦法知道表中的索引情況,所以,嘗試從語句的改寫上來優(yōu)化。
一、原始語句如下:
select isnull(vv.customer_id,v.customer_id) as customer_id, isnull(vv.business_date,replace(v.business_date,'-','')) as business_date, v.prod_id, v.sales, vv.visit_count, v.all_sales from ( SELECT a.customer_id , max(month)+'-01' as business_date, a.PROD_ID , SUM(CAST(VALUE AS NUMERIC(38, 3))) sales, sum(SUM(CAST(VALUE AS NUMERIC(38, 3)))) over(partition by a.customer_id) as all_sales FROM TB_IMPORT_SALES a WHERE a.customer_id IS NOT NULL AND a.PROD_ID IS NOT NULL and a.month='2016-11' GROUP BY a.customer_id , a.PROD_ID )v full join ( SELECT customer_id, max(a.business_date) as business_date, COUNT(*) AS VISIT_COUNT FROM TB_CALL_STORE a WITH(NOLOCK) inner join TB_TIME d on a.business_date = d.t_date where d.section ='2016-11' GROUP BY customer_id )vv on v.customer_id = vv.customer_id
原來是left join,雖然查詢比較慢,但是2分鐘能查出來,現(xiàn)在按照業(yè)務(wù)要求,需要看到所有數(shù)據(jù),所以改成了full join,改了之后5分鐘都查不出結(jié)果。
二、改寫后的代碼
select v.customer_id, replace(max(v.business_date),'-','') as business_date, v.prod_id, max(v.sales_volume) sales_volume , max(v.visit_count) visit_count, max(v.all_sales_volume) all_sales_volume from ( SELECT a.customer_id , max(biz_month)+'-01' as business_date, a.PROD_ID , SUM(CAST(VALUE1 AS NUMERIC(38, 8))) sales_volume, sum(SUM(CAST(VALUE1 AS NUMERIC(38, 8)))) over(partition by a.customer_id) as all_sales_volume, null as visit_count FROM TB_IMPORT_SALES a WHERE a.customer_id IS NOT NULL AND a.PROD_ID IS NOT NULL and a.month='2016-11' GROUP BY a.customer_id , a.PROD_ID union all SELECT customer_id, max(a.business_date) as business_date, p.prod_id, null, null, COUNT(*) AS VISIT_COUNT FROM TB_CALL_STORE a WITH(NOLOCK) cross apply ( select top 1 prod_id from TB_PRODUCT with(nolock) )p inner join TB_TIME d on a.business_date = d.t_date where d.section ='2016-11' GROUP BY customer_id,p.prod_id )v group by v.customer_id, v.prod_id
由于代碼本身比較簡單,沒辦法再進(jìn)一步簡化,而由于連接不了服務(wù)器,其他的方法也用不上,甚至沒辦法分析到底是什么導(dǎo)致運行這么慢。
想了想,full join 本質(zhì)上就是 2次left join+union ,無非就是合并數(shù)據(jù),于是嘗試一下用union all來直接合并數(shù)據(jù),現(xiàn)在改成unoin all最后,就不需要full join。
但是考慮到第2段代碼中并沒有prod_id這個字段,所以這里在第2段代碼加上了cross apply隨便取出一個產(chǎn)品的id,這樣就有prod_id這個字段,可以合并了。
修改之后,果然速度降到了10多秒。
以上是“sql中如何將full join改為left join +union all”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:sql中如何將fulljoin改為leftjoin+unionall-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://muchs.cn/article44/idgee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、微信小程序、做網(wǎng)站、Google、域名注冊、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)