当前位置:网站首页>Explanation of the principle of MySQL's leftmost matching principle
Explanation of the principle of MySQL's leftmost matching principle
2022-06-12 09:52:00 【It mapper】
MYSQL | The principle of the leftmost matching principle
Left most matching principle
The leftmost matching principle is that in a union index , If your SQL The leftmost index in the union index is used in the statement , So this one SQL Statement can use the union index to match . For example, a performance has an index (a,b,c), Now you have the following sentence :
select * from t where a=1 and b=1 and c =1; # This can take advantage of the defined index (a,b,c), use a,b,c select * from t where a=1 and b=1; # This can take advantage of the defined index (a,b,c), use a,b select * from t where b=1 and a=1; # This can take advantage of the defined index (a,b,c), use a,c(mysql There are query optimizers ) select * from t where a=1; # You can also use the defined index (a,b,c), use a select * from t where b=1 and c=1; # You can't use the defined index (a,b,c) select * from t where a=1 and c=1; # This can take advantage of the defined index (a,b,c), But only a Indexes ,b,c I can't use the index
That is to say, through the leftmost matching principle, you can define a union index , But the index can be used in most query conditions . It is worth noting that , When it comes to range queries (>、<、between、like) Will stop matching . That is to say :
select * from t where a=1 and b>1 and c =1; # such a,b You can use (a,b,c),c I can't use the index
This sentence has only a,b You'll use the index ,c You can't use indexes . This can be explained by the structure of the union index . But if it's building (a,c,b) Joint index , be a,b,c You can use indexes , Because the optimizer will automatically rewrite to the optimal query statement
select * from t where a=1 and b >1 and c=1; # If it's building (a,c,b) Joint index , be a,b,c You can use indexes # The optimizer changes to select * from t where a=1 and c=1 and b >1;
This is also part of the leftmost prefix principle , Indexes index1:(a,b,c), I can only walk a、a,b、a,b,c Three types of queries , In fact, there is a problem here ,a,c Go too , But just go a Field index , Not going c Field .
In addition, there is a special case ,select * from table where a = '1' and b > ‘2’ and c='3' This type of one will only have a And b Go to the index ,c Not going .
select * from table where a = '1' and b > ‘2’ and c='3'
This type of sql sentence , stay a、b After going through the index ,c It must be out of order , therefore c You can't go to the index , database I think it's better to scan the whole table c Fields come fast .
With index (a,b,c) For example, building such an index is equivalent to building an index a、ab、abc Three indexes . One index top three indexes is certainly a good thing , After all, every additional index , Will increase the cost of write operations and disk space .
The principle of the leftmost matching principle
The leftmost matching principle is for federated indexes , So we can understand the leftmost matching principle from the principle of joint index .
We all know that the bottom of the index is a B+ Trees , Then, of course, the joint index is still a B+ Trees , It's just that the number of key values in a union index is not one , It's more than one. . Build a B+ Trees can only be built from one value , So the database is built on the leftmost field of the federated index B+ Trees . Example : If you create a (a,b,c) Joint index of , So its index tree is like this :
This is a picture of (a,b,c) Joint index b+ Trees , The non leaf node stores the index of the first keyword a, The leaf node stores the data of three keywords . You can see here a Is ordered , and b,c It's all out of order . But when a At the same time ,b Is ordered ,b At the same time ,c It's orderly again . Through the understanding of the structure of the joint index , Then you can understand why the leftmost matching principle will stop if you encounter range query . With select * from t where a=5 and b>0 and c =1; # such a,b You can use (a,b,c),c Can not be As an example , When found b After the value of ( This is a range value ),c Is chaotic . Therefore, it is impossible to determine which row to take according to the union index .
summary
stay InnoDB In the union index, only the previous one is determined first ( The value on the left side ) after , To determine the next value . If there is a range query , Then the index after the field of range query is used in the union index is in this article SQL It doesn't work . It is worth noting that ,in and = Can be disordered , For example, there is an index (a,b,c), sentence select * from t where c =1 and a=1 and b=1, Such a statement can also use the leftmost matching , because MySQL There is an optimizer in , He can analyze SQL sentence , Optimize it to a form that indexes can match , namely select * from t where a =1 and a=1 and c=1
Why use a union index
To reduce overhead . Build a union index (col1,col2,col3), It's actually equivalent to building (col1),(col1,col2),(col1,col2,col3) Three indexes . Every more index , Will increase the cost of write operations and disk space . For tables with large amounts of data , Using federated indexes can greatly reduce the cost !
Overlay index . On union index (col1,col2,col3), If there is one of the following sql: select col1,col2,col3 from test where col1=1 and col2=2. that MySQL You can get the data directly by traversing the index , No need to return the form , This reduces a lot of randomness io operation . Reduce io operation , Special random io It's actually dba The main optimization strategy . therefore , In real application , Coverage index is one of the main optimization methods to improve performance .
Efficient . More index columns , The less data you can filter through the index . Yes 1000W Table of data , There are the following sql:select from table where col1=1 and col2=2 and col3=3, Suppose that each condition can be screened out 10% The data of , If there is only one value index , Then we can filter out 1000W10%=100w Data , Then return to the table from 100w Match found in data col2=2 and col3= 3 The data of , And then sort , Page again ; If it's a joint index , Filter out... By index 1000w10% 10% *10%=1w, Efficiency improvement can be imagined !
边栏推荐
- 科创人·神州数码集团CIO沈旸:最佳实践模式正在失灵,开源加速分布式创新
- JVM virtual machine
- Microservice gateway
- Value investment
- III Regular expression to finite state automata: NFA to DFA
- QQ, wechat chat depends on it (socket)?
- 使用Visual Studio 2017创建简单的窗口程序
- Selection of interview questions for software testing
- Is it necessary to separate databases and tables for MySQL single table data of 5million?
- Do you know how to improve software testing ability?
猜你喜欢

How CEPH improves storage performance and storage stability

UE4_以现成资源探索创建背景场景的方法
There must be something you want to know about software testing experience sharing

Research progress of DNA digital information storage

001: what is a data lake?
Automated test learning path, come and learn

Implementation of fruit mall wholesale platform based on SSM

Autojs微信研究:微信不同的版本或模拟器上的微信里的控件ip是不同的。

Network layer IP protocol ARP & ICMP & IGMP nat

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
随机推荐
Dazzle the "library" action - award solicitation from the golden warehouse of the National People's Congress - high availability failover and recovery of kingbasees cluster
UEFI edkii programming learning
《第五项修炼》读书笔记
004:aws data Lake solution
Microservice gateway
How to write test cases?
使用Visual Studio 2017创建简单的窗口程序
Value investment
MySQL index
NiO principle
六月集训(第12天) —— 链表
Principle analysis of mongodb storage engine wiredtiger
榜样访谈——董宇航:在俱乐部中收获爱情
总有一根阴线(上影线)会阻止多军前进的脚步,总有一个阳线(下影线)会阻挡空军肆虐的轰炸
markdown_ Picture side by side scheme
端午節安康--諸佬在我心裏越來越粽要了
5 most common CEPH failure scenarios
PandoraBox 使用防火墙规则定义非上网时间
日本经济泡沫与房价泡沫
硬盘 SMART 检测参数详解