Mysql in joint index of the leftmost matching principle (Baidu)

When you create a column selection principle of joint index

  1. Column-frequently used (leftmost matching principle)
  2. Column discrete high priority (discrete high principle)
  3. Column-small width (minimum spatial principle) 

Establish a multi-column index (joint index) in Mysql principled leftmost prefix, that left most priority.
If we are to establish a joint index (col1, col2) a two, in fact, has established two joint index (col1), (col1, col2 );
if there is a 3 index (col1, col2, col3), the actual the already established three joint index (col1), (col1, col2 ), (col1, col2, col3).

Explanation

1, b + tree data item is a composite data structure, such as (name, age, sex) when, b + tree from left to right in order to establish a search tree, such as (Zhang, 20, F) when such data to retrieve, b + tree name priority comparison determines the next search direction, if the same name and age Sex comparison in turn, finally obtained data retrieved; but (20, F) no such name the data came, b + tree node which does not know the first step in the investigation, because the time to establish the search tree name is the first comparative factor, you must first name according to the search query in order to know where to go next.

2, such as (Zhang, F) ​​to retrieve this data, b + tree name can be used to specify the search direction, but the lack of age next field, so only the name is equal to the seating of the data is found, then sex is F match of the data, this is a very important property, namely the left-most matching characteristics of the index. (This situation can not use the joint index)

mysql create a joint index of significance in

One of the top three

Built a (a, b, c) of the composite index, equal to the actual construction of the (a), (a, b), (a, b, c) three indexes, because each one more index, will increase the write operation overhead overhead and disk space. For large amounts of data tables, this is not a small overhead!

Covering index

The same composite index (a, b, c), if the following sql: select a, b, c from table where a = 1 and b = 1. Then MySQL can be obtained directly by traversing the index data, without the need to return to the table, which reduces the number of random io operations. Reduce operating io, io particular random fact is the main optimization strategy dba. So, in a real practical applications, covering index is one of the main means of optimized performance improvements

The more the index column, the less screened by the index data

There 1000W table of data, the following sql: select * from table where a = 1 and b = 2 and c = 3, assuming that each condition may be assumed that 10% of the selected data, if only a single value index, by which index can be screened 1000W 10 = 100w% of data, and return data table found for b = 2 and c = 3 from 100w of data, and then sorting, then paging; if it is a composite index, the index selected by 1000w  % 10  10%  10% = 1W, and then sorting, paging, which is more efficient, glance

 

Computing discrete columns: count (distinct col) / count (col)
for example:
ID column is not repeated a total of 9 9/9 = 1
Gender column only a total of 9 (male or female) is approximately equal to two 2/9 0.2
The larger the higher the selectivity discrete

 

Guess you like

Origin www.cnblogs.com/aspirant/p/11404174.html