Measure First
Optimize the queries that matter: the slowest, or the fastest ones run thousands of times. The slow query log records statements that take longer than long_query_time.
Summarize the log with mysqldumpslow -s t /path/to/slow.log (shows the worst queries by total time) or pt-query-digest from Percona Toolkit. These commands change server settings and read a log file, so they are shown but not run here.
See Slow Statements with the sys Schema
MySQL 8 ships the sys schema, built on performance_schema. Statements are grouped by their normalized form:
Check that the views exist on any server:
Result:
| sys_schema_present |
|---|
| 1 |
Pattern 1: Select Only What You Need
SELECT * reads and sends every column, defeats covering indexes, and breaks when columns are added. Name the columns.
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_country_created | 55 | NULL |
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_country_created | 55 | Using index |
The second plan can show Using index: no table rows are read.
Pattern 2: Keyset Pagination
LIMIT 20 OFFSET 19980 still reads and throws away 19,980 rows. Remember the last id and continue after it:
Result:
| id |
|---|
| 6 |
| 7 |
| 8 |
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | range | PRIMARY | 20 | Using where |
The cost stays small on every page. The trade-off is that you cannot jump to page 500 directly.
Pattern 3: Avoid Functions on Indexed Columns
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | index | idx_big_created | 20216 | Using where; Using index |
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| big_users | ref | idx_big_created | 55 | Using index |
Pattern 4: Count Smartly
COUNT(*) on a big InnoDB table must count rows, because of MVCC. If an approximation is enough, use the statistics:
Result:
| has_estimate |
|---|
| 1 |
TABLE_ROWS is an estimate for InnoDB. Use it for "about 20,000 rows" displays, and exact COUNT(*) where precision matters.
Pattern 5: Batch Writes
Insert many rows per statement or per transaction. Updating a million rows in one go creates a huge transaction. Work in chunks:
Pattern 6: EXISTS Instead of Counting
MySQL plan:
| table | type | key | rows | Extra |
|---|---|---|---|---|
| c | ALL | NULL | 6 | NULL |
| o | ref | customer_id | 1 | Using index; FirstMatch(c) |
Housekeeping
ANALYZE TABLE t;refreshes statistics so the optimizer chooses well.OPTIMIZE TABLE t;rebuilds a table to reclaim space after huge deletes (it locks or copies the table, so schedule it).- Keep the InnoDB buffer pool large enough for the hot data. Check
innodb_buffer_pool_size. - Match column types in joins and use the same character set and collation.
Result:
| buffer_pool_mb | long_query_time_default |
|---|---|
| 128 | 10.0 |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Optimizing without measuring | Wasted effort | Slow log, sys.statement_analysis, EXPLAIN |
Deep OFFSET pagination | Slower every page | Keyset pagination |
SELECT * everywhere | Wasted I/O, no covering index | Select columns |
| Huge single transactions | Long locks, big undo log | Batch |
| Ignoring the buffer pool size | Disk reads on hot data | Size it for your data |
| Query cache advice from old blogs | Removed in MySQL 8.0 | Cache in the application |
Practice
- Rewrite an OFFSET pagination query with keyset pagination.
- Find a query using a function on a column and rewrite it.
- Turn on the slow query log in a test server and capture a
SLEEP(2)query. - List unused indexes with the sys schema.
Conclusion
Measure with the slow log and sys, then fix: select fewer columns, use keyset pagination, keep indexed columns free of functions, batch big writes, and keep statistics fresh.

