Payments are experiencing issues due to temporary restrictions in Russia. If your payment does not go through, please submit a support request.Our support team is available 24/7 — we are always here to help with hosting and server issues.We are now accepting requests for dedicated server rental and colocation services in our data center.Reminder: we recommend enabling backups for additional data protection.A new VPS/VDS lineup with NVMe storage and improved performance is now available.Maintenance work on some servers has been completed. All services are operating normally.
Article3 min readViews2

How to Read EXPLAIN for SELECT in MySQL Without Hasty Conclusions

First look at the query plan: selected indexes, row estimates, and access order. We explain why EXPLAIN ANALYZE requires a separate solution.

Comparing technical analysis results on the desktop
In this article

The catalog page is slow, and SQL is the prime suspect. The query plan helps understand how the optimizer intends to retrieve data. However, it is not an automatic diagnosis: row estimates and the selected index require context, and actual latency also depends on load, data, and expectations.

Start with a known simple query

This example is intended for MySQL 8.x. Use a test copy with representative data and an account with the necessary read permissions. The table products, fields id and active in the example are conditional: replace them with objects from your own schema.

EXPLAIN SELECT id FROM products WHERE active = 1 LIMIT 20;

Do not run the example literally if such a table does not exist. To analyze the problem, you need a real application query with clear parameters. Complex expressions and called functions should be studied separately first: reading the plan should not be considered a universal guarantee of no side effects for arbitrary SQL.

Analyze the access method

In traditional output, pay attention to the table, access type, possible indexes, the selected index, and the row estimate. The field possible_keys does not mean that all listed indexes were used. The field key describes the actually selected option in this plan.

The estimate rows is not an exact count of rows read during actual execution. Statistics and data distribution affect the optimizer's expectations. Therefore, compare plans on data similar to production, not just on an empty test table.

A full table scan is not always an error

For a small table or a query that selects most of the data, a sequential scan can be reasonable. Having an index does not guarantee a fast response: the index may retrieve too many records or fail to match the conditions.

Example: a filter on a flag that is set on almost all products has low selectivity. Adding an index only on this flag is not guaranteed to help. You must consider the entire query, sorting, table joins, and the actual distribution of values.

Do not confuse EXPLAIN with EXPLAIN ANALYZE

EXPLAIN ANALYZE actually executes the query and displays measured data. On a heavy query, this creates real load. Do not enable this mode on a production server as a harmless extension of the query plan viewer.

If you need actual measurements, choose an appropriate environment and constraints together with the administrator. Even a standard SELECT can read a large volume, compete for resources, and hold objects required by the application. This guide does not require running a heavy query in actual mode.

Compare one change at a time

Save the original query, parameters, server version, and execution plan. After changing the index or SQL, repeat the analysis on comparable data. Then measure the execution time and impact on other operations separately. An index speeds up certain reads but requires space and maintenance during writes.

Do not declare optimization complete simply because an index appears in the plan. You need results for a user scenario: does the catalog load faster, has the load decreased, and has query correctness been preserved? The plan helps explain the decision, but it does not replace testing the application itself.

Discussion0

Share your experience and ask questions. Comments without links appear after editorial review.

No comments yet. Start the discussion.