The concept of the index:
A dispersion index is stored in a data structure (search) the value of a database table in one or more columns to sort
Why use an index ?
- The amount of data indexing can greatly reduce the storage engine to be scanned
- The index may become the order of random IO IO
- When the index can help us grouping, sorting operation, avoid the use of temporary table
Clustered index:
The physical order of rows in a database table with the logical data (index) is the same as the sequence key
End of the secondary index to the primary key index, and then look for the primary key index values
If innodb not indexed by default will create an implicit primary key index 6bit
As long as the index is continuous, the data storage location on the storage medium is also continuous
Joint index
For example, index creation is [name, phone]
- Often used in column-major [ leftmost matching principle ]
- Selective high priority [ discrete high principles ]
- Small column-width [ a minimum of space principles ]
Discrete columns:
count(distinct column):count(column)
The larger the ratio the better the dispersion, the better the better the selectivity discrete
Leftmost matching principle :
Keyword index is calculated, must be carried out once from left to right, and can not be skipped
Covering indexes:
If the query returns directly lek by the inode keywords can reduce database IO , the random IO becomes Shun order IO , improve query
Index specifications:
- The data length of the index column as little less.
- The index is not necessarily better, the more complete the better, we must build a suitable, not too big field, for example, is 11 to phoneNum
- Construction of the index, does not allow the field is empty
- Available to the prefix matched to the index Like 999%
- Where conditions Not in and <> can not use the index
- Matching range of values, order by can also be used in the index
- Multi-purpose specified column query that returns only the data columns you want, use less select *