Let's say we only have an index idx_abcd, which is (A, B, C, D). There are approximately 30 million rows.
We now have a query
SELECT COUNT(*)
FROM tableName
WHERE B = ? AND C = ? AND D = ?;
The EXPLAIN
statement of the above query shows the following useful data:
The WHERE
condition does not follow the left-most principle, but the EXPLAIN
shows the DB does use the index.
Could it be possible?
The version of MySQL is 5.7. The Engine is InnoDB.
The EXPLAIN field type: index
indicates it's doing an "index scan." In other words, it's traversing every entry in the index. This is also quite costly, perhaps nearly as costly as a table-scan.
But if there are other columns in the table besides B, C, D, the optimizer might estimate that it will be at least a bit less expensive to read every page for that index than to read every page for the whole table.
The EXPLAIN output is a little confusing, because it does tell you that it's using an index, and it even says "Using index" in the extra notes field. We're told that this note means the query is able to get what it needs by only reading that index, and it can avoid reading pages of the table itself. This is actually true in this case. It's reading only the index named. But it's reading the whole index, not just the subset of the index matching your conditions.
You probably already know why it can't use your index idx_abcd
for row restriction. Because the index has another column A
as the leftmost column, it can't assume that the index entries matching your conditions are stored together.