Using EXPLAIN
MySQL comes with a powerful tool for investigating how the queries
you issue to it are interpreted. Using EXPLAIN, you can get a snapshot of any query to
find out whether you could issue it in a better or more efficient way.
Example 9-6 shows how to use it with the
accounts table you created
earlier.
EXPLAIN SELECT * FROM accounts WHERE number='12345';
The results of this EXPLAIN
command should look like the following:
+--+-----------+--------+-----+-------------+-------+-------+-----+----+-----+ |id|select_type|table |type |possible_keys|key |key_len|ref |rows|Extra| +--+-----------+--------+-----+-------------+-------+-------+-----+----+-----+ | 1|SIMPLE |accounts|const|PRIMARY |PRIMARY|4 |const| 1| | +--+-----------+--------+-----+-------------+-------+-------+-----+----+-----+ 1 row in set (0.00 sec)
The information that MySQL is giving you here is as follows:
select_typeThe selection type is
SIMPLE. If you were joining tables together, this would show the join type.tableThe current table being queried is
accounts.typeThe query type is
const. From worst to best, the possible values can be:ALL,index,range,ref,eq_ref,const,system, andNULL.possible_keysThere is a possible
PRIMARYkey, which means that accessing should be fast.keyThe key actually used is
PRIMARY. This is good.key_lenThe key length is
4. This is the number of bytes of the index that MySQL will use.refThe
refcolumn displays which columns or constants are used ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access