当前位置:网站首页>MySql索引
MySql索引
2022-06-12 23:48:00 【InfoQ】
索引
- 提高数据检索效率,降低数据库的IO成本
- 通过索引列对数据进行排序,降低数据排序的成本,降低CPU的消耗
- 索引列也是要占用空间的
- 索引大大提高了查询效率,但降低了更新的速度,比如 INSERT、UPDATE、DELETE
索引结构
B-Tree



B+Tree

- 所有的数据都会出现在叶子节点
- 叶子节点形成一个单向链表

Hash

- Hash索引只能用于对等比较(=、in),不支持范围查询(betwwn、>、<、...)
- 无法利用索引完成排序操作
- 查询效率高,通常只需要一次检索就可以了,效率通常要高于 B+Tree 索引
- Memory
- InnoDB: 具有自适应hash功能,hash索引是存储引擎根据 B+Tree 索引在指定条件下自动构建的
面试题
- 为什么 InnoDB 存储引擎选择使用 B+Tree 索引结构?
- 相对于二叉树,层级更少,搜索效率高
- 对于 B-Tree,无论是叶子节点还是非叶子节点,都会保存数据,这样导致一页中存储的键值减少,指针也跟着减少,要同样保存大量数据,只能增加树的高度,导致性能降低
- 相对于 Hash 索引,B+Tree 支持范围匹配及排序操作
索引分类


- 如果存在主键,主键索引就是聚集索引
- 如果不存在主键,将使用第一个唯一(UNIQUE)索引作为聚集索引
- 如果表没有主键或没有合适的唯一索引,则 InnoDB 会自动生成一个 rowid 作为隐藏的聚集索引
思考题
select * from user where id = 10;
select * from user where name = 'Arm';
-- 备注:id为主键,name字段创建的有索引
n * 8 + (n + 1) * 6 = 16 * 1024
1171 * 16 = 18736
1171 * 1171 * 16 = 21939856
语法
CREATE [ UNIQUE | FULLTEXT ] INDEX index_name ON table_name (index_col_name, ...);
SHOW INDEX FROM table_name;
DROP INDEX index_name ON table_name;
-- name字段为姓名字段,该字段的值可能会重复,为该字段创建索引
create index idx_user_name on tb_user(name);
-- phone手机号字段的值非空,且唯一,为该字段创建唯一索引
create unique index idx_user_phone on tb_user (phone);
-- 为profession, age, status创建联合索引
create index idx_user_pro_age_stat on tb_user(profession, age, status);
-- 为email建立合适的索引来提升查询效率
create index idx_user_email on tb_user(email);
-- 删除索引
drop index idx_user_email on tb_user;
使用规则
最左前缀法则
索引失效情况
- 在索引列上进行运算操作,索引将失效。如:
explain select * from tb_user where substring(phone, 10, 2) = '15';
- 字符串类型字段使用时,不加引号,索引将失效。如:
explain select * from tb_user where phone = 17799990015;
,此处phone的值没有加引号
- 模糊查询中,如果仅仅是尾部模糊匹配,索引不会是失效;如果是头部模糊匹配,索引失效。如:
explain select * from tb_user where profession like '%工程';
,前后都有 % 也会失效。
- 用 or 分割开的条件,如果 or 其中一个条件的列没有索引,那么涉及的索引都不会被用到。
- 如果 MySQL 评估使用索引比全表更慢,则不使用索引。
SQL 提示
explain select * from tb_user use index(idx_user_pro) where profession="软件工程";
explain select * from tb_user ignore index(idx_user_pro) where profession="软件工程";
explain select * from tb_user force index(idx_user_pro) where profession="软件工程";
覆盖索引&回表查询
using index condition
using where; using index;
select id, name from xxx where name='xxx';
select id, name, gender from xxx where name='xxx';
select *
select id, username, password from tb_user where username='itcast';
前缀索引
create index idx_xxxx on table_name(columnn(n));
select count(distinct email) / count(*) from tb_user;
select count(distinct substring(email, 1, 5)) / count(*) from tb_user;
单列索引&联合索引
explain select id, phone, name from tb_user where phone = '17799990010' and name = '韩信';
注意事项
- 多条件联合查询时,MySQL优化器会评估哪个字段的索引效率更高,会选择该索引完成本次查询
设计原则
- 针对于数据量较大,且查询比较频繁的表建立索引
- 针对于常作为查询条件(where)、排序(order by)、分组(group by)操作的字段建立索引
- 尽量选择区分度高的列作为索引,尽量建立唯一索引,区分度越高,使用索引的效率越高
- 如果是字符串类型的字段,字段长度较长,可以针对于字段的特点,建立前缀索引
- 尽量使用联合索引,减少单列索引,查询时,联合索引很多时候可以覆盖索引,节省存储空间,避免回表,提高查询效率
- 要控制索引的数量,索引并不是多多益善,索引越多,维护索引结构的代价就越大,会影响增删改的效率
- 如果索引列不能存储NULL值,请在创建表时使用NOT NULL约束它。当优化器知道每列是否包含NULL值时,它可以更好地确定哪个索引最有效地用于查询
边栏推荐
- Find out the data that can match the keyword key in field 1 or field 2 in the database table. If you want to display the matching data in field 1 first
- VS2015 DLIB 1916 USER_ ERROR__ inconsistent_ build_ configuration__ see_ dlib_ faq_ 1 USER_ ERROR__ inconsiste
- Enterprise wechat H5_ Authentication, PC website, enterprise wechat scanning code, authorized login
- Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity
- Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022
- Leetcode 2164. Sort odd and even subscripts separately (yes, once)
- 【Matlab】矩阵
- Buuctf-[ciscn 2019 preliminary]love math
- Is the PMP training organization an actual training?
- 【Matlab】多项式计算
猜你喜欢
支持Canvas的Leaflet.Path.DashFlow动态流向线
Case sharing of online real queuing system reconfiguration -- practical part
AWS lambda: how to store secrets to external APIs- AWS Lambda: How to store secret to external API?
Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity
Day 3 of jsbom and DOM learning
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
2022 questions d'examen pour le personnel de gestion de la sécurité de l'unit é de gestion des produits chimiques dangereux et examen de simulation en ligne
【Matlab】矩阵
OSM地图本地发布-如何生成各省市矢量地图
Start of u-boot S analysis (III)
随机推荐
Talent Weekly - 5
Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022
TCP与UDP
Start of u-boot S analysis (IV)
利率降低导致债券价格上涨
Sequence maximum return
Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
How to pass the PMP review?
华为云会议初体验【华为云至简致远】
Summary of individual NLP internship experience
Mgr and greatsql resource summary
M_8:设计消息队列存储消息数据的 MySQL 表格
[SciPy optimization tutorial] v. quick solution of univariate function optimization
NCF 的Dapr应用实例的运行
【Matlab】矩阵变换与矩阵求值
【Matlab】多项式计算
如何让矢量瓦片配图神器maputnik支持 geoserver
Leetcode 2200. Find all k nearest neighbor subscripts in the array (yes, one pass)
How does idea switch the interface to Chinese
2022 operation of simulated examination platform for hoisting machinery command certificate