gpt4 book ai didi

mysql - 将 JOIN 查询重写为子查询,缺少行?

转载 作者:行者123 更新时间:2023-11-29 10:03:51 25 4
gpt4 key购买 nike

我有以下返回这些行的 MySQL 查询:

SELECT p.ID AS id, p.post_title AS title, p.post_author AS uid, SUM(v.pageviews) AS pageviews, IFNULL(c.comment_count, 0) AS comment_count 
FROM `views` v
LEFT JOIN `posts` p ON v.postid = p.ID
LEFT JOIN (SELECT comment_post_ID, COUNT(comment_post_ID) AS comment_count FROM `comments` WHERE comment_date_gmt > DATE_SUB('2018-08-16 18:34:46', INTERVAL 29 DAY) AND comment_approved = 1 GROUP BY comment_post_ID) c ON p.ID = c.comment_post_ID
WHERE 1 = 1 AND p.post_type IN('post') AND v.view_datetime > DATE_SUB('2018-08-16 18:34:46', INTERVAL 29 DAY) AND p.post_password = '' AND p.post_status = 'publish' GROUP BY v.postid ORDER BY pageviews DESC LIMIT 5 OFFSET 0;

JOIN query

我目前正在尝试使用子查询而不是 JOIN 来重写它,以检查性能是否更好。

以下查询返回相同的结果(comment_count 列除外,因为我们只查询两个表而不是三个):

SELECT p.ID AS id, p.post_title AS title, p.post_author AS uid, v.pageviews AS pageviews 
FROM `posts` p,
(SELECT SUM(pageviews) AS pageviews, postid FROM `views` WHERE view_datetime > DATE_SUB( '2018-08-16 18:34:46', INTERVAL 29 DAY) GROUP BY postid) v
WHERE 1 = 1 AND p.ID = v.postid AND p.post_type IN('post') AND p.post_password = '' AND p.post_status = 'publish' GROUP BY v.postid ORDER BY pageviews DESC LIMIT 5 OFFSET 0

subqueries query

但是,如果我将 comments 表添加到组合中,则没有评论的帖子将被自动排除(例如 ID 2956 具有 3100 次浏览),返回与原始查询相比,结果不同:

SELECT p.ID AS id, p.post_title AS title, p.post_author AS uid, v.pageviews AS pageviews, IFNULL(c.comment_count, 0) AS comment_count 
FROM `posts` p,
(SELECT SUM(pageviews) AS pageviews, postid FROM `views` WHERE view_datetime > DATE_SUB( '2018-08-16 18:34:46', INTERVAL 29 DAY) GROUP BY postid) v,
(SELECT COUNT(comment_post_ID) AS comment_count, comment_post_ID FROM `comments` WHERE comment_date_gmt > DATE_SUB('2018-08-16 18:34:46', INTERVAL 29 DAY) AND comment_approved = 1 GROUP BY comment_post_ID) c
WHERE 1 = 1 AND p.ID = v.postid AND v.postid = c.comment_post_ID AND p.post_type IN('post') AND p.post_password = '' AND p.post_status = 'publish' GROUP BY v.postid ORDER BY pageviews DESC LIMIT 5 OFFSET 0

subqueries query with comments table

我对这些东西还没有那么有经验(正如你所知),所以任何帮助/提示来了解正在发生的事情将非常感激。

最佳答案

正如我在评论中提到的,您的新查询仍然有 JOIN。如果您想使用子查询测试性能,您需要将查询更改为如下所示(这是我根据您原始工作查询的最佳猜测;如果没有看到表结构和数据,很难 100% 确定):

SELECT id, post_title AS title, post_author AS uid, 
(SELECT SUM(pageviews)
FROM views v
WHERE v.postid = posts.id AND
v.view_datetime > DATE_SUB('2018-08-16 18:34:46', INTERVAL 29 DAY)
) AS pageviews,
(SELECT COUNT(comment_post_ID)
FROM comments c
WHERE c.comment_post_ID = posts.id AND
c.comment_date_gmt > DATE_SUB('2018-08-16 18:34:46', INTERVAL 29 DAY) AND
c.comment_approved = 1
) AS comment_count
FROM posts
WHERE p.post_type IN('post') AND p.post_password = '' AND p.post_status = 'publish'
ORDER BY pageviews DESC
LIMIT 5 OFFSET 0;

关于mysql - 将 JOIN 查询重写为子查询,缺少行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359567/

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