当前位置:网站首页>Talk about MySQL indexing mechanism

Talk about MySQL indexing mechanism

2022-06-13 10:32:00 edui

Index principle

  • Brief introduction of index

First MySQL If there is no index, the query speed is very slow , So in view of the rapidity of binary search, the index is established ,MySQL It's using B+ Tree index , Pictured
 Insert picture description here
The sub leaf node stores data information , Non child leaf nodes store directory information , That is, the minimum value corresponding to a page and each node pointing to a page saves a page of data , Pages are connected by a two-way linked list ( It is easy to traverse with a two-way linked list ), One page save 16k The record of , Each record in the page is connected through a single linked list ( The single linked list is mainly designed to reuse space ), By primary key ( If the primary key is not designed, the value of the auto increment primary key column will be hidden ) From small to large, the order is good , Each page also has a corresponding slot to record the value and address offset of a section of records like the directory of a book, which can be accessed directly rather than through the chain head .B+ The query process of tree index is first and B+ Compare the roots of the tree to find , Compare the index value with the slot to quickly find , Find a position where the next value is larger than it and the previous value is smaller than it , Go to the small column to get the corresponding number of pages, find the corresponding number of pages and do the same comparison , Found when the leaf node is found .

  • Secondary indexes
    The secondary index is to create another one through the index column B+ Trees , The difference between the tree node and the primary index is that the primary key value is saved in the non child node to ensure the uniqueness of the index , In the leaf node, only the secondary index value and the corresponding index, that is, the primary key value, are saved , Do not save complete data columns . In the query process, the primary key value corresponding to the value is found through the index, and then the complete records are queried back to the table through the primary key value , But if you only query the primary key value, you don't need to go back to the table , So try to use less or no similar (select * from xxxx) This kind of asterisk query .

SQL Reasons for slow implementation

  1. Occasionally slow
    It may be a log redo Writing full slows down the process of flushing dirty pages and then reading to the buffer , Or maybe the data to be accessed has a lock waiting for blocking .
  2. It's been slow
    This is the reason why the index is not taken , Maybe there is no index , It may be that the index has not been set up , Maybe the database selected the wrong index .
    For example, the index column is used expression , In fuzzy query, if the fuzzy symbol is in the front, the index will not go
    Most of the reason why the database runs the wrong index is that when searching the secondary index, the database will predict in the query optimizer whether to directly scan the whole database or run the secondary index first and then return to the table. It is expensive , Who costs less and who uses sampling statistics to predict , So it may be wrong to predict , And then, instead of going through the index, you scan the whole thing .

summary

When using indexes, you should pay attention to the following items :
1、 Just for searching 、 Create indexes for sorted or grouped Columns
2、 Create indexes for columns with large cardinality
3、 The type of index column should be as small as possible
4、 You can index only the prefix of the string value
5、 Only when the index column appears alone in the comparison expression can the index
6、 In order to minimize the occurrence of page splitting and record shifting in the clustered index , It is recommended that the primary key have AUTO_INCREMENT attribute .
7、 Locate and remove duplicate and redundant indexes from the table
8、 Try to use overlay index to query , That is, it is better to include only index columns in the query list to avoid the performance loss caused by returning to the table .

原网站

版权声明
本文为[edui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130925196471.html