
The MySQL Workshop
By :

EXPLAIN
is a very useful tool when it comes to performance. The SQL query is used to tell the database what you want, but EXPLAIN
asks the database how it thinks it is going to do it.
Let's use the city
table in the world_simple
database as an example:
SELECT * FROM city WHERE ID=2460; EXPLAIN SELECT * FROM city WHERE ID=2460;
This produces the following output:
Figure 5.30 – SELECT and EXPLAIN
Note that it says 1 warning
. You can see the actual message by running SHOW WARNINGS;
. This is expected for EXPLAIN
as there will be a note with a rewritten version of the statement. You can ignore this for now.
We select a single city (Skopje
) and are using an ID (2460
) to do this lookup.
Let's go over the EXPLAIN
output to see what each field means:
Figure 5.31 – Meaning of each field
Now let's add a primary key to this table and run EXPLAIN...