[英]MySQL GROUP BY optimisation
誰能告訴我如何加快mysql group by子句? 我已經閱讀了文檔,但沒有給出任何好的示例。
UPDATE SQL
SELECT
post.topic_id,
topic.topic_posts,
topic.topic_title,
topic.topic_poster_name,
topic.topic_last_post_id,
forum.forum_name AS group_name,
`group`.slug AS child_slug,
`parent`.slug AS parent_slug
FROM bb_posts post
LEFT JOIN bb_topics topic
ON topic.topic_id = post.topic_id
LEFT JOIN bb_forums forum
ON forum.forum_id = topic.forum_id
LEFT JOIN wp_bp_groups `group`
ON topic.forum_id = `group`.id
LEFT JOIN wp_bp_groups `parent`
ON `group`.parent_id = `parent`.id
WHERE (topic_title LIKE '%$search_terms%' || MATCH(post.post_text) AGAINST('$search_terms'))
&& topic_status = 0
GROUP BY topic_id
ORDER BY topic.topic_start_time DESC
LIMIT $offset,$num
一般的最佳做法是確保您要分組的字段具有索引。
滿足GROUP BY子句的最通用方法是掃描整個表並創建一個新的臨時表,其中每個組中的所有行都是連續的,然后使用該臨時表發現組並應用聚合函數(如果有)。 在某些情況下,MySQL可以做得更好,並且可以避免使用索引訪問來創建臨時表。
http://dev.mysql.com/doc/refman/5.0/en/group-by-optimization.html
當分組的列上有索引時,Group by最快,並且:
使where子句成為連接條件的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.