当前位置:网站首页>B+树索引使用(7)匹配列前缀,匹配值范围(十九)
B+树索引使用(7)匹配列前缀,匹配值范围(十九)
2022-07-26 12:55:00 【用户9919783】
上篇文章索引的代价,b+树占的空间比较大,增删改对b+树每个节点的索引排序影响也很大,时间耗费长,所以没有必要不要乱建索引,还介绍了索引的最左原则和全值查询。
B+树索引使用(6)最左原则 --mysql从入门到精通(十八)
匹配列前缀
innoDB给其他列添加二级索引,会按列给他排序,不管是页之间的双向链表排序,还是页内数据槽点的单向列表排序,都是按列值排的。比如name列,如果名字有TOM,Anny,Aone等,1)先按首字母给他排序。2)首字母都是A一样,则按第二个字母给他排序。3)依次类推,一样就用后面一直给他排序。所以我们用模糊查询的时候where name like ‘a%’;这种百分比号放在后面是可以触发索引查询的,但若是where name like ‘%a’;这种因为第一个字母找不到,这种没有排序好,导致只能全盘扫描,放弃索引查询。
举个例子,当我们有字段列url,分别有数据,www.baidu.com,www.google,www.gov.com,www.wto.com。
这时候我们想查询com结尾的,如果用where url like ‘%com’;则效率会非常低,无法触发索引查询。那我们为了提高效率,用索引来查询数据,可以先把表的数据逆排序一下,改为,moc.udiab.www,moc.elgoog.www,nc.vog.www,gro.otw.www,这时候就可以用where url like ‘com%’;来触发索引查询。
匹配值范围
我们看idx_name_birthday_phone索引b+示意图,所有记录都是按索引从小到大进行排序的,比如我们用where name > ‘Anny’ and name < ‘barlow’;所以这个查询过程:1)先在b+树叶子节点找到name值大于Anny的二级索引记录,读取主键,在用聚簇索引进行回表查询操作,获取聚簇索引的全部用户记录数据后发给客户端。2)根据上一步找到的记录,沿着所在的链表位子(页之间双向链表关联,页内数据,单向链表关联)查找下一条二级索引记录,判断该记录是否<barlow,如果符合,则返回给客户端。3)重复2的操作,直到不符合。
所以,这时候会使用索引查询的,但重点需要注意,注意,注意(重要的事要说三遍):如果对多个列进行范围查询,只有索引最左边的那个列查询时候会使用到b+树的索引进行查询。
比如where name > ‘Anny’ and name < ‘barlow’and birthday > ‘1990-01-01’;这时候查询过程:1)和前面说的一样,先name在b+树叶子节点找到列>Anny的主键,用主键查询聚簇索引的树回表操作,查询完之后,在查询下一个是否满足<barlow。2)对于上面name不同的值,在进行birhday进行birthday>’1990-01-01’过滤。
这样查询对于联合索引来说,只会name的时候用到了索引排序,而因为biryhday排序的条件是需要先name排序相同才会排序,此刻获取的name都是不同的,所以在birthday范围查询的时候无法触发索引。
精确到某一列,范围另外一列
当我们用where name = ‘Anny’ and birthday > ‘1990-01-01’ and phone > ‘13200000000’;这时候我们查询过程就是:1)name肯定使用b+树的二级索引先查询到叶子节点的列值加主键,再聚簇索引回表操作返回聚簇索引叶子节点的全部数据。2)因为name相同的情况下,birthday会触发索引查询,先在b+树叶子节点找到>’1990-01-01’的列值和主键,在通过主键回表查询全部数据3)因为phone使用索引查询的前提是birthday相同,而前面的是不同的birthday,索引phone不能使用索引查询。
边栏推荐
- [5g] what are Cu and Du in 5g?
- Use positioning to realize left, middle and right layout, and the middle content is adaptive
- Huawei recruited "talented teenagers" twice this year; 5.4 million twitter account information was leaked, with a selling price of $30000; Google fired engineers who believed in AI consciousness | gee
- Sword finger offer (21): push in and pop-up sequence of stack
- Solution: unable to load the file c:\users\user\appdata\roaming\npm\npx PS1, because running scripts is prohibited on this system.
- Streamnational team culture: a "transparent" company
- C#把Type当做泛型T,来作为方法的泛型进行使用
- 基于C#开放式TCP通信建立与西门子PLC的socket通信示例
- 一款超好用的神器Apifox,甩 Swagger 几条街...(荣耀典藏版)
- B+树索引使用(8)排序使用及其注意事项(二十)
猜你喜欢

牛客刷SQL---2
![[applet] why can't the onreachbottom event be triggered? (one second)](/img/da/3641040c63f6db4d227dcf2ff89919.png)
[applet] why can't the onreachbottom event be triggered? (one second)

jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?

V00 - do whatever you want when you are old

Food safety | these common foods are poisonous! Check your dining table quickly

Kubelet CRI 容器运行时

高通再次「押宝」中科创达,挑战智能驾驶软硬件全栈方案
![[typescript] typescript common types (Part 1)](/img/80/5c8c51b92d3a9d76f38beba7be0aa6.png)
[typescript] typescript common types (Part 1)

维度灾难 维数灾难 暂记

华为超融合FusionCube解决方案笔记
随机推荐
子组件触发父组件自定义事件(defineEmits):子组件传值给父组件
Slam 02. overall framework
1-6月中国ADAS供应商占比9% 又一家零部件巨头全面布局智驾新赛道
Flutter prevents scientific counting and removes mantissa invalid 0
PXE原理与配置
Streamnational team culture: a "transparent" company
基于Bézier曲线的三维造型与渲染
基于C#实现的学生考试系统
Flink is slow to write redis. Do you have any ideas for optimization?
高通再次「押宝」中科创达,挑战智能驾驶软硬件全栈方案
Display inline+calc realizes left, middle and right layout, and the middle is adaptive
Examples of ThreadLocal usage scenarios under multithreading
LCD notes (5) LCD driver framework_ Use device tree
Detailed explanation of redis's single login
被罚“带薪休假”一个月后,谷歌解雇了“爱”上 AI 的他
Px2rem loader converts PX into REM and adapts to mobile vant UI and other frameworks
关于图片地址链接复制问题
如何面对科技性失业?
Bitwise and how to write SQL
The "2022 Huawei developer competition eastern China division opening ceremony" was successfully held in Fuzhou