a mysql index

First, as a B + Tree mysql select data structures, left closing section B + Tree

A balanced binary tree node for storing data is too small, too large tree height need to search many times resulting in the need to find the value you're looking for.

B + Tree have a little look at a few

Data is not stored in non-leaf nodes, only the storage key, can be increased degree, the smaller the value of the index, the better

Leaf node does not store the pointer

Sequential access pointer, to improve access performance interval, the random sequence becomes io io.

The two main index structure of mysql

 

 

 

Below is a diagram of the basic architecture mysql database

 

 

 The principle of joint index created

1, the most common columns, the leftmost matching principle

2, a column, a discrete high-selectivity

3, the smallest of the column, the principle of a minimum of space

Joint index and single-index do not coincide complete waste of space

 

 

If the query can return a result by column inode cover index called keywords, such as the joint index a, b, where a and b used when searching c

Index creation are the following principles

The smaller the better index of the column length

The index is not necessarily better, the more complete the better, must be appropriate

like 99% may be able to use the index, not the same as different results based on dispersion, like% 99% like% 99 can not be used in the index

where are not in the index can not be used and not equal

Multi-purpose specified column query that returns only the data columns you want, you can not use the index

Instead of finding a joint index if the index can not be used from the leftmost column

If the left-most column joint index match and exact match range may be used a further index

If a column joint index range match, then it can not use the index in the right column.

 

mysql leftmost matching 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).

 

1, b + tree data item is a composite data structure, such as (name, age, sex) when, b + tree from left to right in the order of the search tree is established, such as when (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 significance of the establishment of joint index

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

Guess you like

Origin www.cnblogs.com/xiaofeiyang/p/11823584.html