当前位置:网站首页>Exploration of MySQL index
Exploration of MySQL index
2022-04-22 08:04:00 【Bonyin】
1 The essence of index
Index is help mysql A well ordered data structure for efficient data acquisition .

- Now suppose that the data structure of the index is to find the binary tree structure , The table structure shown in the figure above , The index field is col2, Building a lookup binary tree is the structure on the right ,eg:
- Now there's one sql sentence ,select * from t where t.col2 = 89;
- The process is to find the binary tree first , find 89 This node ( Node is a <k,v> The structure of storage ,k Is the index value 89,v Is the disk storage pointer of the row where the index is located 0x77).
- according to v Disk storage pointer , Make a disk io Operation can get data .
Be careful
- But if we don't have an index , The disk is traversed one by one in order io, Efficiency is too low , When the amount of data is large , It's impossible .
- When the index is a binary tree structure , What's the problem ?
Or the example above , When our index is col1 Field time , At this time, the structure of the search binary tree is as follows , It has become a linked list structure .

Similar to the above structure , Efficiency is too low , Every time you look for data , You have to go through it from the beginning in memory . So indexing binary tree is not feasible .
1.1 Red and black trees are indexed ?
- Characteristics of the red black tree :
- The root node is a black node ;
- A red node , The corresponding child nodes are all black nodes ;
- Leaf nodes are all black nodes , by NUll;
- The number of black nodes on the path from any non leaf node to leaf node is the same ;
- Red black tree as a balanced binary tree , But it's different from AVL Trees , The red black tree changes by changing the color between node paths , The index will not appear. One path is the of other paths 2 Times longer , Compared with AVL Trees , The frequency of its self rotation balance is reduced a lot , In terms of performance AVL Better trees .
- Now it's still the above example , With col1 The field is index , Building a red black tree :

As mysql Index words , The problem is that when there are tens of millions of data in the table , So the depth of red and black trees is too deep , Queries are too slow . stay
Looking at the use of col1 structure AVL The look of the tree :

AVL The tree is building (1-7) Spin when 4 Time , But the red and black tree spin is 3 Time , The performance of index red black tree is better .
1.2 mysql The underlying index is B+Tree structure :
- Let's talk about it first B-Tree structure :

characteristic : Multiforked tree , The depth of leaf nodes is the same , There are no pointers between leaf nodes ;
All index elements are not duplicate , From left to right is self increasing .
And the disk file pointer of index data is stored on the location of each index element ; - B+Tree structure :

1.3 mysql Storage engine MyisAM
- The table of the storage engine falls on the disk, and the corresponding files are three : xx.frm xx.MYI, xx.MYD
- xx.frm What is stored is the data related to the table structure ;
- xx.MYI Stored is index data ;
- xx.MYD What is stored is the data of the table ;

The picture above is MyisAm The index storage structure of , Index and data are separated .
1.4 InnoDB Storage engine for
Index and data together .
The corresponding file on the disk is :xx.frm Table structure information
xx.ibd Store data and index 
Leaf nodes store complete data , Reduce the operation of returning to the table .
Why? innodb The primary key of is recommended to be self incremented ? Because the underlying data store is organized according to the primary key , If you don't add a primary key ,mysql The default primary key will be added . Integer index comparison size ratio UUID More efficient and efficient .
Self increase is due to b+tree With bidirectional pointers between leaf nodes , Convenient range lookup to retrieve data .
hash Type index can't solve the problem of access and search . So it just doesn't work .
版权声明
本文为[Bonyin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220626055424.html
边栏推荐
- Training and empowerment | building a professional technical service team
- adb命令的使用【一】
- Focusing on data security, Shiping information is listed in the six sub areas of the panorama of China's network security industry
- VMware set fixed IP address -- bridge mode
- 培训赋能 | 打造专业技术服务团队
- Shiping information was listed in the panorama of China's network security market in 2021
- .net core CefSharp ChromiumWebBrowser中网站JS调用winfrom程序中C#方法
- 《数据安全法》解读及安全保密技术应用交流会在京成功举办
- Fiddler使用
- [write missed scan from scratch] host discovery -- handwritten a subdomain name digger
猜你喜欢

About information collection

log4net在.Net Core使用

行业应用 |从实践中读懂银行的敏感数据安全防护思路

. net5 log4net failed to log to the database after starting for a period of time

Continue raspberry pie 4B + OLED: automatically display the IP address after startup

Call another function within a shell function (without return value and with return value)

世平信息成功通过中国信通院“数据安全类产品能力验证”评测

About XSS Cross Station

Raspberry pie 4B: USB boot using raspi config

Autoware displays user interface details
随机推荐
Shiping information was listed in the panorama of China's network security market in 2021
Raspberry pie 4B: USB mobile hard disk box startup (beta version)
web自动化:8.2 selenium中如何发送js指令
utgard连接opcserver报错Caused by: org.jinterop.dcom.common.JIRuntimeException: Access is denied. [0x800
登录界面——渗你千千万万遍
mysql安装采坑
Raspberry pie Lite: install the latest version of discuz
关于信息收集
How to protect personal information security@ Everyone must not miss this dry goods sharing
.net 5 Web自定义中间件实现返回默认图片
Redis listens for key expiration events
世平信息上榜《CCSIP 2021中国网络安全产业全景图》
职工信息的综合运算
SuperSocket在.Net5中使用——启动篇
Leaders of Hangzhou commercial password Application Association and their delegation visited Shiping for information exchange
实验5 组件及事件处理
Read SAE J1939 protocol data stream
adb命令的使用【一】
【技术】西门子软PLC与组态软件如何通讯?
Xcode select: failed to locate 'PIP3', requesting installation