当前位置:网站首页>Linux server development, MySQL index principle and optimization
Linux server development, MySQL index principle and optimization
2022-07-07 07:54:00 【Tuen Mun pheasant calls me chicken】
Recommend a free open course of zero sound College , Personally, I think the teacher spoke well , Share with you :Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now
1. summary
- Indexes are physical concepts , Constraints are logical concepts .
- The difference between a primary key index and a unique index is that the primary key index cannot be empty , A unique index can have one NULL value .
- Component table PRIMARY KEY Create a primary key for the display , If it is not created, it will be in UNIQUE NOT NULL Select the first one as the primary key . If there is no non empty unique index ,MySql Then automatically generate 6 byte _rowid A primary key .
- A foreign key constraint is a constraint , To ensure the integrity of the two tables , It's transactional , Try not to use .

2.B+ Trees
The full name of binary tree is balanced binary search tree ,B+ Tree is a multi-path balanced search tree , A characteristic of the search tree is that traversal in middle order is an ordered structure . The balance is the height of the tree , The aim is to provide a stable search time complexity , It is the time complexity that remains stable after adding, deleting, modifying and checking . If it becomes a linked list, it will become O(n) Time complexity of , Is not a balanced .B+ The tree is a very balanced , It is mainly reflected in that leaf nodes are on the same layer , The height of each link is the same .
The height of the tree represents the number of comparisons , Red and black trees are tall and thin , Used in memory ; and B+ The tree is short and plump , Used to access disks . One disk IO yes 10ms about , A memory access 100us about , Millions of differences .
B+ How the tree maps to the disk ? A node is 16K The data of , When accessing disk data, the size of the data pulled is usually 4K or 8K, It's exactly an integral multiple , The configuration file can also be modified .B+ One node of the tree must store at least two rows of data . If you exceed 16K, The excess will be mapped to the shared table space . Non leaf nodes store index information , Leaf nodes store data information .
B+ Tree range query LIMIT1,5, Adjacent leaf nodes refer to each other , Avoid backtracking queries .
Auto increment can be used if the data is large bigint type , If every second 1 Billion data , need 5849 It takes years to run out !
myisam in frm At the end is the table information file ;myd It's a data file , Use heap table to organize storage ;myi Is the index file , use B+ Tree organization storage .
inodb in frm Is the table information structure ;bbd It's a data file , use B+ A clustered index created by a tree primary key . When using a secondary index to find , Will find the primary key id, Through the primary key id Find row data , This is called a return table query .
inodb By segment 、 District 、 Page composition , Segments are divided into data segments , Index segment , Rollback segments, etc ; The size of the area is 1MB(1 Zones by 64 Composed of consecutive pages ); The default value of the page is 16K; Page is a logical page , The physical size of the disk is generally 4K perhaps 8K; In order to ensure the continuity of pages in the area , The storage engine usually requests from the disk at one time 4-5 Districts .

- Buffer pool
- Change buffer: Cache non unique indexes DML Add, delete and change operation , The cache first . If it is a unique index, write the log first , Then the asynchronous thread brushes the disk , The non unique index will be written to the cache and then flushed .
- Log buffer
- LRU The algorithm switches out the rarely used data , This is the cached hot data policy . The ring buffer has no data movement .
Left most matching principle
For composite indexes , Match from left to right , encounter <>between like Just stop matching .
Overlay index
Data can be found through the secondary index , Instead of using a clustered index . The secondary index data contains less , Therefore, the tree height is generally lower than that of the clustered index , So there will be fewer disks io. Query clustered indexes and auxiliary indexes will not be queried back to the table .
Index failure
- select … where A and B if A and B One of the does not contain an index , The index becomes invalid
- The index field participates in the operation , The index becomes invalid .from_unixtime(idx)=‘2021-04-30’;
- Implicit conversion of index field , The index becomes invalid .‘1’ Implicitly convert to 1;
- LIKE Fuzzy query , wildcard % start , The index becomes invalid .select *from user where name like ‘%ark’;
- Use... On index fields NOT <> != Index failure .id<>0, Then change to idx>0 or idx<0;
- In the composite index , The first column index is not used , The index becomes invalid .
边栏推荐
- Cnopendata list data of Chinese colleges and Universities
- Shell 脚本的替换功能实现
- [mathematical notes] radian
- C language communication travel card background system
- Hands on deep learning (IV) -- convolutional neural network CNN
- 面试结束后,被面试官在朋友圈吐槽了......
- Pytest+allure+jenkins environment -- completion of pit filling
- Common validation comments
- 微博发布案例
- Technology cloud report: from robot to Cobot, human-computer integration is creating an era
猜你喜欢

Cnopendata American Golden Globe Award winning data

Figure out the working principle of gpt3

【数学笔记】弧度

Idea add class annotation template and method template
![[unity] several ideas about circular motion of objects](/img/84/e70c6696629dbe17ace011553f43ff.png)
[unity] several ideas about circular motion of objects

有 Docker 谁还在自己本地安装 Mysql ?

快速使用 Jacoco 代码覆盖率统计

Is the test cycle compressed? Teach you 9 ways to deal with it

Qt学习27 应用程序中的主窗口

探索干货篇!Apifox 建设思路
随机推荐
Explore Cassandra's decentralized distributed architecture
[2022 actf] Web Topic recurrence
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
芯片资料 网站 易特创芯
What are the positions of communication equipment manufacturers?
242. Bipartite graph determination
vus. Precautions for SSR requesting data in asyndata function
Is the test cycle compressed? Teach you 9 ways to deal with it
[UTCTF2020]file header
【webrtc】m98 screen和window采集
[P2P] local packet capturing
这5个摸鱼神器太火了!程序员:知道了快删!
misc ez_usb
Shell 脚本的替换功能实现
Problem solving: unable to connect to redis
json 数据展平pd.json_normalize
Thinkcmf6.0安装教程
C语言队列
【Unity】物体做圆周运动的几个思路
Detailed explanation of Kalman filter for motion state estimation