Summary
Query materializes 1 temp tableLearn → and sorts the result; examines ~3,000 rows to return 5 in 3.6 ms. Main finding: the sort will likely spill to disk because sort_buffer_size is too small.
- Total time
- 3.55 ms
- Rows returned
- 5
- Rows examined
- 3.00K
- Operators
- 6
Primary recommendation
Check internal_tmp_mem_storage_engine and temporary-tableLearn → disk usage before raising tmp_table_size.
Why does this help?
the default TempTable engine limits each in-memory internal temporary tableLearn → by tmp_table_size, and max_heap_table_size does not apply to it; only the MEMORY engine also uses max_heap_table_size, taking the smaller of the two. A temporary tableLearn → in the plan does not by itself show that it spilled to disk.
SQL
SELECT t.country, t.cnt
FROM (
SELECT country, COUNT(*) cnt
FROM users
WHERE status='active'
GROUP BY country) t
ORDER BY t.cnt DESC
Execution plan (flamegraph)
Click a plan operator to inspect details.
Warnings (4)
-
WARN
1 temp tableLearn →(s) (Materialize) — up to 5 rows; may spill to disk
Labeled:
Materialize -
WARN
1 sort operationLearn →(s) — 5 rows; may use disk-based filesortLearn →
Labeled:
Sort -
WARN
FilesortLearn → detected with sort_buffer_size=256.0 KB. Whether it spills depends on the sort's row count and record size.
-
WARN
Query materializes 1 temp tableLearn →(s); tmp_table_size is 16.0 MB. This limit alone does not show whether a table spilled to disk.
Suggestions (4)
-
MEDIUM
Check internal_tmp_mem_storage_engine and temporary-tableLearn → disk usage before raising tmp_table_size.
Why?
the default TempTable engine limits each in-memory internal temporary tableLearn → by tmp_table_size, and max_heap_table_size does not apply to it; only the MEMORY engine also uses max_heap_table_size, taking the smaller of the two. A temporary tableLearn → in the plan does not by itself show that it spilled to disk.
-
MEDIUM
Increase sort_buffer_size or add an ordered index to avoid filesortLearn →
-
MEDIUM
Check sort merge passes and query timing before increasing sort_buffer_size for this session.
Why?
when sort records exceed the buffer, MySQL can write sorted runs and merge them. A larger buffer may avoid that work; small sorts may already fit in memory. Test session-level changes and account for concurrent sorting sessions before changing the global default.
-
MEDIUM
Check the internal temporary-tableLearn → engine and disk usage before increasing tmp_table_size for this session.
Why?
MySQL 8.0.28+ TempTable uses tmp_table_size as its per-table limit; max_heap_table_size does not limit TempTable. Shared TempTable RAM and mmap limits also affect disk usage. If the internal engine is MEMORY, the smaller of tmp_table_size and max_heap_table_size applies instead.
Agent-ready · token savings
Feeding this plan to an AI as the myflames digest instead of raw EXPLAIN JSON cuts the prompt by 1.8× (43.1% fewer tokens).
- Raw plan
- 1,057 tokens
- myflames digest
- 601 tokens
- Saved
- 456 tokens
- Per 1,000 queries
- ~$1.37 saved
Estimate (heuristic estimate (offline)). Prices as of 2026-05-26 (Sonnet 4.6 input). Generate the digest with myflames digest.
Collected environment
Session variables
| version | 8.4.11 |
|---|---|
| innodb_buffer_pool_size | 128.0 MB |
| sort_buffer_size | 256.0 KB |
| join_buffer_size | 256.0 KB |
| tmp_table_size | 16.0 MB |
| max_heap_table_size | 16.0 MB |
| innodb_flush_log_at_trx_commit | 1 |
| optimizer_switch | 27 flags (click to expand)index_merge=onindex_merge_union=onindex_merge_sort_union=onindex_merge_intersection=onengine_condition_pushdown=onindex_condition_pushdown=onmrr=onmrr_cost_based=onblock_nested_loop=onbatched_key_access=offmaterialization=onsemijoin=onloosescan=onfirstmatch=onduplicateweedout=onsubquery_materialization_cost_based=onuse_index_extensions=oncondition_fanout_filter=onderived_merge=onuse_invisible_indexes=offskip_scan=onhash_join=onsubquery_to_derived=offprefer_ordering_index=onhypergraph_optimizer=offderived_condition_pushdown=onhash_set_operations=on |
Tables (click a name to expand)
demodb.users
Columns (5)
idintnamevarchar(80)countryvarchar(2)statusvarchar(20)created_atdatetime
Indexes (2)
- INDEX
(unnamed)(id) - INDEX
idx_country(country)
myteach — interactive algorithm catalog
Every operator shown above has a hands-on lesson with a cost-model slider and an animated walk-through. The catalog is the central index — bookmark it once, revisit per query:
Lessons relevant to this plan
Raw sidecar (JSON)
{
"$schema": "https://myflames.dev/schemas/sidecar-v1.json",
"schema_version": "1.3",
"generated_at": "2026-10-15T19:55:05Z",
"myflames_version": "2.3.0",
"source": {
"type": "live",
"engine": "mysql",
"engine_version": "8.4.11"
},
"plan_summary": {
"total_time_ms": 3.553,
"rows_sent": 5,
"rows_examined_estimate": 3000,
"operator_count": 6,
"max_depth": 6
},
"optimizer_switches": [
{
"name": "materialization",
"value": "on",
"explanation": "A subquery or derived table is executed once and its result stored in a temporary table for reuse. It may spill to disk past tmp_table_size (MySQL's TempTable engine) or the smaller of tmp_table_size and max_heap_table_size (MEMORY engine, MariaDB).",
"node_labels": [
"Materialize"
]
}
],
"warnings": [
{
"severity": "warn",
"category": "temp_table",
"text": "1 temp table(s) (Materialize) — up to 5 rows; may spill to disk",
"source": "plan",
"node_labels": [
"Materialize"
]
},
{
"severity": "warn",
"category": "filesort",
"text": "1 sort operation(s) — 5 rows; may use disk-based filesort",
"source": "plan",
"node_labels": [
"Sort"
]
},
{
"severity": "warn",
"category": "env",
"text": "Filesort detected with sort_buffer_size=256.0 KB. Whether it spills depends on the sort's row count and record size.",
"source": "environment"
},
{
"severity": "warn",
"category": "env",
"text": "Query materializes 1 temp table(s); tmp_table_size is 16.0 MB. This limit alone does not show whether a table spilled to disk.",
"source": "environment"
}
],
"suggestions": [
{
"severity": "medium",
"category": "tuning_variable",
"action": "Check internal_tmp_mem_storage_engine and temporary-table disk usage before raising tmp_table_size.",
"source": "plan",
"why": "the default TempTable engine limits each in-memory internal temporary table by tmp_table_size, and max_heap_table_size does not apply to it; only the MEMORY engine also uses max_heap_table_size, taking the smaller of the two. A temporary table in the plan does not by itself show that it spilled to disk.",
"target_variable": "tmp_table_size"
},
{
"severity": "medium",
"category": "tuning_variable",
"action": "Increase sort_buffer_size or add an ordered index to avoid filesort",
"source": "plan",
"target_variable": "sort_buffer_size"
},
{
"severity": "medium",
"category": "tuning_variable",
"action": "Check sort merge passes and query timing before increasing sort_buffer_size for this session.",
"source": "environment",
"why": "when sort records exceed the buffer, MySQL can write sorted runs and merge them. A larger buffer may avoid that work; small sorts may already fit in memory. Test session-level changes and account for concurrent sorting sessions before changing the global default.",
"target_variable": "sort_buffer_size"
},
{
"severity": "medium",
"category": "tuning_variable",
"action": "Check the internal temporary-table engine and disk usage before increasing tmp_table_size for this session.",
"source": "environment",
"why": "MySQL 8.0.28+ TempTable uses tmp_table_size as its per-table limit; max_heap_table_size does not limit TempTable. Shared TempTable RAM and mmap limits also affect disk usage. If the internal engine is MEMORY, the smaller of tmp_table_size and max_heap_table_size applies instead.",
"target_variable": "tmp_table_size"
}
],
"executive_summary": "Query materializes 1 temp table and sorts the result; examines ~3,000 rows to return 5 in 3.6 ms. Main finding: the sort will likely spill to disk because sort_buffer_size is too small.",
"plan_tree": {
"node_id": "n:c1019a6ed56c",
"short_label": "Sort",
"folded_label": "SORT starts=1 rows=5",
"children": [
{
"node_id": "n:beb14d906666",
"short_label": "Table scan [t]",
"folded_label": "TABLE SCAN [t] starts=1 rows=5",
"children": [
{
"node_id": "n:a1d4f99ec950",
"short_label": "Materialize",
"folded_label": "MATERIALIZE starts=1 rows=5",
"children": [
{
"node_id": "n:f3a4ee7934b5",
"short_label": "Group",
"folded_label": "GROUP starts=1 rows=5",
"children": [
{
"node_id": "n:83cc7ae0fbe7",
"short_label": "Filter: ((users.status = 'active'))",
"folded_label": "FILTER ((users.status = 'active')) starts=1 rows=2572",
"children": [
{
"node_id": "n:18bb13cfb7fc",
"short_label": "Index scan [users.idx_country]",
"folded_label": "INDEX SCAN [users.idx_country] starts=1 rows=3000",
"children": []
}
]
}
]
}
]
}
]
}
]
},
"query": {
"raw": "SELECT t.country, t.cnt FROM (SELECT country, COUNT(*) cnt FROM users WHERE status='active' GROUP BY country) t ORDER BY t.cnt DESC",
"beautified": "SELECT t.country, t.cnt\nFROM (\nSELECT country, COUNT(*) cnt\nFROM users\nWHERE status='active'\nGROUP BY country) t\nORDER BY t.cnt DESC"
},
"primary_action": {
"ref": "suggestions[0]"
},
"collected": {
"variables": {
"bulk_insert_buffer_size": "8388608",
"character_set_connection": "utf8mb4",
"character_set_server": "utf8mb4",
"collation_connection": "utf8mb4_0900_ai_ci",
"collation_server": "utf8mb4_0900_ai_ci",
"eq_range_index_dive_limit": "200",
"innodb_buffer_pool_instances": "1",
"innodb_buffer_pool_size": "134217728",
"innodb_file_per_table": "ON",
"innodb_flush_log_at_trx_commit": "1",
"innodb_flush_method": "O_DIRECT",
"innodb_io_capacity": "10000",
"innodb_log_buffer_size": "67108864",
"innodb_log_file_size": "50331648",
"internal_tmp_mem_storage_engine": "TempTable",
"join_buffer_size": "262144",
"max_allowed_packet": "67108864",
"max_connections": "151",
"max_heap_table_size": "16777216",
"optimizer_prune_level": "1",
"optimizer_search_depth": "62",
"optimizer_switch": "index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,engine_condition_pushdown=on,index_condition_pushdown=on,mrr=on,mrr_cost_based=on,block_nested_loop=on,batched_key_access=off,materialization=on,semijoin=on,loosescan=on,firstmatch=on,duplicateweedout=on,subquery_materialization_cost_based=on,use_index_extensions=on,condition_fanout_filter=on,derived_merge=on,use_invisible_indexes=off,skip_scan=on,hash_join=on,subquery_to_derived=off,prefer_ordering_index=on,hypergraph_optimizer=off,derived_condition_pushdown=on,hash_set_operations=on",
"range_optimizer_max_mem_size": "8388608",
"read_buffer_size": "131072",
"read_rnd_buffer_size": "262144",
"sort_buffer_size": "262144",
"table_open_cache": "4000",
"thread_cache_size": "9",
"tmp_table_size": "16777216",
"version": "8.4.11",
"version_comment": "MySQL Community Server - GPL",
"version_compile_os": "Linux"
},
"stats": {
"demodb.users": {
"table_schema": "demodb",
"table_name": "users",
"table_rows": 3000,
"data_length": 180224,
"index_length": 81920,
"data_free": 0,
"auto_increment": 4096,
"engine": "InnoDB",
"row_format": "Dynamic"
}
},
"schema": {
"demodb.users": {
"ddl": "CREATE TABLE `users` (\n `id` int NOT NULL AUTO_INCREMENT,\n `name` varchar(80) DEFAULT NULL,\n `country` varchar(2) DEFAULT NULL,\n `status` varchar(20) DEFAULT NULL,\n `created_at` datetime DEFAULT NULL,\n PRIMARY KEY (`id`),\n KEY `idx_country` (`country`)\n) ENGINE=InnoDB AUTO_INCREMENT=4096 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
"table_name": "users",
"columns": [
{
"name": "id",
"type": "int",
"rest": "NOT NULL AUTO_INCREMENT"
},
{
"name": "name",
"type": "varchar(80)",
"rest": "DEFAULT NULL"
},
{
"name": "country",
"type": "varchar(2)",
"rest": "DEFAULT NULL"
},
{
"name": "status",
"type": "varchar(20)",
"rest": "DEFAULT NULL"
},
{
"name": "created_at",
"type": "datetime",
"rest": "DEFAULT NULL"
}
],
"indexes": [
{
"kind": "PRIMARY KEY",
"name": null,
"columns": [
"id"
],
"parts": [
{
"name": "id",
"length": null,
"desc": false,
"expression": false
}
],
"using": null
},
{
"kind": "KEY",
"name": "idx_country",
"columns": [
"country"
],
"parts": [
{
"name": "country",
"length": null,
"desc": false,
"expression": false
}
],
"using": null
}
],
"engine": "InnoDB",
"charset": "utf8mb4"
}
}
},
"teach_hooks": [
{
"lesson": "filesort",
"match": {
"folded_label": "SORT starts=1 rows=5",
"short_label": "Sort"
},
"controls": {
"rows": 5,
"row_size": 200,
"sbs": 262144
},
"note": "Sort: t.cnt DESC",
"query_sql": "SELECT t.country, t.cnt FROM (SELECT country, COUNT(*) cnt FROM users WHERE status='active' GROUP BY country) t ORDER BY t.cnt DESC"
},
{
"lesson": "full_scan",
"match": {
"folded_label": "TABLE SCAN [t] starts=1 rows=5",
"short_label": "Table scan [t]"
},
"controls": {
"rows": 5,
"row_size": 200,
"selectivity": 100.0
},
"note": "Table scan on t (t)",
"query_sql": "SELECT t.country, t.cnt FROM (SELECT country, COUNT(*) cnt FROM users WHERE status='active' GROUP BY country) t ORDER BY t.cnt DESC"
},
{
"lesson": "tmp",
"match": {
"folded_label": "MATERIALIZE starts=1 rows=5",
"short_label": "Materialize"
},
"controls": {
"rows": 5,
"row_size": 200,
"tmp_size": 16777216,
"max_heap": 16777216,
"engine": "temptable"
},
"note": "Materialize",
"query_sql": "SELECT t.country, t.cnt FROM (SELECT country, COUNT(*) cnt FROM users WHERE status='active' GROUP BY country) t ORDER BY t.cnt DESC"
},
{
"lesson": "filter",
"match": {
"folded_label": "FILTER ((users.status = 'active')) starts=1 rows=2572",
"short_label": "Filter: ((users.status = 'active'))"
},
"controls": {
"input_rows": 2572,
"selectivity": 10.0
},
"note": "Filter: (users.`status` = 'active')",
"query_sql": "SELECT t.country, t.cnt FROM (SELECT country, COUNT(*) cnt FROM users WHERE status='active' GROUP BY country) t ORDER BY t.cnt DESC"
},
{
"lesson": "btree",
"match": {
"folded_label": "INDEX SCAN [users.idx_country] starts=1 rows=3000",
"short_label": "Index scan [users.idx_country]"
},
"controls": {
"rows": 3000,
"key_size": 16,
"page_size": 16384,
"key_type": "secondary_noncovering"
},
"note": "Index scan on users using idx_country (users)",
"query_sql": "SELECT t.country, t.cnt FROM (SELECT country, COUNT(*) cnt FROM users WHERE status='active' GROUP BY country) t ORDER BY t.cnt DESC"
}
],
"operator_complexities": [
{
"node_id": "n:c1019a6ed56c",
"folded_label": "SORT starts=1 rows=5",
"short_label": "Sort",
"complexity": {
"big_o": "O(n log n)",
"short": "n log n",
"severity": "medium",
"rationale": "Filesort: MySQL sorts the output in the server layer. Priority-queue filesort (LIMIT) is not detected today — we report the upper bound.",
"confidence": "worst_case",
"learn_more": "filesort"
}
},
{
"node_id": "n:beb14d906666",
"folded_label": "TABLE SCAN [t] starts=1 rows=5",
"short_label": "Table scan [t]",
"complexity": {
"big_o": "O(n)",
"short": "n",
"severity": "medium",
"rationale": "Full table scan: the storage engine returns every row; cost scales with the table size.",
"confidence": "exact",
"learn_more": "full_table_scan"
}
},
{
"node_id": "n:a1d4f99ec950",
"folded_label": "MATERIALIZE starts=1 rows=5",
"short_label": "Materialize",
"complexity": {
"big_o": "build: O(n) • scan: O(1) per probe, O(rows) to fetch all",
"short": "build+scan",
"severity": "good",
"rationale": "Materialize is a two-phase operator. The build pays the inner subquery's cost ONCE; each scan of the temp table is effectively constant time. Don't compound build cost by loops.",
"confidence": "exact",
"learn_more": "materialization",
"build_complexity": {
"big_o": "O(n)",
"short": "n",
"severity": "good",
"rationale": "Streaming aggregation: reads each input row once and updates the aggregate values. Any input sorting is a separate child operator with its own cost.",
"confidence": "exact"
},
"scan_complexity": {
"big_o": "O(1) per probe, O(rows) to fetch all",
"short": "O(1)/probe",
"severity": "good",
"rationale": "Materialize scan: reads the pre-computed temp table. Each probe is constant time once the table is in memory.",
"confidence": "typical",
"learn_more": "materialization"
}
}
},
{
"node_id": "n:f3a4ee7934b5",
"folded_label": "GROUP starts=1 rows=5",
"short_label": "Group",
"complexity": {
"big_o": "O(n)",
"short": "n",
"severity": "good",
"rationale": "Streaming aggregation: reads each input row once and updates the aggregate values. Any input sorting is a separate child operator with its own cost.",
"confidence": "exact"
}
},
{
"node_id": "n:18bb13cfb7fc",
"folded_label": "INDEX SCAN [users.idx_country] starts=1 rows=3000",
"short_label": "Index scan [users.idx_country]",
"complexity": {
"big_o": "O(n)",
"short": "n",
"severity": "medium",
"rationale": "Index scan: reads every entry of the index in key order. Cheaper than a table scan because the index is smaller, but still O(n).",
"confidence": "exact",
"learn_more": "index_scan"
}
}
]
}