簡體   English   中英

子選擇還是加入? 有沒有更好的方法來編寫此mysql查詢?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM