In a very basic setup, I have a table with some fields:
firstname (VARCHAR)
lastname (VARCHAR)
country (INT)
email (VARCHAR)
I created in Index over 2 fields:
CREATE INDEX countr_email_idx ON person (country, email)
This is a simple example from school, so no assumptions on whether the index is of any use should be made.
Now when I query a simple Select over the email field, the MySQL documentation states that because it is not the left-most attribute of my index, it can't be used. This is also reflected in the empty possible_keys
value when i run the query with EXPLAIN
:
EXPLAIN SELECT passenger_id, email
FROM passengerdetails
WHERE email LIKE 'bob%'
which yields
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | passengerdetails | index | NULL | countr_email_idx | 225 | NULL | 36370 | Using where; Using index
However, countr_email_idx
is listed as the index that is used after all. Why does this happen?
Best Answer
The documentation for MySQL has the following bit of information regarding the EXPLAIN Output Format:
So while an index seek is not expected, MySQL will still retrieve the data via the index rather than via a normal data row scan.
Addendum regarding primary key indexes
On the assumption that you are using the InnoDB engine and that the PK on the
passengerdetails
tables is based on thepassenger_id
, then all other secondary indexes will be based on this primary key as stated in the article 14.11.9 Clustered and Secondary Indexes in the official documentation.Even if you didn't create a primary clustered index yourself, MySQL will do it for you to ensure speedy queries. This is documented as:
and: