[英]MySQL GROUP BY optimisation
Can anyone tell me how I can speed up mysql group by clause? 谁能告诉我如何加快mysql group by子句? Ive read the documentation but it doesnt give any good examples.
我已经阅读了文档,但没有给出任何好的示例。
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
The general best practice would be to make sure the field you are grouping on has an index. 一般的最佳做法是确保您要分组的字段具有索引。
The most general way to satisfy a GROUP BY clause is to scan the whole table and create a new temporary table where all rows from each group are consecutive, and then use this temporary table to discover groups and apply aggregate functions (if any).
满足GROUP BY子句的最通用方法是扫描整个表并创建一个新的临时表,其中每个组中的所有行都是连续的,然后使用该临时表发现组并应用聚合函数(如果有)。 In some cases, MySQL is able to do much better than that and to avoid creation of temporary tables by using index access.
在某些情况下,MySQL可以做得更好,并且可以避免使用索引访问来创建临时表。
http://dev.mysql.com/doc/refman/5.0/en/group-by-optimization.html http://dev.mysql.com/doc/refman/5.0/en/group-by-optimization.html
Group by is fastest when you have an index on the column being grouped on, and: 当分组的列上有索引时,Group by最快,并且:
使where子句成为连接条件的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.