Why MySQL Uses Temporary Tables for GROUP BY and How to Optimize It
When MySQL's query optimizer cannot use a suitable index for a GROUP BY, it reads the needed rows, creates a temporary table, and performs sorting, which can degrade performance unless sort_buffer_size is properly configured and large result sets are avoided.
When the MySQL query optimizer cannot find a suitable index to use, it must first read the required rows and then use a temporary table to complete the GROUP BY operation.
Example:
EXPLAIN SELECT max(gmt_create) FROM group_message WHERE group_id > 1 and group_id < 10 GROUP BY user_id \G
********** 1. row *********
id: 1
select_type: SIMPLE
table: group_message
type: range
possible_keys: idx_group_message_gid_uid,idx_gid_uid_gc
key: idx_gid_uid_gc
key_len: 4
ref: NULL
rows: 32
Extra: Using where; Using index; Using temporary; Using filesortThe execution plan shows that MySQL used an index to locate the rows, then created a temporary table and performed a filesort to obtain the GROUP BY result.
In the example, group_id is a range condition rather than a constant, and the GROUP BY column is user_id. Because the index order cannot assist the grouping, MySQL first scans the index range, stores the rows in a temporary table, and finally sorts and groups them.
To optimize this situation, ensure that sort_buffer_size is large enough for the sort operation, and try to avoid large result‑set GROUP BY queries. If the temporary table exceeds the configured size, it will be written to disk, causing a dramatic performance drop.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
