Understanding MySQL Slow Query Analysis with EXPLAIN
This article explains why SQL queries become slow in MySQL, describes the server architecture, details how to use the EXPLAIN statement to interpret execution plans, and outlines how to read and optimize key fields such as type, key, and extra for better performance.
When an interface response is slow, the root cause is often a sluggish SQL query; developers need to diagnose the issue by examining MySQL’s internal processing flow.
The article first introduces MySQL’s architecture, covering the client connection, business logic modules, connection management, query cache, parser, optimizer, executor, storage engine, and physical files.
It then explains how the EXPLAIN keyword simulates the optimizer to reveal the execution plan, listing important columns such as id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, and Extra.
The "type" column indicates join types from best to worst (const → eq_ref → ref → range → index → all), with visual examples for each case.
The "key" column shows the actual index used; a NULL value means no index was applied.
The "Extra" column provides additional execution details, highlighting common messages like "Using index" (covering index, good performance), "Using filesort" (external sorting, to avoid), and "Using temporary" (temporary table creation, often caused by ORDER BY or GROUP BY without proper indexes).
Finally, the article advises enabling MySQL’s slow query log to pinpoint problematic statements and summarizes the key points for effective SQL optimization.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
