[英]SubSelect or Joins? Is there a better way to write this mysql query?
我知道总有更好的方法来做某事,但我不确定如何做? 优化此查询的最佳方法是什么? 我应该使用Joins还是单独的查询等?我知道这不是一个复杂的查询。
任何建议,将不胜感激!
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(date)
。 而不是为每一行评估子选择,派生表对于整个查询应仅评估一次,并与来自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
子查询中LIMIT 0, 5
则可能会获得更好的性能。 这只会在子查询中提取最近的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
据我所知,这似乎是一个很好的机会。
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,
MAX(replies.date) AS reply_date,
COUNT(replies.id) AS total_replies
FROM community_threads
INNER JOIN `user` ON community_threads.author_id = `user`.id
INNER JOIN community_replies AS replies ON replies.thread_id = community_threads.id
WHERE category_id = 1
GROUP BY thread_id, thread_title, thread_date, author_id, author_name, author_organization)
ORDER BY reply_date DESC
LIMIT 0, 5
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.