A: Concepts
- After indexing, a statement may hit more than one index , then, select the index, it will handed over to the optimizer to select the appropriate index .
- optimizer chooses index purpose is to find an optimal program implementation , and with minimal cost to execute the statement.
Two: optimizer chooses the principle of indexing?
- in a database, the number of scanning lines is one of the factors affecting the implementation of the price .
- the number of lines scanned the less, the less often means that access data on disk, the less CPU resource consumption.
- Of course, the number of scanning lines is not the only criteria for judging whether the optimizer will combine the use of temporary tables, sorting and other factors whether a comprehensive judgment .
Three: The optimizer is how to determine the number of scan lines?
- MySQL Before you can start executing the statement, and can not know exactly meet the conditions of this record how many , but only according to statistics estimate the number of records .
- The statistical information is an index of "discrimination."
- Obviously, the more the index on a different value, the discrimination index, the better.
- and an index on the number of different values, which we call "base" (cardinality).
- that is, the larger the base, the better the discrimination index.
- You can use the show index method, see a base index.
- Principle
- When sampling statistics, InnoDB is selected by default N pages of data, statistical different values on these pages, get a mean value and then multiplied by the number of pages in the index, it has been the base of this index.
- The data table will be continuously updated, the index statistics will not be fixed. So, when the number of changed data rows exceeds 1 / M, it will automatically trigger a re-do index statistics.
- Why do I need to use statistical sampling?
- because the entire table to take out line by line statistics, although you can get accurate results, but the price is too high, you can only choose "statistical sampling."
- In MySQL, there are two ways to store the index statistics, can be selected by setting the value of the parameter innodb_stats_persistent:
- set on time, statistics will indicate persistent storage. In this case, the default is N 20, M 10.
- set off when the statistics indicate only stored in memory. In this case, the default is N 8, M 16.
- Because it is statistical sampling, so no matter where N is 8 or 20, this base is very easy inaccurate.
Five: Select anomaly index, which has several problems can be handled?
- a recount index information
- Since it is not statistics, it would be corrected. analyze table t command, the statistics can be used to re-index information.
- Specifies the index
- select * from table force index(`index_name`);