当前位置:网站首页>联合索引的左匹配原则
联合索引的左匹配原则
2022-07-06 06:08:00 【雪峰.贵】
联合索引:
key : ‘index_name_age’(‘name’,‘age’)
场景:
case1:
select * from person_info where name='gxf' and age = '18';
此时会走 index_name_age 联合索引。
case2:
select * from person_info where name='gxf' ;
此时会走 index_name_age 联合索引。
case3:
select * from person_info where age = '18';
此时不会走 index_name_age 联合索引。会全表扫描。
注意
1.最左前缀 匹配原则
mysql会一直向右匹配 直到遇到 > 、< 、 between 、like 就停止匹配。
where a=1 and b=2 and c=3 and d>4;
如果建立 index(a,b,d,c) 则c是用不到索引的。
如果建立 index(a,b,c,d) 则都可用到索引。
如果建立 index(b,c,a,d) 则都可用到索引。
2.= 和 in
可以乱序
如果建立 index(a,b,c,d)。
where a=1 and b=2 and c=3 and d in(4,5);
where a=1 and d in(4,5) and b=2 and c=3 ;
都可走索引,因为mysql会优化你的SQL把顺序调整成可以使用索引的形式。
为什么case3不走联合索引:
假设:现在有个联合索引 index(col3,col2);
则,联合索引文件里就会存储一个B+树,叶子节点会存储两个Alice的行数据,并且会在此基础上,再根据col2排序。最终结果:34的Alice排在了77的Alice的前面。
现在 where col3 = ‘Alice’ and col2=34; 则会先找到叶子节点,再去找34。即走了索引。
若 where col2 = 34;则这个联合索引文件存的B+树就用不了了。即不能走索引。
边栏推荐
- LeetCode 729. 我的日程安排表 I
- [Thesis code] SML part code reading
- 【C语言】字符串左旋
- Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
- Nodejs realizes the third-party login of Weibo
- Configuring OSPF GR features for Huawei devices
- Detailed explanation of BF and KMP
- Huawei BFD configuration specification
- 还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
- Amazon Engineer: eight important experiences I learned in my career
猜你喜欢
Postman核心功能解析-参数化和测试报告
P问题、NP问题、NPC问题、NP-hard问题详解
nodejs实现微博第三方登录
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
Application of Lie group in gtsam
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
【eolink】PC客户端安装
(中)苹果有开源,但又怎样呢?
[ram IP] introduction and experiment of ram IP core
随机推荐
J'ai un chaton.
单元测试的意义
LeetCode 739. 每日温度
[web security] nodejs prototype chain pollution analysis
[paper reading] nflowjs: synthetic negative data intensive anomaly detection based on robust learning
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
A complete collection of necessary learning websites for office programmers
Overview of three core areas of Mathematics: algebra
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
Understanding of processes and threads
对数据安全的思考(转载)
Manhattan distance sum - print diamond
ESP32 ESP-IDF看门狗TWDT
【Postman】动态变量(也称Mock函数)
IPv6 comprehensive experiment
Cognitive introspection
Testing and debugging of multithreaded applications
Database: ODBC remote access SQL Server2008 in oracel
[C language syntax] the difference between typedef struct and struct
Leaflet map