I have a table foo
with (among 20 others) columns bar
, baz
and quux
with indexes on baz
and quux
You should add an index on (bar, quux)
.
Without this index, MySQL can't see how to perform the query efficiently so it has to choose from various inefficient query plans.
In the first example it scans the quux
index and for each row found, looks up the corresponding value of bar
in the original table. This takes twice as long to check each row, but it gets lucky that a row that has the correct value of bar
is near the beginning of its scan and so it can stop. This could be because the value of bar
you are searching for occurs frequently, so the chance of being lucky is very high. As a result it may only have to examine a handful of rows before it finds a match, so even though it takes twice as long to check each row, the fact that only a few rows are checked gives a massive overall saving. Since you have no index on bar
, MySQL doesn't know in advance that the value :bar
occurs frequently so it cannot know that this query will be fast.
In the second example it uses a different plan where it always scans the whole table. Each row is read directly from the table, without usage of an index. This means that each row read is fast, but because you have a lot of rows, it is slow overall. If none of the rows matched on :bar
this would be the faster query plan. But if roughly 1% of rows have the desired value of bar
, it will be (very) approximately 100 times slower to use this query plan compared to the above plan. Since you have no index on bar
, MySQL doesn't know this in advance.
You could also just add the missing index and then both queries will go much faster.