当前位置:网站首页>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
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
- 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 . - 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 .
边栏推荐
猜你喜欢
Vivo large scale kubernetes cluster automation operation and maintenance practice
Système de gestion des défauts du projet Cynthia
On the exploitation of a horizontal ultra vires vulnerability
Install Kubernetes 1.24
苹果放大招!这件事干的太漂亮了……
Docker部署Mysql
Cynthia项目缺陷管理系统
Computing cyclic redundancy codes -- excerpts
China SaaS industry panorama
Cynthia project defect management system
随机推荐
ASCII码值是怎么计算的,怎么计算arccos的值
Node-RED系列(二七):扩展节点之s7节点的使用说明
关于#数据库#的问题:反复检查过了查不出来
Execution order of subclass and parent constructor
程序设计原则
A hot MySQL training topic, making you a master of SQL
flutter简单优秀的开源dialog使用free_dialog
Node red series (27): instructions for S7 node of the extension node
C# 11 更加实用的 nameof
go-zero微服务实战系列(三、API定义和表结构设计)
文件夹数据同步工具Sync Folders Pro
五分钟内编写Pytorch模型
Idea remote debugging jar submitted by spark submit
Record several interesting XSS vulnerability discoveries
go path包
[image denoising] image denoising based on MATLAB Gaussian + mean + median + bilateral filtering [including Matlab source code 1872]
Win7 cannot be remote desktop problem
Actual combat simulation │ real time error alarm of enterprise wechat robot
修饰模式和代理模式的异同
Simple query cost estimation [Gauss is not a mathematician this time]