sql優(yōu)化實(shí)戰(zhàn) 把full join改為left join +union all(從5分鐘降為10秒)
今天收到一個(gè)需求,要改寫一個(gè)報(bào)表的邏輯,當(dāng)改完之后,再次運(yùn)行,發(fā)現(xiàn)運(yùn)行超時(shí)。
因?yàn)樘厥庠颍瑹o法訪問客戶的服務(wù)器,沒辦法查看sql的執(zhí)行計(jì)劃、沒辦法知道表中的索引情況,所以,嘗試從語句的改寫上來優(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)致運(yùn)行這么慢。
想了想,full join 本質(zhì)上就是 2次left join+union ,無非就是合并數(shù)據(jù),于是嘗試一下用union all來直接合并數(shù)據(jù),現(xiàn)在改成unoin all最后,就不需要full join。
但是考慮到第2段代碼中并沒有prod_id這個(gè)字段,所以這里在第2段代碼加上了cross apply隨便取出一個(gè)產(chǎn)品的id,這樣就有prod_id這個(gè)字段,可以合并了。
修改之后,果然速度降到了10多秒。
到此這篇關(guān)于sql優(yōu)化實(shí)戰(zhàn) 把full join改為left join +union all(從5分鐘降為10秒)的文章就介紹到這了,更多相關(guān)left join +union all內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)據(jù)庫的三級(jí)模式和兩級(jí)映射介紹
在這里大家一定要注意三級(jí)模式中的概念模式對(duì)應(yīng)的是數(shù)據(jù)庫設(shè)計(jì)的邏輯模型,而不是概念模型(E-R模型),一定不要弄混了2012-10-10SQL之left join、right join、inner join的區(qū)別淺析
這篇文章主要介紹了SQL之left join、right join、inner join的區(qū)別淺析,本文講解了它們的區(qū)別并給出了實(shí)例,需要的朋友可以參考下2015-02-02Dbeaver如何從一個(gè)數(shù)據(jù)庫復(fù)制表到另外一個(gè)數(shù)據(jù)庫
在數(shù)據(jù)庫管理中,導(dǎo)出表是一項(xiàng)常見操作,可以通過特定的工具或數(shù)據(jù)庫自帶的功能實(shí)現(xiàn),步驟包括:1.在數(shù)據(jù)庫管理軟件中找到需導(dǎo)出的表,右鍵選擇導(dǎo)出數(shù)據(jù),2.選擇目標(biāo)數(shù)據(jù)庫,并進(jìn)行表映射設(shè)置,3.根據(jù)需求調(diào)整導(dǎo)出參數(shù),4.執(zhí)行操作完成數(shù)據(jù)導(dǎo)出2024-10-10Windows10用Navicat?定時(shí)備份報(bào)錯(cuò)80070057的問題解析
這篇文章主要介紹了Windows10用Navicat?定時(shí)備份報(bào)錯(cuò)80070057的問題,本文通過圖文并茂的形式給大家分享問題所在原因及解決方案,需要的朋友可以參考下2023-10-10淺談一下數(shù)據(jù)庫系統(tǒng)的發(fā)展與組成
這篇文章主要介紹了淺談一下數(shù)據(jù)庫系統(tǒng)的發(fā)展與組成,數(shù)據(jù)庫系統(tǒng),指在計(jì)算機(jī)系統(tǒng)中引入數(shù)據(jù)庫后的系統(tǒng),一般由數(shù)據(jù)庫、數(shù)據(jù)庫管理系統(tǒng)、應(yīng)用系統(tǒng)、數(shù)據(jù)庫管理員(DBA)構(gòu)成,本文就數(shù)據(jù)庫的發(fā)展展開詳細(xì)講解2023-07-07