当前位置:网站首页>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 !
边栏推荐
- C#入门系列(十二) -- 字符串
- 硬盘 SMART 检测参数详解
- Jetpack架构组件学习(3)——Activity Results API使用
- DNA数字信息存储的研究进展
- 简单介绍线程和进程区别
- 《第五项修炼》读书笔记
- Summary of APP test interview questions, which must be taken in the interview
- Principle analysis of mongodb storage engine wiredtiger
- 5 most common CEPH failure scenarios
- 《保护我们的数字遗产:DNA数据存储》白皮书发布
猜你喜欢

科创人·神州数码集团CIO沈旸:最佳实践模式正在失灵,开源加速分布式创新
Do you know how to improve software testing ability?
![[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)](/img/25/c3af3f51c04865820e3bbe2f010098.png)
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)

QQ, wechat chat depends on it (socket)?

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000

Principle analysis of mongodb storage engine wiredtiger

List of computer startup shortcut keys

链式哈希表

How CEPH improves storage performance and storage stability

7-13 地下迷宫探索(邻接表)
随机推荐
UEFI EDKII 编程学习
UE4_以现成资源探索创建背景场景的方法
科创人·神州数码集团CIO沈旸:最佳实践模式正在失灵,开源加速分布式创新
Test case and bug description specification reference
Introduction to applet
Jetpack架构组件学习(3)——Activity Results API使用
There is always a negative line (upper shadow line) that will stop the advance of many armies, and there is always a positive line (lower shadow line) that will stop the rampant bombing of the air for
7-4 网红点打卡攻略(dfs)
NiO principle
Record and store user video playback history selection
Is it necessary to separate databases and tables for MySQL single table data of 5million?
MySQL-MVCC
Tap series article 3 | introduction to Tanzu application platform deployment reference architecture
六月集训(第12天) —— 链表
TAP 系列文章3 | Tanzu Application Platform 部署参考架构介绍
ThreadLocal
Combat tactics based on CEPH object storage
科创人·世界500强集团CIO李洋:数字化转型成事在人,决策者应时刻聚焦于「柴」
Storage R & D Engineer Recruitment
ADB command collection, let's learn together