今天收到一個(gè)需求,要改寫(xiě)一個(gè)報(bào)表的邏輯,當(dāng)改完之后,再次運(yùn)行,發(fā)現(xiàn)運(yùn)行超時(shí)。
因?yàn)樘厥庠?,無(wú)法訪問(wèn)客戶的服務(wù)器,沒(méi)辦法查看sql的執(zhí)行計(jì)劃、沒(méi)辦法知道表中的索引情況,所以,嘗試從語(yǔ)句的改寫(xiě)上來(lái)優(yōu)化。
一、原始語(yǔ)句如下:
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
原來(lái)是left join,雖然查詢比較慢,但是2分鐘能查出來(lái),現(xiàn)在按照業(yè)務(wù)要求,需要看到所有數(shù)據(jù),所以改成了full join,改了之后5分鐘都查不出結(jié)果。
二、改寫(xiě)后的代碼
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
由于代碼本身比較簡(jiǎn)單,沒(méi)辦法再進(jìn)一步簡(jiǎn)化,而由于連接不了服務(wù)器,其他的方法也用不上,甚至沒(méi)辦法分析到底是什么導(dǎo)致運(yùn)行這么慢。
想了想,full join 本質(zhì)上就是 2次left join+union ,無(wú)非就是合并數(shù)據(jù),于是嘗試一下用union all來(lái)直接合并數(shù)據(jù),現(xiàn)在改成unoin all最后,就不需要full join。
但是考慮到第2段代碼中并沒(méi)有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)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 簡(jiǎn)單了解MySQL union all與union的區(qū)別
- MySQL如何使用union all獲得并集排序
- MySQL中UNION與UNION ALL的基本使用方法
- 淺析mysql union和union all
- SQL語(yǔ)句之Union和Union All的用法
- MySQL中使用or、in與union all在查詢命令下的效率對(duì)比
- Mysql聯(lián)合查詢UNION和UNION ALL的使用介紹
- Sql學(xué)習(xí)第一天——SQL UNION 和 UNION ALL 操作符認(rèn)識(shí)
- sqlserver Union和SQL Union All使用方法
- sql 查詢結(jié)果合并union all用法_數(shù)據(jù)庫(kù)技巧
- 數(shù)據(jù)庫(kù)中union 與union all 的區(qū)別
- sql Union和Union All的使用方法