gpt4 book ai didi

mysql - 子选择还是加入?有没有更好的方法来编写这个 mysql 查询?

转载 作者:行者123 更新时间:2023-11-29 00:39:35 25 4
gpt4 key购买 nike

我知道做某事总是有更好的方法,但我不确定该怎么做?优化此查询的最佳方法是什么?我应该使用联接、单独的查询等吗?我知道这不是一个复杂的查询……只是想扩展我的知识。

如有任何建议,我们将不胜感激!

SELECT
community_threads.id AS thread_id,
community_threads.title AS thread_title,
community_threads.date AS thread_date,
community_threads.author_id AS author_id,
`user`.display_name AS author_name,
`user`.organization AS author_organization,
(SELECT date FROM community_replies replies WHERE replies.thread_id = community_threads.id ORDER BY date DESC LIMIT 1) AS reply_date,
(SELECT count(id) FROM community_replies replies WHERE replies.thread_id = community_threads.id ORDER BY date DESC LIMIT 1) AS total_replies
FROM
community_threads
INNER JOIN `user` ON community_threads.author_id = `user`.id
WHERE
category_id = '1'
ORDER BY
reply_date DESC
LIMIT 0, 5

最佳答案

这可以通过针对子选择的 JOIN 进行改进,子选择获取每个 thread_id 的聚合 COUNT() 和聚合 MAX (日期)。派生表不应为每一行评估子选择,而应仅对整个查询评估一次,并与 community_threads 中的其余行连接。

SELECT
community_threads.id AS thread_id,
community_threads.title AS thread_title,
community_threads.date AS thread_date,
community_threads.author_id AS author_id,
`user`.display_name AS author_name,
`user`.organization AS author_organization,
/* From the joined subqueries */
maxdate.date AS reply_date,
threadcount.num AS total_replies
FROM
community_threads
INNER JOIN `user` ON community_threads.author_id = `user`.id
/* JOIN against subqueries to return MAX(date) (same as order by date DESC limit 1) and COUNT(*) from replies */
/* number of replies per thread_id */
INNER JOIN (
SELECT thread_id, COUNT(*) AS num FROM replies GROUP BY thread_id
) threadcount ON community_threads.id = threadcount.thread_id
/* Most recent date per thread_id */
INNER JOIN (
SELECT thread_id, MAX(date) AS date FROM replies GROUP BY thread_id
) maxdate ON community_threads.id = maxdate.thread_id
WHERE
category_id = '1'
ORDER BY
reply_date DESC
LIMIT 0, 5

如果将 LIMIT 0, 5 放在 reply_date 子查询中,您可能会获得更好的性能。这只会在子查询中提取最近的 5 个,INNER JOIN 将丢弃所有来自 community_threads 的不匹配项。

/* I *think* this will work...*/
SELECT
community_threads.id AS thread_id,
community_threads.title AS thread_title,
community_threads.date AS thread_date,
community_threads.author_id AS author_id,
`user`.display_name AS author_name,
`user`.organization AS author_organization,
/* From the joined subqueries */
maxdate.date AS reply_date,
threadcount.num AS total_replies
FROM
community_threads
INNER JOIN `user` ON community_threads.author_id = `user`.id
INNER JOIN (
SELECT thread_id, COUNT(*) AS num FROM replies GROUP BY thread_id
) threadcount ON community_threads.id = threadcount.thread_id
/* LIMIT in this subquery */
INNER JOIN (
SELECT thread_id, MAX(date) AS date FROM replies GROUP BY thread_id ORDER BY date DESC LIMIT 0, 5
) maxdate ON community_threads.id = maxdate.thread_id
WHERE
category_id = '1'
ORDER BY
reply_date DESC

关于mysql - 子选择还是加入?有没有更好的方法来编写这个 mysql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847116/

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