An index doesn’t make a query faster in the abstract — it changes the plan MySQL chooses, and EXPLAIN ANALYZE proves it. On a 500,000-row table, filtering on an unindexed column costs 50000 and takes ~100ms; the same filter after an index costs 875 and takes ~3ms. Below is that comparison unedited, plus a note for anyone running MySQL 8.0 or later: the query cache no longer exists.
Table of Contents
The query cache was removed in MySQL 8.0
Is it worth using query caching in MySQL? The feature no longer exists. From MySQL’s own manual, in its list of features removed in MySQL 8.0: “The query cache was removed.” Checked against the live 8.4.10 server used for every other command on this page:
mysql> SHOW VARIABLES LIKE 'query_cache%';
Empty set (0.00 sec)
Not disabled — gone. There are no query_cache_* variables to set at all on 8.0+. If you need statement-result caching now, that’s an application layer concern (Redis, an ORM cache, etc.), not a MySQL server setting.
The benchmark: before and after an index, on 500,000 rows
Setup: products, 500,000 rows, category_id with 200 distinct values (~2,500 rows per value), no index on category_id to start:
EXPLAIN SELECT id, sku, price, name FROM products WHERE category_id = 42;
+----+-------------+----------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | products | ALL | NULL | NULL | NULL | NULL | 499999 | 10.00 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+--------+----------+-------------+
EXPLAIN ANALYZE SELECT id, sku, price, name FROM products WHERE category_id = 42;
-> Filter: (products.category_id = 42) (cost=50000 rows=50000) (actual time=0.0835..107 rows=2500 loops=1)
-> Table scan on products (cost=50000 rows=499999) (actual time=0.075..95.8 rows=500000 loops=1)
type: ALL is the tell in plain EXPLAIN — a full table scan. The EXPLAIN ANALYZE line underneath shows why: it read all 500,000 rows to find 2,500 matches, taking ~107ms of actual wall time on this hardware. After CREATE INDEX idx_products_category ON products(category_id);:
EXPLAIN SELECT id, sku, price, name FROM products WHERE category_id = 42;
+----+-------------+----------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | products | ref | idx_products_category | idx_products_category | 4 | const | 2500 | 100.00 | NULL |
+----+-------------+----------+------+-----------------------+-----------------------+---------+-------+------+----------+-------+
EXPLAIN ANALYZE SELECT id, sku, price, name FROM products WHERE category_id = 42;
-> Index lookup on products using idx_products_category (category_id=42) (cost=875 rows=2500) (actual time=0.363..3.24 rows=2500 loops=1)
type: ref, estimated cost 50000 → 875, actual time ~107ms → ~3.24ms. Same query, same data, one index. This is what “optimize a query” concretely means — not a percentage to memorize, a plan to change.
The number behind the SELECT * warning
“Avoid SELECT *” is common advice. With just the single-column index above, SELECT * and a column-limited query use the same index and take about the same time — the index says which rows match, but MySQL still fetches the full row per match, so a shorter column list doesn’t help yet:
SELECT * FROM products WHERE category_id = 42;
-> Index lookup on products using idx_products_category (category_id=42) (cost=875 rows=2500) (actual time=0.463..4.47 rows=2500 loops=1)
Add a covering index that includes every column the query needs — CREATE INDEX idx_products_cat_covering ON products(category_id, sku, price); — and ask only for those columns:
EXPLAIN SELECT category_id, sku, price FROM products WHERE category_id = 42;
+----+-------------+----------+------+--------------------------------------------+----------------------------+---------+-------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------+--------------------------------------------+----------------------------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | products | ref | idx_products_category,idx_products_cat_cov… | idx_products_cat_covering | 4 | const | 2500 | 100.00 | Using index |
+----+-------------+----------+------+--------------------------------------------+----------------------------+---------+-------+------+----------+-------------+
EXPLAIN ANALYZE SELECT category_id, sku, price FROM products WHERE category_id = 42;
-> Covering index lookup on products using idx_products_cat_covering (category_id=42) (cost=280 rows=2500) (actual time=0.0288..0.361 rows=2500 loops=1)
Extra: Using index is MySQL telling you it never touched the table — everything the query needs is in the index itself. Cost drops from 875 to 280; actual time from 4.47ms to 0.36ms, about 12x on this run. That’s the real mechanism behind “avoid SELECT *“: it isn’t that * is slow by itself, it’s that * forces a row lookup a covering index could otherwise skip entirely. Whether the plan really flips from type: ALL to type: ref to Using index on your own server is what optimize-query-guide.sh checks: it fills a schema of its own with the same 500,000 rows, replays the EXPLAIN sequence above, confirms that MySQL 8 has no query_cache_* variables, then deletes its rows and prints the DROP DATABASE command for the empty schema it leaves behind.
Finding slow queries and tuning joins
The slow query log and EXPLAIN are both real tools for spotting slow queries, but heatware already has a dedicated page measuring the slow query log’s actual defaults and log format on this same 8.4.10 server: how to enable the MySQL slow query log. If your join is the slow part, see tuning join_buffer_size for hash joins, including the measured threshold where increasing it stops helping. Neither is re-explained here.
What wasn’t tested
OR versus UNION and leading-wildcard LIKE were not tested here.