当前位置:网站首页>Mysql database index study notes
Mysql database index study notes
2022-07-07 09:14:00 【White butterfly】
Preface
The author has been studying database indexing recently , So I want to write a blog to record my learning , If there is a mistake , Please point out .
One 、 Index introduction
1.1 What is the index
The index is a separate 、 Database structure stored on disk , Contains a reference pointer to all records in the data table . Using an index, you can quickly find rows with a specific value in one or more columns , all MySQL All column types can be indexed , Using indexes on related columns is the best way to improve the speed of query operation .
1.2 Advantages and disadvantages of index
advantage :
- Can improve the efficiency of data retrieval , Reduce the IO cost
- Sort data through index columns , To reduce the CPU Consumption of
Inferiority :
- Take up disk space
- When adding data in the table 、 When deleting and modifying , Index should also be maintained dynamically , This reduces the speed of data maintenance .
1.3 When to use index
- When uniqueness is a characteristic of some kind of data itself , Specify a unique index . Using a unique index ensures the data integrity of the defined column , To improve query speed .
- To sort or group frequently ( That is to say group by or order by operation ) Index on the column of , If there are more than one column to sort , You can build composite indexes on these columns .
Two 、 Classification of indexes
2.1 Example index
- General index : General index is MySQL The basic index type in , Allows you to insert duplicate and null values in a column that defines an index
- Unique index requires that the value of the index column must be unique , But you can have an empty value . If it's a composite index , The combination of column values must be unique
- Primary key index is a special unique index , No null values are allowed
2.2 Composite index
- Index of multiple field combinations , Only if the left side of these fields is used in the query criteria , Indexes are used . If the intermediate index is not used , Later indexes will also become invalid , When using a composite index, follow the leftmost prefix set
Clustered index selection rules :
- If there is a primary key , A primary key index is a clustered index .
- If there is no primary key , The first unique... Will be used (UNIQUE) Index as clustered index .
- If the table does not have a primary key , Or there is no suitable unique index , be InnoDB It will automatically generate a rowid As a hidden gathering rope
lead .
3、 ... and 、 Use of index
3.1 Basic commands for indexing
1. Create index :
create index Index name on Table name ( Field name ( length ));
alter table Table name add index Index name ( Field name ( length ));
2. Delete index :
drop index Index name on Table name ;
3. Look at the index :
show index from Table name
3.2 Determine whether the index is effective
have access to EXPLAIN Statement to see if the index is in use .
explain select * from Table name where Conditions ( Conditions need to be indexed )
The meaning of each field in the execution plan :
3.3 Avoid index invalidation
There are several ways , To avoid index failure :
1. When using composite indexes , Need to follow “ Left most prefix ” principle ;
2. Do nothing on the index column , For example, to calculate 、 function 、 Type conversion , It will cause index invalidation and turn to full table scan ;
3. Try to use overlay index ( Queries that access index columns ), Reduce select * Overwriting the index can reduce the number of table returns ;
4.MySQL In use is not equal to (!= perhaps <>) Unable to use the index will result in a full table scan ;
5.LIKE Start with a wildcard (%abc)MySQL The index will become invalid and become a full table scan operation ;
for example :
select * from company where companyName like ‘% Jiangnan leather is long ’;
It can be changed to
select * from company where reverse(companyName) like reverse(‘% Jiangnan leather is long ’);
6. String without single quotation marks will invalidate the index ( An implicit conversion of the index column may have occurred );
7. When or The conditions of connection , When the left and right fields have indexes , The index will take effect
Four 、 Index implementation principle (InnoDB)
stay MySQL In the index structure of , The choice is B+Tree, So what is B+Tree Well ? Say B+Tree Let's introduce it first B-Tree
B-Tree(B Trees ),B A tree is a kind of multi fork road scale search tree , Relative to a binary tree ,B Each node of the tree can have multiple branches , That is, multi fork .
At a maximum degree (max-degree) by 5(5 rank ) Of b-tree For example , So this one B Each node of the tree can store up to 4 individual key,5
A pointer to the :
characteristic :
- 5 Step B Trees , Each node can store up to 4 individual key, Corresponding 5 A pointer to the .
- Once the node stores key The quantity has arrived 5, Will fission , The intermediate element splits up .
- stay B In the tree , Both non leaf nodes and leaf nodes store data .
B+Tree yes B-Tree Variants , We have a maximum degree (max-degree) by 4(4 rank ) Of b+tree For example , Come and have a look
The structure diagram is shown below :
In the end, we see ,B+Tree And B-Tree comparison , There are three main differences :
- All the data will appear in the leaf node .
- Leaf nodes form a one-way linked list .
- Non leaf nodes only serve to index data , The specific data is stored in the leaf node .
MySQL Index data structure for classic B+Tree optimized . In the original B+Tree On the basis of , Add a node pointing to adjacent leaves
Linked list pointer , So we have a sequence pointer B+Tree, Improve the performance of interval access , Conducive to sorting .
5、 ... and 、 other
5.1 Return to the table for query
First look for data in the secondary index , Find primary key value , Then it is added to the clustered index according to the primary key value , How to get the data , It is called back to table query .
When we execute the following query statement , The specific process is as follows :
1. Because it's based on name Field to query , So first according to name='Arm’ To name Field in the secondary index
look for . But in the secondary index, you can only find Arm The corresponding primary key value 10.
2. Because the data returned by the query is all fields , So at this time , You also need to use the primary key value 10, Find in the clustered index 10 Corresponding records , Finally find 10 The corresponding line row.
3. Finally get the data of this line , Just go back .
5.2 Overlay index
Overlay index means The query uses an index , And the columns that need to be returned , All can be found in this index .
5.3 Prefix index
When the field type is string (varchar,text,longtext etc. ) when , Sometimes you need to index long strings , It will make
The index becomes large , When inquiring , Waste a lot of disk IO, Affecting query efficiency . At this point, you can prefix only part of the string with , build
Vertical index , This can greatly save index space , To improve index efficiency .
create index idx_xxxx on table_name(column(n)) ;
summary
I hope it's lucky
边栏推荐
- Locust performance test 4 (custom load Policy)
- Detailed learning notes of JVM memory structure (I)
- [chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
- 2020 year end summary
- Serial port experiment - simple data sending and receiving
- STM32串口寄存器库函数配置方法
- Personal deduction topic classification record
- PMP experience learning and sharing process
- What is the use of PMP certificate?
- Simulation volume leetcode [general] 1706 Where does the ball meet
猜你喜欢
Synchronized underlying principle, volatile keyword analysis
Storage of data in memory
2022-06-30 unity core 8 - model import
数据在内存中的存储
C language pointer (Part 2)
Serial port experiment - simple data sending and receiving
2022-07-06 Unity核心9——3D动画
Postman interface debugging method
Postman interface test (I. installation and use)
C language pointer (special article)
随机推荐
OpenGL帧缓冲
Detailed learning notes of JVM memory structure (I)
Screen automatically generates database documents
UnityShader入门精要个人总结--基础篇(一)
徽商期货公司评级是多少?开户安全吗?我想开户,可以吗?
Simulation volume leetcode [general] 1706 Where does the ball meet
Self awakening from a 30-year-old female programmer
Postman interface test (II. Set global variables \ sets)
H3C VXLAN配置
C language pointer (exercises)
串口实验——简单数据收发
Serial port experiment - simple data sending and receiving
面试题:高速PCB一般布局、布线原则
Panel display technology: LCD and OLED
Digital triangle model acwing 275 Pass a note
Common operating commands of Linux
C language pointer (special article)
Systick滴答定时器
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
Summary of PMP learning materials