gpt4 book ai didi

mysql - 如何使我的 SQL 查询在下面的 sql 代码上更快?

转载 作者:行者123 更新时间:2023-11-29 01:50:19 26 4
gpt4 key购买 nike

我需要使下面的查询更快。我使用了多个选择并使我的应用程序非常慢。请帮助我修复它。

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

结果:(结果正确但速度太慢)

ScreenShot of result

最佳答案

使用条件聚合:

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

关于mysql - 如何使我的 SQL 查询在下面的 sql 代码上更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46965246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com