簡體   English   中英

如何在以下sql代碼上更快地執行SQ​​L查詢?

[英]How can I make my SQL query faster on below sql code?

我需要使下面的查詢更快。 我使用了幾種選擇,使我的應用程序運行非常緩慢。 請幫助我修復它。

select
Branch,
(select count(*) from TCB T1 where Standard like "%FCC%" and T0.Branch=T1.Branch) as FCC,
(select count(*) from TCB T2 where (Standard like "%ISEDC%" or Standard like "%RSS%") and T0.Branch=T2.Branch) as ISEDC,
(select count(*) from TCB T3 where (Standard like "%RED%" or Standard like "%EN%") and T0.Branch=T3.Branch) as RED,
(select count(*) from TCB T4 where Standard like "%MIC%" and T0.Branch=T4.Branch) as MICJapan,
(select count(*) from TCB T5 where Standard like "%IMDA%" and T0.Branch=T5.Branch) as IMDA,
(select count(*) from TCB T6 where Standard like "%ACTA%" and T0.Branch=T6.Branch) as ACTA,
(select count(*) from TCB T7 where Standard like "%CS03%" and T0.Branch=T7.Branch) as CS03,
((select count(*) from TCB T1 where Standard like "%FCC%" and T0.Branch=T1.Branch)+
(select count(*) from TCB T2 where (Standard like "%ISEDC%" or Standard like "%RSS%") and T0.Branch=T2.Branch)+
(select count(*) from TCB T3 where (Standard like "%RED%" or Standard like "%EN%") and T0.Branch=T3.Branch)+
(select count(*) from TCB T4 where Standard like "%MIC%" and T0.Branch=T4.Branch)+
(select count(*) from TCB T5 where Standard like "%IMDA%" and T0.Branch=T5.Branch)+
(select count(*) from TCB T6 where Standard like "%ACTA%" and T0.Branch=T6.Branch)+
(select count(*) from TCB T7 where Standard like "%CS03%" and T0.Branch=T7.Branch)
) as Total

from
TCB T0
group by Branch

結果:(結果正確,但是速度太慢)

屏幕截圖

使用條件聚合:

select Branch,
       sum(Standard like '%FCC%') as FCC,
       sum(Standard like '%ISEDC%' or Standard like '%RSS%') as ISEDC,
       . . .
from TCB 
group by Branch

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM