As we all know, InnoDB uses IoT (Index organization table), the so-called Index organization table, and leaf nodes store all the data, which means that data is always stored in some order. So the question is, if it is such a statement, how should it be executed? The statements are as follows:
Select Count (distinct from table1;
There is an index on column A, so how do you scan it according to the simple idea? Very simple, one scan, so that, in fact, did an index full scan, the efficiency is very poor. This scanning method scans a lot of duplicate indexes, so the optimization is easy to think of: Skip the duplicate index. So the Internet can search for such an optimization method:
Select Count (* from (selectdistinct from table1) t;
From the data that has been searched, the extra in such an execution plan becomes the using index for group-by from the using index.
But, however, fortunately we are not using the 5.1 version now, we are basically more than 5.5, these modern versions, have implemented the loose index scan:
Good and good, there is no need to use this artifice to optimize SQL.
There is a bit of meaning in the document about group by, saying that the most popular way is to do a full table scan and create a temporary table, so that the execution plan will be ugly, there must be all and using temporary table.
Deep feeling this one wrote a lot of writing, weekend more, I want to go to bed early.
MySQL's loose index scan