gpt4 book ai didi

mysql - 使用计数(不同)查询运行选择时如何加快 mysql 查询

转载 作者:行者123 更新时间:2023-11-29 00:52:03 26 4
gpt4 key购买 nike

有人可以告诉我如何使用加速这个 mysql 查询吗?我知道由于 count(distinct subcontractorRef) 语法,它目前运行缓慢。还有其他选择吗?

SELECT DISTINCT (contractorsRef) AS cref, RIDGROUP AS ridg,  
(select count(DISTINCT subcontractorRef)
FROM request
INNER JOIN request_config ON request_config.RIDGROUP = request.RIDGROUP
WHERE request_config.contractorsRef = outer_config.contractorsRef
AND currenttaxyear =2011
AND weekno =31) AS xxx
FROM request_config outer_config
WHERE currenttaxyear =2011
AND weekno =32
AND contractorsRef <>132

最佳答案

尝试使用 JOIN 而不是子查询:

SELECT
contractorsRef AS cref,
RIDGROUP AS ridg,
IFNULL(T1.subcontractorCount, 0) AS xxx
FROM request_config AS outer_config
LEFT JOIN
(
SELECT
request_config.contractorsRef,
COUNT(DISTINCT subcontractorRef) AS subcontractorCount
FROM request
INNER JOIN request_config
ON request_config.RIDGROUP = request.RIDGROUP
AND currenttaxyear = 2011
AND weekno = 31
GROUP BY contractorsRef
) T1
ON T1.contractorsRef = outer_config.contractorsRef
WHERE currenttaxyear = 2011
AND weekno = 32
AND contractorsRef <> 132
GROUP BY outer_config.contractorsRef

关于mysql - 使用计数(不同)查询运行选择时如何加快 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106981/

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