当前位置:网站首页>联合索引的左匹配原则
联合索引的左匹配原则
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+树就用不了了。即不能走索引。
边栏推荐
- Luogu p1460 [usaco2.1] healthy Holstein cows
- Arrays and collections
- [Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS
- How Huawei routers configure static routes
- selenium源码通读·9 |DesiredCapabilities类分析
- Application du Groupe Li dans gtsam
- Thoughts on data security (Reprint)
- Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
- 2022 software testing workflow to know
- 【无标题】
猜你喜欢
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
C language bubble sort
进程和线程的理解
(中)苹果有开源,但又怎样呢?
[postman] collections configuration running process
LeetCode 732. 我的日程安排表 III
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
The difference and usage between continue and break
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
【微信小程序】搭建开发工具环境
随机推荐
[eolink] PC client installation
Software test interview questions - Test Type
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
【Postman】Monitors 监测API可定时周期运行
LeetCode 739. 每日温度
Sqlmap tutorial (III) practical skills II
Application of Lie group in gtsam
SQLMAP使用教程(三)实战技巧二
误差的基本知识
【eolink】PC客户端安装
请求转发与重定向
P问题、NP问题、NPC问题、NP-hard问题详解
GTSAM中李群的运用
H3C S5820V2_ Upgrade method after stacking IRF2 of 5830v2 switch
Company video accelerated playback
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
(5) Explanation of yolo-v3 core source code (3)
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
假设检验学习笔记
PAT(乙级)2022年夏季考试