MySQL Performance Basics: Indexes, EXPLAIN and the Slow Query Log
Most MySQL performance problems come from a few slow queries, not from the server itself. The basic method has four steps. First, find slow queries with the slow query log or the sys schema. Second, run EXPLAIN or EXPLAIN ANALYZE to see how MySQL executes them, looking for full table scans and large row estimates.
Topic: Cybersecurity Careers & Certifications in Cybersecurity & Ethical Hacking · Course information
Also covers: Data Analytics & Data Science
Quick answer: Most MySQL performance problems come from a few slow queries, not from the server itself. The basic method has four steps. First, find slow queries with the slow query log or the sys schema. Second, run EXPLAIN or EXPLAIN ANALYZE to see how MySQL executes them, looking for full table scans and large row estimates. Third, add or adjust indexes that match the WHERE, JOIN and ORDER BY columns, using composite indexes in the right column order. Fourth, measure again to confirm the change helped. Only after queries are fixed should you look at server settings such as the InnoDB buffer pool size.
Key takeaways
- Measure first; do not guess which query is slow.
- EXPLAIN shows the plan; EXPLAIN ANALYZE also runs the query and shows actual timings.
- An index helps only if its columns match how the query filters or sorts.
- Composite index column order matters because of the leftmost prefix rule.
- Every index speeds some reads but adds cost to writes, so add them with a reason.
Table of Contents
- How Do You Find Slow Queries in MySQL?
- How Do You Read an EXPLAIN Plan?
- How Do Indexes Speed Up Queries?
- How Should You Order Columns in a Composite Index?
- Which Query Patterns Stop Indexes Working?
- When Should You Tune Server Settings?
- What Is a Simple Performance Checklist?
- References
How Do You Find Slow Queries in MySQL?
Turn on the slow query log, which records statements that take longer than long_query_time seconds. On a test server you can enable it at runtime:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SHOW VARIABLES LIKE 'slow_query_log_file';
Summarise the log with mysqldumpslow, or query the sys schema view statements_with_full_table_scans:
SELECT query, exec_count, no_index_used_count
FROM sys.statements_with_full_table_scans
LIMIT 10;
Put permanent settings in the server configuration file rather than relying on SET GLOBAL.
How Do You Read an EXPLAIN Plan?
Put EXPLAIN before a SELECT to see the plan, as described in the EXPLAIN statement docs. EXPLAIN ANALYZE runs the query and reports actual rows and time per step.
| Column | What to look for |
|---|---|
| type | ALL means a full table scan; ref, eq_ref, range and const are usually better |
| possible_keys | Indexes MySQL could use |
| key | The index actually chosen; NULL means none |
| rows | Estimated rows examined; large numbers signal work to reduce |
| Extra | Using filesort or Using temporary can point to sort or grouping costs |
The full list is in EXPLAIN output format.
How Do Indexes Speed Up Queries?
An index lets InnoDB find matching rows without reading the whole table. The manual page on how MySQL uses indexes explains the details. A typical fix looks like this:
EXPLAIN SELECT order_id, total
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC;
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at);
EXPLAIN ANALYZE SELECT order_id, total
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC;
Compare the type, key and rows values before and after.
How Should You Order Columns in a Composite Index?
MySQL can use a multiple-column index for any leftmost prefix of its columns.
| Index on (customer_id, created_at) | Can the index help? |
|---|---|
| WHERE customer_id = 42 | Yes |
| WHERE customer_id = 42 ORDER BY created_at | Yes, for filter and sort |
| WHERE created_at > '2026-01-01' only | Not as a normal index lookup |
Put equality-filtered columns first, then range or sort columns.
Which Query Patterns Stop Indexes Working?
- Wrapping an indexed column in a function, such as WHERE YEAR(created_at) = 2026; use a date range instead.
- A leading wildcard such as LIKE '%abc'.
- Comparing a string column to a number, which forces type conversion.
- SELECT * when only a few columns are needed.
When Should You Tune Server Settings?
After queries and indexes are fixed. The most important InnoDB setting is the buffer pool, which caches data and indexes in memory. Change one setting at a time and measure. For the wider operational side, see our MySQL database administration guide, and for analytical queries see MySQL for data analysts. WebAsha offers hands-on MySQL performance training.
What Is a Simple Performance Checklist?
| Check | Why |
|---|---|
| Index columns used in WHERE and JOIN | Avoids full table scans |
| Read EXPLAIN for slow queries | Shows how MySQL runs them |
| Enable the slow query log | Finds the queries worth fixing |
| Size the InnoDB buffer pool | Keeps hot data in memory |
The MySQL 8.4, Optimization chapter and the MySQL 8.4 Reference Manual explain each item in detail.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0