当前位置:网站首页>MySQL 45 talk | 05 explain the index in simple terms (Part 2)
MySQL 45 talk | 05 explain the index in simple terms (Part 2)
2022-06-28 05:24:00 【Blue cotton】
MySQL 45 speak | 05 Index in simple terms ( Next )
I didn't understand it at first ,B The station looked for two videos and thought it was very good , You can learn from the article .
Overlay index ( Joint index ) Optimize back to table query 、 Leftmost prefix principle
What is? MySQL Index push down ?
Raise questions
Here's the watch T in , If I execute
select * from T where k between 3 and 5
, You need to perform several tree searches Cable operation , How many lines will be scanned ?
mysql> create table T (
ID int primary key,
k int NOT NULL DEFAULT 0,
s varchar(16) NOT NULL DEFAULT '',
index k(k)) engine=InnoDB;
- InnoDB The index structure of :
This article SQL The execution process of query statement :
select * from T where k between 3 and 5
- stay k Found on index tree k=3 The record of , obtain ID = 300;
- Until then ID Index tree found ID=300 Corresponding R3;
- stay k Index tree takes next value k=5, obtain ID=500;
- Back to ID Index tree found ID=500 Corresponding R4;
- stay k Index tree takes next value k=6, Not meeting the conditions , The loop ends .
In the process , Go back to the process of searching the primary key index tree , We call it Back to the table . You can see , This query process read k Of the index tree 3 Bar record ( step 1、3 and 5), I went back to my watch twice ( step 2 and 4).
Because the data required for query results Only on the primary key index , So I have to go back to my watch . that , Is there any
Possibly index optimized , Avoid the return process ?
Overlay index
In this query , Indexes k already “ covers ” Our query needs , We call it Overlay index ( Joint index ).
Overlay index Can reduce the number of tree searches , Significantly improve query performance , Is a common means of performance optimization .
problem : On a citizen information sheet , Is it necessary to identify On a citizen information sheet , Is it necessary to identify
The certificate number and name are established Joint index ?
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
Build another one ( ID number 、 full name ) Joint index of , Is it a waste of space ?
If there is one now High frequency requests , He should check his name according to the ID number of the citizen. , This Joint index It makes sense . It can use overlay index on this high frequency request , No need to go back to the table Check the whole line of records , Reduce statement execution time .
Of course , The maintenance of index fields always comes at a cost , A trade-off is needed when building redundant indexes to support coverage indexes .
Leftmost prefix principle
Create a separate one for an infrequent request ( ID number , Address ) The index is a bit wasteful . What should be done ?
B+ Trees This index structure , Available with index “ Left most prefix ”, To locate records .
use (name,age) This joint index is used to analyze :
- (name,age) Index diagram :
You can see , Index entry Is the field that appears in the index definition Order Of .
- When your logical need is to find out all the names are “ Zhang San ” When people are , Can quickly locate ID4, Then go back and get all the results you need .
- If you're looking up all the names, the first word is “ Zhang ” People who , Yours SQL The condition of the statement is "where name like ‘ Zhang %’". At this time , You can also use this index , The first eligible record found is ID3, And then go back through , Until the conditions are not met .
This Left most prefix It can be Joint index Of Leftmost left N A field , It can also be String index Of Leftmost left M Characters .
problem : When building a federated index , How to arrange When building a federated index , How to arrange the order of the fields in the index .
The evaluation criteria are , Index reusability .
The first principle is , If by adjusting the order , One less index can be maintained , So this order is often the one that needs to be prioritized .
There are only b The sentence of , Can't use (a,b) This joint index .( The leftmost prefix is in order )
The second principle is space .
Index push down
Those parts that don't match the leftmost prefix , What will happen ?
Or is it based on the joint index of citizen table (name, age) For example . If there is a need now : Search out the “ First name A word is Zhang , And the age is 10 All the boys at the age of ”.
that ,SQL This is how the statement is written :
mysql> select * from tuser where name like ' Zhang %' and age=10 and ismale=1;
I already know Prefix index The rules , So this statement is used to search the index tree , Only use “ Zhang ”, Find the first satisfaction Record of conditions ID3. Of course , It's not bad , Better than full scan .
Then judge whether other conditions are met .
stay MySQL 5.6 Before , Only from ID3 Start back to watch one by one . Find the data row on the primary key index , Then compare the field values .
MySQL 5.6 Introduced Index push down Optimize (index condition pushdown), During index traversal , Opposite rope The fields contained in the citation are judged first , direct Filter Delete records that do not meet the conditions , Reduce the number of times to return to the table .
- No index push down execution process :
- Index push down execution process :
In these two pictures , Each dashed arrow indicates Back to the table once .
chart 3 in , stay (name,age) I took it out of the index age Value , This process InnoDB Not going to see age Value , Just put... In order “name The first word is ’ Zhang ’” Take out the records one by one and return them to the table . therefore , need Back to the table 4 Time .
chart 4 Follow chart 3 Is the difference between the ,InnoDB stay (name,age) Inside the index, we judge age Is it equal to 10, For not equal to 10 Of Record , Judge directly and skip . In our case , Only need to ID4、ID5 These two records get data from the table break , Just need Back to the table 2 Time .
Summary
- Overlay index
- Prefix index
- Index push down
- One of the important principles of database design is to access resources as little as possible .
边栏推荐
- 二级造价工程师考试还没完?还有资格审核规定!
- BioVendor sRAGE Elisa试剂盒化学性质和技术研究
- When using the MessageBox of class toplevel, a problem pops up in the window.
- Biovendor sRAGE protein solution
- How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
- CPG 固体支持物研究:Lumiprobe通用 CPG II 型
- Wireless sensor network learning notes (I)
- Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
- 公司为什么选择云数据库?它的魅力到底是什么!
- [JVM] - memory partition in JVM
猜你喜欢
活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
[untitled] drv8825 stepping motor drive board schematic diagram
Operation of simulated examination platform of G3 boiler water treatment recurrent training question bank in 2022
[JVM] - Division de la mémoire en JVM
2022 high altitude installation, maintenance and removal examination questions and answers
开关电源电压型与电流型控制
Store inventory management system source code
【LeetCode】12、整数转罗马数字
分享|智慧环保-生态文明信息化解决方案(附PDF)
BioVendor sRAGE蛋白解决方案
随机推荐
mysql 导出查询结果成 excel 文件
If a programmer goes to prison, will he be assigned to write code?
PMP考试成绩多久出来?这些你务必知道!
[leetcode] 12. Integer to Roman numeral
Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
mysql导出数据库字典成excel文件
OpenSSL client programming: SSL session failure caused by an obscure function
msa. h: There is no such file or directory
证明素数/质数有无限多个
Object detection with OpenCV
? How to write the position to output true
Wireless sensor network learning notes (I)
Docker installs mysql5.7 and starts binlog
基于订单流工具,我们能看到什么?
metaRTC5.0 API编程指南(一)
Docker安装Mysql5.7并开启binlog
How long will the PMP test results come out? You must know this!
109. simple chat room 12: realize client-side one-to-one chat
Extjs图书管理系统源码 智能化图书管理系统源码
禁用右击、键盘打开控制台事件