当前位置:网站首页>B+树索引使用(6)最左原则 --mysql从入门到精通(十八)
B+树索引使用(6)最左原则 --mysql从入门到精通(十八)
2022-07-26 12:55:00 【用户9919783】
上篇文章我们说了,myISAM表和innoDB表的不同,myISAM吧用户记录数据放在数据文件,会给每行数据一个行号,myISAM会给主键生成索引,吧索引页放在索引文件。B+树的叶子节点存储的是主键+行号,意味着第一次通过主键查询只能查到行号,之后回表,通过行号查询数据文件整行数据。而innoDB的聚簇索引不需要二次查询。
B+树(5)myISAM简介 --mysql从入门到精通(十七)
索引的代价
理解索引的原理之后,就会知道索引并不是没有缺点的。
1、空间上的代价:我们知道每个页都是16kb大小,而一颗b+树有每个节点都属于一个页,这样建立太多索引对空间内存占用非常大。
2、时间上的代价:索引会吧每个页按从小到大组成一个双向链表,内节点或者底层叶子节点里的数据也是按索引从小到大组成一个单向链表,这样每次进行增,删,改操作可能都会对节点排序和页记录进行破坏,所以如果建立太多索引,对表的增删改性能影响很大。
B+树索引使用场景
下面我们开始使用b+树索引,所有使用技巧都源于你对b+树索引特征本质的理解,如果你还不能理解前面的文章,建议你去读一遍,不然下面的文字对你来说是一种折磨,看了就会忘记。下面我们来介绍b+树索引的查询情况,先建立个表,存储人的基本信息,设置id为主键,这样innoDB会默认创年聚簇索引,在显示创建idx_name_birthday_phone为复合索引,所以在列b+树的叶子节点会有name,birthdate,phone和主键id,不会有country。
mysql> create table person_info(
-> id int not null auto_increment,
-> name varchar(100) not null,
-> birthday date not null,
-> phone char(11) not null,
-> country varchar(100) not null,
-> primary key (id),
-> key idx_name_birthday_phone (name,birthday,phone)
-> );
Query OK, 0 rows affected (0.05 sec)聚簇索引的叶子节点存储的是用户记录数据,因为我们创建了复合索引,复合索引的叶子节点存储的是name,birthday,phone,没有country,查询的时候:1)先按name排序。2)name相同按birthday排序。3)birthday也相同,按phone排序。
全值匹配查询
SELECT * FROM person_info WHERE name = '' AND birthday = '' AND phone = '';
//改成这样也不会有影响
SELECT * FROM person_info WHERE birthday = '' AND phone = '' AND name = '' ;当我们用如上sql时,就是全值匹配查询,不管用哪条sql,mysql的查询优化器,都会根据建立的联合索引查询,1)先查询name。2)name相同时候查询birthday。3)birthday也相同时候查询phone。
匹配最左原则
//sql1
SELECT * FROM person_info WHERE name = '' AND birthday = '';
//sql2
SELECT * FROM person_info WHERE name = '' ;
//sql3
SELECT * FROM person_info WHERE birthday = '' AND phone = '';
//sql4
SELECT * FROM person_info WHERE name = '' AND phone = '';当我们用sql1和sql2查询的时候,依旧可以触发联合索引来查询数据,但如果使用sql3是不能触发联合索引的,因为我们建立的索引是按name先排序比较,比较相同之后,采用birthday比较,但现在没有查询name,导致无法使用联合索引查询。我们用sql4也能触发索引查询,但不能用phone比较,原因与sql3相同,必须birthday比较完才可以比较phone排序。
边栏推荐
- PXE principle and configuration
- Detailed interpretation of hole convolution (input and output size analysis)
- 【TypeScript】TypeScript常用类型(上篇)
- Azure synapse analytics Performance Optimization Guide (2) -- optimize performance using materialized views (Part 1)
- The best engineer was "forced" away by you like this!
- 【5G】5G中的CU和DU是什么?
- 华为年内二度招聘“天才少年”;540万Twitter账号信息泄露,卖价3万美元;谷歌解雇了相信AI有意识的工程师|极客头条...
- Shutter background graying effect, how transparency, gray mask
- About the copy of picture address links
- After being fined "paid leave" for one month, Google fired him who "loves" AI
猜你喜欢

【上位机教程】CANopen通信下一体化步进电机与台达PLC(AS228T)的应用

如何面对科技性失业?

Analysis of Wireshark data package of network security B module of national vocational college skills competition Wireshark 0051.pcap

新功能 | 智能开放搜索上线定制词权重模型
![[5g] what are Cu and Du in 5g?](/img/5b/3453ade88ded4593edfcba9beb8ada.jpg)
[5g] what are Cu and Du in 5g?

食品安全 | 随便果可以”随便“吃吗?

Kubernetes - Introduction to PV and PVC of advanced storage

PXE principle and configuration

New function | intelligent open search online customized word weight model

被罚“带薪休假”一个月后,谷歌解雇了“爱”上 AI 的他
随机推荐
Redisson distributed lock usage example (I)
How to optimize a large number of if else [email protected] Detailed explanation of valib
LCD notes (5) LCD driver framework_ Use device tree
From January to June, China's ADAS suppliers accounted for 9%, and another parts giant comprehensively laid out the new smart drive track
手机上买股票,在哪里开户比较安全?
Flutter prevents scientific counting and removes mantissa invalid 0
A super easy-to-use artifact apifox, throw swagger a few streets... (glory Collection Edition)
[typescript] typescript common types (Part 1)
Database composition table
jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?
食品安全 | 微波炉什么食品都能加热?这些安全隐患要知道
mqtt send receive
Kubernetes - Introduction to PV and PVC of advanced storage
Kubelet CRI 容器运行时
食品安全 | 这些常见食物小心有毒!速查自家餐桌
Flink is slow to write redis. Do you have any ideas for optimization?
Commonly used list Why does isempty() suddenly report null?
C#把Type当做泛型T,来作为方法的泛型进行使用
牛客刷SQL---2
How to remove underline and color when there is focus in the shutter textfield