sql優化實戰 把full join改為left join +union all(從5分鐘降為10秒)
阿新 • • 發佈:2020-09-24
今天收到一個需求,要改寫一個報表的邏輯,當改完之後,再次執行,發現執行超時。
因為特殊原因,無法訪問客戶的伺服器,沒辦法檢視sql的執行計劃、沒辦法知道表中的索引情況,所以,嘗試從語句的改寫上來優化。
一、原始語句如下:
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分鐘能查出來,現在按照業務要求,需要看到所有資料,所以改成了full join,改了之後5分鐘都查不出結果。
二、改寫後的程式碼
select v.customer_id,replace(max(v.business_date),'') as business_date,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,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,p.prod_id,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
由於程式碼本身比較簡單,沒辦法再進一步簡化,而由於連線不了伺服器,其他的方法也用不上,甚至沒辦法分析到底是什麼導致執行這麼慢。
想了想,full join 本質上就是 2次left join+union ,無非就是合併資料,於是嘗試一下用union all來直接合並資料,現在改成unoin all最後,就不需要full join。
但是考慮到第2段程式碼中並沒有prod_id這個欄位,所以這裡在第2段程式碼加上了cross apply隨便取出一個產品的id,這樣就有prod_id這個欄位,可以合併了。
修改之後,果然速度降到了10多秒。
到此這篇關於sql優化實戰 把full join改為left join +union all(從5分鐘降為10秒)的文章就介紹到這了,更多相關left join +union all內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!