My simple MySql query doesn't use index My simple MySql query doesn't use index mysql mysql

My simple MySql query doesn't use index


I guess that you have few rows in 'comments' table, this is why MySQL is doing a full table scan instead of using the index in your first query. It's estimating that the cost of a full table scan may be lower than first match the index and then lookup the rows.

In your second query is using the index because it is possible to get all the columns of the query (the 'id' column) directly from the index with no need to lookup the table rows after matching the index. This is the meaning of "Using index" extra information.

Try if with a significant number of rows in 'comments' MySQL still uses a full scan, I think that it would be a strange behaviour. In fact, I've tested exactly the same in a MySQL version 5.1 and it's always using the 'index' even with few rows.


The second query is an index-covered query. The whole information requested can be read from the index (since the primary key is part of any secondary index in InnoDB).

In the first query MySQL has to read the PKs from the index, then to read the rows. Because table has so small amount of rows, optimizer decides that it would be faster if it reads rows directly and discard the ones that do not match