Part blog, we have a detailed description of the index mysql storage structure, that is, our B + tree variants, is a doubly linked list with a B + tree. So today I look at in detail, how to use and how to view the usage index index.
Let's take a simple set up several tables.
Only ID plus an index, let's run explain, look at the results.
Which contains the id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra columns, let's go over how these are what use it.
id:
id column the greater the higher execution priority, id from executing the same downward, id finally performed to NULL.
例如:EXPLAIN SELECT r.id FROM (SELECT id from student) r
We can see from the sql statement, we have two select, that is, there are two id, then, we will give priority to run red color select, which is id 2, and then run the id to select 1.
This column shows which table to explain the line being accessed.
When there is a subquery in the from clause, table columns are <derivenN> format, showing the current query dependent query id = N, so the first query execution id = N.
When a union, the value table column UNION RESULT <union1,2>, 1 and 2 represent the row select id involved in the union.
In order from best to worst are: system> const> eq_ref> ref > range> index> ALL in general, have to ensure that the query reaches the level range, preferably up to ref
This column shows the query which may use the index to find.
There are columns when possible possible_keys explain, while the case key display NULL, this situation is not much because the data in the table, mysql think this query index of little help, choose a full table query. If the column is NULL, no relevant index. In this case, you can see if you can create an appropriate index to improve query performance, and then view the results by checking with explain the where clause.
This column shows which index mysql actually used to optimize access to the table.
If you do not use the index, the column is NULL. If you want to force mysql to use or ignore possible_keys column index, the use of force index in the query, ignore index.
The ref column:
This column shows the extra information.