当前位置:网站首页>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 .
边栏推荐
- 《动手学深度学习》(四) -- 卷积神经网络 CNN
- Ansible
- 微信小程序基本组件使用介绍
- Qt学习28 主窗口中的工具栏
- Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
- vus.SSR在asynData函数中请求数据的注意事项
- Detailed explanation of Kalman filter for motion state estimation
- Cnopendata American Golden Globe Award winning data
- Visualization Document Feb 12 16:42
- pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
猜你喜欢
2022年茶艺师(中级)考试试题及模拟考试
为什么要了解现货黄金走势?
[guess-ctf2019] fake compressed packets
解决问题:Unable to connect to Redis
nacos
What are the positions of communication equipment manufacturers?
Leetcode 90: subset II
2022 welder (elementary) judgment questions and online simulation examination
【斯坦福计网CS144项目】Lab3: TCPSender
快速使用 Jacoco 代码覆盖率统计
随机推荐
Shell 脚本的替换功能实现
Ansible
[mathematical notes] radian
C语言航班订票系统
Qt学习28 主窗口中的工具栏
Installing postgresql11 database under centos7
【webrtc】m98 screen和window采集
【VHDL 并行语句执行】
探索干货篇!Apifox 建设思路
vus. Precautions for SSR requesting data in asyndata function
Common validation comments
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
Linux server development, MySQL process control statement
After the interview, the interviewer roast in the circle of friends
Common method signatures and meanings of Iterable, collection and list
Value sequence (subsequence contribution problem)
@component(““)
Live online system source code, using valueanimator to achieve view zoom in and out animation effect
Problem solving: unable to connect to redis
【webrtc】m98 screen和window采集