当前位置:网站首页>Reading mysql45 lecture - index continued
Reading mysql45 lecture - index continued
2022-06-25 16:25:00 【Hurry to Friday】
There is a table creation statement as follows :
create table T ( ID int primary key, k int NOT NULL DEFAULT 0, s varchar(16) NOT NULL DEFAULT '', index k(k)) engine=InnoDB;
insert into T values(100,1, 'aa'),(200,2,'bb'),(300,3,'cc'),(500,5,'ee'),(600,6,'ff')
In execution select * from t where k between 3 and 5 The general process of :
- First, according to k Index tree found k=3 Index entry for
- And then depending on the corresponding ID value =300 Query the whole row of data back to the table
- And then k The index tree continues to look for the next value , Get next k=5 Index entry for
- obtain k=5 Corresponding ID=500, Query the whole row of data in the table again
- Continue to k Search the index tree for the next value , Find the next k=6 It doesn't meet the requirements , So stop querying , And the operation of returning to the table will not be performed .
As mentioned before : The data in the table is stored according to the data of the primary key , So when you need to query the data in the table, you must return to the table , So is there any situation that you don't need to go back to the table ?
Overlay index
If the above query statement is select ID from t where k between 3 and 5 , That is, you only need to query ID Value , You don't need a whole row of data , and ID The value of already exists in K On the index tree , So there is no need to return the form at this time .
Tree searches can be reduced by overwriting the index , Significantly improve query performance , So using overlay index is a common By means of performance optimization .
Suppose there is a citizen list :
CREATE TABLE `tuser` (
`id` int(11) NOT NULL,
`id_card` varchar(32) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`ismale` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_card` (`id_card`),
KEY `name_age` (`name`,`age`) ) ENGINE=InnoDB
Because the ID number can identify a person , Therefore, it is often necessary to use the ID number to query personal information .
Suppose there is a high frequency request , Is to use the ID card number to query the name ;
that sql Almost select name from tuser where id_card = xxxx; because name Not the primary key. , No existence id_card On the index tree , You need to go back to the table ;
In order to reduce the number of back tables , We will name and id Being a federated primary key is a little far fetched , Because it is impossible to use the fields required by each high-frequency request as the joint primary key .
So this time you can id_card and name Create a joint index ;
Index push down
Go one step further on the request for names , The query name contains the name of the table , The ID number is 32 start ;
select name from tuser where substring(id_card,1,2) = 32 and name like ‘% surface %’
stay mysql5.6 Before , On the basis of substring(id_card,1,2) = 32 Find eligible ID after , Will return to the table again , Inquire about name Field and then determine whether the word table is included ;
stay mysql5.6 after , On the basis of substring(id_card,1,2) = 32 Find eligible ID after , It will directly judge name Whether the field contains the word table ;
Leftmost prefix principle
Based on the results of the above joint index , Then, if you still check the vaccination status according to your ID card number , Check the loan times and other background according to the ID card number , In order to reduce the operation of returning tables , Do we all have a joint index , There are too many indexes ;
You can see that there is already a (name,age) The index of :

You can see that the index entries are sorted according to the field order defined by the index .
It's just (name,age) This joint index is name before ,age After that ; So the index entries are all name before ,age After ; Index entries are based on name Sort , And then based on age Sort , because name= Zhang San has more than one , So we will follow the internal rules age Sort ;
So when we execute select Number of loans from table where name like '% Spongebob %' When , Also can walk (name,age) This index ;
But if it is execution select Number of loans from table where age >40 I won't go (name,age) This index ;
This is the leftmost prefix principle , As long as the leftmost prefix principle is satisfied , You can use indexes to speed up queries .
The joint index is (a,b); But there are only b The limitations of , You won't use the union index , If you want to go to the index at this time , You have to maintain a separate (b) The index of ;
But if the union index is (b,a), There are only... In the query criteria b The limitations of , That is to say, they will query , You can maintain one index less , Save space .
If by adjusting the order , can The first Maintain one index less , So this order is often the one that needs to be prioritized .
边栏推荐
- mysql整体架构和语句的执行流程
- Gold three silver four, an article to solve the resume and interview
- What are some tricks that novice programmers don't know?
- Read the configuration, explain the principle and read the interview questions. I can only help you here...
- Principle analysis of ThreadLocal source code
- Summary of 2022 spring moves of ordinary people (Alibaba and Tencent offer)
- _ 19_ IO stream summary
- Preliminary understanding of JVM
- Activation and value transfer of activity
- Sleep formula: how to cure bad sleep?
猜你喜欢

DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection翻译

GridLayout evenly allocate space

10款超牛Vim插件,爱不释手了
Create raspberry PI image file of raspberry pie

Xinlou: Huawei's seven-year building journey of sports health

Overall MySQL architecture and statement execution process

Read the configuration, explain the principle and read the interview questions. I can only help you here...

In the wechat environment, H5 jumps to the specified page of the applet

Reverse series to obtain any wechat applet code

Multiple decorators decorate a function
随机推荐
Advanced SQL statement 1 of Linux MySQL database
_ 19_ IO stream summary
Deadlock, thread communication, singleton mode
2021, committed to better development
Continuously improve the overall performance of adaoracle Oracle Oracle
揭秘GaussDB(for Redis):全面对比Codis
Practice of geospatial data in Nepal graph
Collection overview, array encapsulation
leetcode-8. String to integer (ATOI)
Describe your understanding of the evolution process and internal structure of the method area
Prototype mode
一行代码可以做什么?
Day21 multithreading
教务系统开发(PHP+MySQL)
Tensorflow loading cifar10 dataset
Go language - lock operation
The style of the mall can also change a lot. DIY can learn about it!
Consumer and producer cases of inter thread synchronization (condition variable)
Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
Summary of 2022 spring moves of ordinary people (Alibaba and Tencent offer)