当前位置:网站首页>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 .
边栏推荐
- Pytorch parameter initialization
- Numbers that appear only once
- You Li takes you to talk about C language 6 (common keywords)
- Operation suggestions for today's spot Silver
- 大视频文件的缓冲播放原理以及实现
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- 2022 welder (elementary) judgment questions and online simulation examination
- 2022年茶艺师(中级)考试试题及模拟考试
- 测试周期被压缩?教你9个方法去应对
- Regular e-commerce problems part1
猜你喜欢

Linux server development, SQL statements, indexes, views, stored procedures, triggers

图解GPT3的工作原理

numpy中dot函数使用与解析

IPv4 exercises

Kbu1510-asemi power supply special 15A rectifier bridge kbu1510

【经验分享】如何为visio扩展云服务图标

buuctf misc USB

2022 welder (elementary) judgment questions and online simulation examination

buuctf misc USB

【webrtc】m98 screen和window采集
随机推荐
芯片资料 网站 易特创芯
【p2p】本地抓包
vus.SSR在asynData函数中请求数据的注意事项
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
242. Bipartite graph determination
自定义类加载器加载网络Class
C语言二叉树与建堆
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
2022 tea master (intermediate) examination questions and mock examination
[P2P] local packet capturing
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
MySQL multi column index (composite index) features and usage scenarios
解决问题:Unable to connect to Redis
[SUCTF 2019]Game
QT learning 28 toolbar in the main window
[UVM foundation] what is transaction
Most elements
LeetCode 40:组合总和 II
Linux server development, MySQL stored procedures, functions and triggers