gpt4 book ai didi

mysql - 特定mysql查询的性能优化?

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

如何在保持相同结果的同时优化以下查询的性能?

SELECT
a.country,
count(DISTINCT b.`hash`)
FROM
audience a,
behaviour b
WHERE
DATE(b.`timestamp`) = '2016-03-31'
AND b.`hash` = a.`hash`
GROUP BY
a.country;

最佳答案

WHERE 子句中使用的所有列上添加索引,并使用 BETWEEN...AND 作为时间戳,而不是 DATE() 函数:

ALTER TABLE `behaviour` 
ADD INDEX `ts_IDX` (`timestamp`),
ADD INDEX `hash_IDX` (`hash`);
---------------------------------
ALTER TABLE `audience`
ADD INDEX `hash_IDX` (`hash`);
---------------------------------
SELECT
a.country,
COUNT(DISTINCT b.`hash`)
FROM
audience a
INNER JOIN behaviour b ON b.`hash` = a.`hash`
WHERE
b.`timestamp` BETWEEN '2016-03-31 00:00:00' AND '2016-03-31 23:59:59'
GROUP BY
a.country;
---------------------------------

这应该有帮助。

关于mysql - 特定mysql查询的性能优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334046/

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