简体   繁体   中英

MySQL GROUP BY optimisation

Can anyone tell me how I can speed up mysql group by clause? 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). In some cases, MySQL is able to do much better than that and to avoid creation of temporary tables by using index access.

  • Make sure that every foreign key has a corresponding index.
  • Create covering indexes on the fields you retrieve
  • Creating an index on the field you are sorting bij wouldn't hurt either.

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:

  • The query is over a single table.
  • The GROUP BY names only columns that form a leftmost prefix of the index and no other columns. (If, instead of GROUP BY, the query has a DISTINCT clause, all distinct attributes refer to columns that form a leftmost prefix of the index.) For example, if a table t1 has an index on (c1,c2,c3), loose index scan is applicable if the query has GROUP BY c1, c2,. It is not applicable if the query has GROUP BY c2, c3 (the columns are not a leftmost prefix) or GROUP BY c1, c2, c4 (c4 is not in the index).
  • The only aggregate functions used in the select list (if any) are MIN() and MAX(), and all of them refer to the same column. The column must be in the index and must follow the columns in the GROUP BY.
  • Any other parts of the index than those from the GROUP BY referenced in the query must be constants (that is, they must be referenced in equalities with constants), except for the argument of MIN() or MAX() functions.
  • For columns in the index, full column values must be indexed, not just a prefix. For example, with c1 VARCHAR(20), INDEX (c1(10)), the index cannot be used for loose index scan.

使where子句成为连接条件的一部分。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM