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.

May 10, 2025 - 11:50
Updated: 
101.2k

Cybersecurity Careers & Certifications in Cybersecurity & Ethical Hacking · Course information

Data Analytics & Data Science

MySQL Performance Basics: Indexes, EXPLAIN and the Slow Query Log

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?

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.

ColumnWhat to look for
typeALL means a full table scan; ref, eq_ref, range and const are usually better
possible_keysIndexes MySQL could use
keyThe index actually chosen; NULL means none
rowsEstimated rows examined; large numbers signal work to reduce
ExtraUsing 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 = 42Yes
WHERE customer_id = 42 ORDER BY created_atYes, for filter and sort
WHERE created_at > '2026-01-01' onlyNot 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?

CheckWhy
Index columns used in WHERE and JOINAvoids full table scans
Read EXPLAIN for slow queriesShows how MySQL runs them
Enable the slow query logFinds the queries worth fixing
Size the InnoDB buffer poolKeeps 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

Find the slowest queries using the slow query log or sys schema views before changing indexes or server settings.

EXPLAIN shows the planned execution without running the query, while EXPLAIN ANALYZE runs it and reports actual rows and timings.

It means MySQL reads every row in the table, a full table scan, which is often slow on large tables.

No. Each index uses storage and slows inserts and updates, so add indexes only for columns used in frequent filters, joins or sorts.

A composite index can be used for queries that filter on its first column, or first and second columns, and so on in order.

Wrapping an indexed column in a function usually prevents a normal index lookup, so rewrite the condition as a range on the raw column.

It sets the number of seconds a statement must take before the slow query log records it when the log is enabled.

The InnoDB buffer pool size usually matters most, because it decides how much data and index content stays cached in memory.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.