gpt4 book ai didi

mysql - 如何优化count和group by子查询操作

转载 作者:行者123 更新时间:2023-11-29 13:37:05 24 4
gpt4 key购买 nike

我的主机说以下查询占用了大量服务器 CPU。请告诉我如何优化它。

SELECT COUNT(*) FROM (SELECT COUNT(*) AS tot,wallpapers.*,resolutions.res_height,resolutions.res_width FROM wallpapers

INNER JOIN analytics ON analytics.`wall_id` = wallpapers.`wall_id`

INNER JOIN resolutions ON resolutions.`res_id` = wallpapers.`res_id`



WHERE analytics.ana_date >= '2013-09-01 16:36:56' AND wallpapers.wall_status = 'public'

GROUP BY analytics.`wall_id`) as Q

请注意,分析表包含所有浏览量和点击量的记录。所以它非常非常大。

最佳答案

据我所知,您的查询只是在通过联接和 WHERE 子句进行过滤后计算不同的 wall_id 值。像这样的东西应该很接近:

SELECT COUNT(DISTINCT analytics.wall_id)
FROM wallpapers
INNER JOIN analytics ON analytics.wall_id = wallpapers.wall_id
INNER JOIN resolutions ON resolutions.res_id = wallpapers.res_id
WHERE analytics.ana_date >= '2013-09-01 16:36:56'
AND wallpapers.wall_status = 'public'

关于mysql - 如何优化count和group by子查询操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596036/

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