事件:提速從24秒提速到1秒內,關鍵是這個sql語句的奇怪現象讓我感覺很不可思議,粘出來**。大家分享下!
先前語句sql,
with temp as
(select d.fzherenitcode,p.objid release
from mb_recruitmentdoc d
inner join mb_recruitmentdoc_antifilled an on an.demandid=d.demandid and d.thisflowenddate>'2010-4-1 0:00:00' and d.thisflowenddate<='2011-3-31 0:00:00'
inner join position p on p.objid=an.stvacancypostnum
),onboard as
(select a.recommenditcode itcode, c.id onnum from mb_resume_recommend a
inner join mb_recommend_resume b on a.recommendid=b.recommendid and a.recommendtime>'2010-4-1 0:00:00' and a.recommendtime<='2011-3-31 0:00:00'
inner join mb_resume_dc_userinfo c on c.resumeid=b.resumeid
)select a.itcode,
isnull(count(t.release),0) release,
isnull(sum(m.resumeid),0) input,
isnull(sum(r1.recommendtype),0) receive,
isnull(sum(r3.recommendcount),0) business,
isnull(sum(r2.recommendcount),0) send,
isnull(count(1),0) insum
from accounts_users a
left join temp t on t.fzherenitcode=a.itcode
left join mb_posinal m on m.sourceitcode=a.itcode and m.sourceid=2015 and m.lastmodifytime>'2010-4-1 0:00:00' and m.lastmodifytime<='2011-3-31 0:00:00'
left join mb_resume_recommend r1 on r1.recommendeditcode=a.itcode and r1.recommendtype!=3 and r1.recommendtime>'2010-4-1 0:00:00' and r1.recommendtime<='2011-3-31 0:00:00'
left join mb_resume_recommend r2 on r2.recommenditcode=a.itcode and r2.recommendtype!=3 and r2.recommendtime>'2010-4-1 0:00:00' and r2.recommendtime<='2011-3-31 0:00:00'
left join mb_resume_recommend r3 on r3.recommenditcode=a.itcode and r3.recommendtype=3 and r3.recommendtime>'2010-4-1 0:00:00' and r3.recommendtime<='2011-3-31 0:00:00'
left join onboard b on b.itcode=a.itcode
group by a.itcode
優化後的
with temp as
(select d.fzherenitcode,p.objid release
from mb_recruitmentdoc d
inner join mb_recruitmentdoc_antifilled an on an.demandid=d.demandid and d.thisflowenddate>'2010-4-1 0:00:00' and d.thisflowenddate<='2011-3-31 0:00:00'
inner join position p on p.objid=an.stvacancypostnum
),onboard as
(select a.recommenditcode itcode, c.id onnum from mb_resume_recommend a
inner join mb_recommend_resume b on a.recommendid=b.recommendid and a.recommendtime>'2010-4-1 0:00:00' and a.recommendtime<='2011-3-31 0:00:00'
inner join mb_resume_dc_userinfo c on c.resumeid=b.resumeid
)select a.itcode,
isnull(count(t.release),0) release,
isnull(sum(m.resumeid),0) input,
isnull(sum(case when r1.recommendtype!=3 then isnull(r1.recommendcount,0) else 0 end),0) receive,
isnull(sum(case when r1.recommendtype=3 then isnull(r1.recommendcount,0) else 0 end),0) business,
isnull(sum(r2.recommendcount),0) send,
isnull(count(1),0) insum
from accounts_users a
left join temp t on t.fzherenitcode=a.itcode
left join mb_posinal m on m.sourceitcode=a.itcode and m.sourceid=2015 and m.lastmodifytime>'2010-4-1 0:00:00' and m.lastmodifytime<='2011-3-31 0:00:00'
left join mb_resume_recommend r1 on r1.recommendeditcode=a.itcode and r1.recommendtime>'2010-4-1 0:00:00' and r1.recommendtime<='2011-3-31 0:00:00'
left join mb_resume_recommend r2 on r2.recommenditcode=a.itcode and r2.recommendtype!=3 and r2.recommendtime>'2010-4-1 0:00:00' and r2.recommendtime<='2011-3-31 0:00:00'
left join onboard b on b.itcode=a.itcode
group by a.itcode
初看差不多,明顯的第2個case when 語句,
left join mb_resume_recommend r1 on r1.recommendeditcode=a.itcode and r1.recommendtype!=3 and r1.recommendtime>'2010-4-1 0:00:00' and r1.recommendtime<='2011-3-31 0:00:00'
left join mb_resume_recommend r3 on r3.recommenditcode=a.itcode and r3.recommendtype=3 and r3.recommendtime>'2010-4-1 0:00:00' and r3.recommendtime<='2011-3-31 0:00:00'
這2個邏輯相反,不用再次關聯表就可以實現了。直接用case when 語句,由於思維定勢,上去就關聯表。從最早的那個sql語句可以看出
left join mb_resume_recommend r1 on r1.recommendeditcode=a.itcode and r1.recommendtype!=3 and r1.recommendtime>'2010-4-1 0:00:00' and r1.recommendtime<='2011-3-31 0:00:00'
left join mb_resume_recommend r2 on r2.recommenditcode=a.itcode and r2.recommendtype!=3 and r2.recommendtime>'2010-4-1 0:00:00' and r2.recommendtime<='2011-3-31 0:00:00'
left join mb_resume_recommend r3 on r3.recommenditcode=a.itcode and r3.recommendtype=3 and r3.recommendtime>'2010-4-1 0:00:00' and r3.recommendtime<='2011-3-31 0:00:00'
關聯前2個都沒問題,一旦關聯上第3個連線語句,速度就超慢,仔細檢視發現。前2個語句都是type!=3,如果第3個語句type=3。那麼的話,還得去重新掃瞄這個表。應為前兩個表已經篩選過了,再重新篩選。速度自然自然很慢。
其實速度提公升的辦法很簡單,case when 也很容易。但是由於思維定勢,沒想到用case when 去解決,還是去反覆關聯。
經過改造,完全可以去掉第3個關聯。
sql優化提速整理
sql優化提速整理 場景描述 在我們實際開發中,隨著業務的不斷增加,資料量也在不斷的攀公升,這樣就離不開乙個問題 資料查詢效率優化 根據自己的以往實際專案工作經驗和學習所知,現在對sql查詢優化做乙個簡單的梳理總結,總結的不好之處,望多多指點交流學習 主要通過以下幾個點來進行總結分析 索引 語句本身...
iPhone開發札記 2011 11 15
ios架構 1.站在高階層次來看,ios扮演底層硬體和應用程式的中介。應用程式不能直接訪問硬體,而需要和系統介面進行互動。系統介面又轉而去和適當的驅動打交道。這樣的抽象可以防止應用程式改變底層硬體。2.應用程式會和底層硬體隔離,但是應用程式 仍需要考慮裝置之間的差異。3.ios的層 iphone s...
WMS開發學習札記2
response是響應物件的請求,對客戶端發出的請求作出動態的響應,向客戶端傳送資料。當伺服器處理完你的請求資訊後,將處理結果發回給客戶端。response.redirect方法的作用是實現頁面的跳轉,跳轉到你所需要的url頁面,並且可以用此方法實現不同頁面之間的值的傳遞。語法 response.r...