Why is MAX() 100 times slower than ORDER BY … LIMIT 1?

前端 未结 1 1961
悲哀的现实
悲哀的现实 2021-01-02 12:31

I have a table foo with (among 20 others) columns bar, baz and quux with indexes on baz and quux

相关标签:
1条回答
  • 2021-01-02 12:56

    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.

    0 讨论(0)
提交回复
热议问题