当前位置:网站首页>mysql - 索引
mysql - 索引
2022-07-17 05:08:00 【HjasnJH】
索引的分类
主键索引、唯一索引、普通索引、组合索引、以及全文索引;
主键索引
非空唯一索引,一个表只有一个主键索引;在 innodb 中,主键索引的B+树包含表数据信息;
PRIMARY KEY(key)唯一索引
不可以出现相同的值,可以有NULL值;
UNIQUE(key)普通索引
允许出现相同的索引内容;
INDEX(key)
KEY(key[,...])组合索引
对表上的多个列进行索引
INDEX idx(key1,key2[,...]);
UNIQUE(key1,key2[,...]);
PRIMARY KEY(key1,key2[,...]);全文索引
将存储在数据库当中的整本书和整篇文章中的任意内容信息查找出来的技术;关键词 FULLTEXT;
在短字符串中用 LIKE % ;在全文索引中用 match 和 against ;
主键选择
innodb 中表是索引组织表,每张表有且仅有一个主键;
1. 如果显示设置 PRIMARY KEY ,则该设置的key为该表的主键;
2. 如果没有显示设置,则从非空唯一索引中选择;
2.1、只有一个非空唯一索引,则选择该索引为主键;
2.2、有多个非空唯一索引,则选择声明的第一个为主键;
3. 没有非空唯一索引,则自动生成一个 6 字节的 _rowid 作为主键;
索引实现
存储结构
innodb存储引擎使用的是段区页的存储模式,如下

其中页是磁盘管理的最小单位,默认16k,区的大小为1M
每个页都是一个B+树的节点,如下,非叶子节点存放的是索引信息,每个页大小16kb,假设key和指针占用16字节,叶子节点占用100字节,则3层的B+树可以存放 16kb/16 * 16kb/16 * 160 = 1.6亿条,可以看出,B+树的高度一般在2-4层

聚集索引
是一颗B+树,按照主键构建的B+树,叶子节点存放数据页,数据也是索引的一部分

如上图,如要获取主键id从18-40的数据的B+树也就是磁盘的访问遍历顺序
辅助索引
不是主键创建的索引都是辅助索引,辅助索引也是一棵B+树,其叶子节点不包含行记录的所有数据,只包含聚集索引的key,如下图,假设查找33,则其对应的主键的key是47,也就是要到聚集索引中查找key为47的值。

覆盖索引
对于在辅助索引就能找到的数据,就不需要再到聚集索引中查找,因为辅助索引的树的高度一般是比聚集索引的树低,减少了IO的访问次数。
比如,表1中age是主键
age,name 是组合索引
当查找age=10时,就会使用辅助索引直接查找,而不使用age索引。
组合索引最左匹配原则
对于组合索引,从左到右依次匹配,遇到 > < between like 就停止匹配;
索引失效
1、select ... where A and B 若 A 和 B 中有一个不包含索引,则索引失效;
2、索引字段参与运算,则索引失效;例如: from_unixtime(idx) = '2021-04-30';
3、索引字段发生隐式转换,则索引失效;例如: '1' 隐式转换为 1 ;
4、LIKE 模糊查询,通配符 % 开头,则索引失效;
例如: select * from user where name like'%ark';
5、在索引字段上使用 NOT <> != 索引失效;如果判断 id <> 0 则修改为 idx > 0 or idx < 0 ;
6、组合索引中,没使用第一列索引,索引失效;
边栏推荐
- Pat class B 1002: write this number
- [ES6] explain in detail the common objects and other methods of set and array (full version)
- uniapp 使用uview实现折叠面板
- Shell脚本配置root免密登录到其他主机
- Two methods of rotation chart and automatic rotation
- Leetcode53. maximum subarray and
- Excel template export of easypoi
- Es6最新常用知识宝典(能够帮助你解决面试题困惑,编写程序中出现的问题等)
- Use echars to realize water drop, ring, segmentation, stacking, organization chart, map outline and other charts
- Shell script configures root to login to other hosts without secret
猜你喜欢

微信小程序5-基础加强(没写完)
![[AI] action recognition using simple neural network -- Based on coco key points](/img/67/cd6be6e070fb5d4d44ee043ebd7fac.png)
[AI] action recognition using simple neural network -- Based on coco key points

uniapp中使用ucharts图表,饼状图,柱状图,折线图

Uni app conditional compilation ifdef ENDIF compatible with multiple terminals

微信小程序 学习笔记

Excel template export of easypoi

小程序editor富文本编辑使用及rich-text解析富文本

基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题

ECS deployment web project

Cesium BIND Mouse Events and remove Mouse Events
随机推荐
uni-app 条件编译#ifdef #endif 兼容多个终端
Get the multi-functional version of the maximum and minimum values of the internal values of the objects in the array and the full version of the roll call system, and show the effect
轮播图节流阀原理及本地存储归总
手把手教你复现Log4j2核弹级漏洞
[ES6] explain in detail the common objects and other methods of set and array (full version)
Pat class B 1002: write this number
Flex flexible layout
Cesium 获取鼠标点击处经纬度的三种方法
Applet cloud development upload pictures to cloud storage
Using JS to realize the second level menu of anjuke and the full version (demonstration of precautions and problem points)
Web3js development technology
循环赛制日程表问题
Use (offset, page) in JS to achieve login effect
Leetcode53. maximum subarray and
指针进阶简单总结
[AI] action recognition using simple neural network -- Based on coco key points
Pointer advanced simple summary
ES6 latest commonly used knowledge dictionary (which can help you solve interview questions, problems in programming, etc.)
使用js中的(offset,page)实现登录效果
RK356x U-Boot研究所(命令篇)3.4 mem内存相关命令的用法