当前位置:网站首页>MySQL常见索引类型
MySQL常见索引类型
2022-08-02 07:19:00 【bisal(Chen Liu)】
关系型数据库中的索引,能够提升数据检索的效率,是提升性能的主要途径,GreatSQL开源社区推送的这篇《MySQL 常见索引类型介绍》,介绍了在MySQL中常见的几种索引。
MySQL 主要索引类型有如下几种,
(1) 主键索引。
(2) 唯一索引。
(3) 普通索引。
(4) 空间索引。
(5) 全文索引。
假设有如下一张表,
CREATE TABLE `t1` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`u1` int unsigned NOT NULL DEFAULT '0',
`u2` int unsigned NOT NULL DEFAULT '0',
`u3` varchar(20) NOT NULL DEFAULT '',
`u4` varchar(35) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB聚簇索引,
索引与数据存放在一起,找到索引的同时也找到了数据。
聚簇索引具有唯一性,一张表只有一个聚簇索引。
聚簇索引默认是主键,如果表中没有定义主键,InnoDB 会选择一个非空唯一索引代替。如果没有,InnoDB 会使用隐藏的_rowid列来作为聚簇索引。
非聚簇索引,
索引与数据分开存放,索引结构的叶子节点指向了数据的对应行。
主键索引,
# 查看创建的索引关键词
PRIMARY KEY (`id`)唯一索引,索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一,
# 创建唯一索引
mysql> alter table t1 add unique idx_u1(`u1`);
# 查看创建的索引关键词
UNIQUE KEY `idx_u1` (`u1`)单列索引,即索引创建在单个列上,
# 创建单列索引
mysql> alter table t1 add index idx_u1(`u1`);
# 查看创建的索引关键词
KEY `idx_u1` (`u1`)多列索引,创建多列索引时记得遵循最左匹配原则,将区分度较高的字段放前面,
# 创建多列索引
mysql> alter table t1 add index idx_u1_u2(`u1`,`u2`);
# 查看创建的多列索引关键词
KEY `idx_u1_u2` (`u1`,`u2`)前缀索引,即当字段的长度超过索引限制,可以为字段的部分前缀创建索引,
# 创建前缀索引,取字符串的前4个字符
mysql> alter table t1 add index idx_u3(`u3`(4));
# 查看创建的索引关键词
KEY `idx_u3` (`u3`(4))倒序索引,8.0版本出的新功能,以往创建倒序索引可以创建成功,但实际上仍然是顺序的,
# 5.7 创建倒序索引
mysql> select u1 from t1 limit 10;
+----+
| u1 |
+----+
| 12 |
| 23 |
| 12 |
| 34 |
+----+
10 rows in set (0.00 sec)
# 创建倒序索引
mysql> alter table t1 add index idx_u3(u3 desc);
# 实际查出来还是顺序
mysql> select u1 from t1 limit 10;
+----+
| u1 |
+----+
| 12 |
| 12 |
| 12 |
| 12 |
+----+
# 8.0 创建倒序索引
mysql> select u1 from t1 limit 5;
+----+
| u1 |
+----+
| 12 |
| 23 |
| 12 |
| 34 |
+----+
5 rows in set (0.00 sec)
# 添加索引
mysql> alter table t1 add index idx_u1(u1 desc);
# 查询倒序索引成功
mysql> select u1 from t1 limit 5;
+----+
| u1 |
+----+
| 74 |
| 74 |
| 74 |
| 74 |
+----+
5 rows in set (0.00 sec)函数索引,
# 创建函数索引
mysql> alter table t1 add index idx_abs_u2 ((ABS(`u2`)));
# 查看创建的索引关键词
KEY `idx_abs_u2` ((abs(`u2`)))表达式索引,
# 创建表达式索引
mysql> alter table t1 add index idx_u1u2 ((u1 + u2));
# 查看创建的索引关键词
KEY `idx_u1u2` (((`u1` + `u2`)))不可见索引,
# 设置不可见(invisible),修改为可见(visible)
mysql> ALTER TABLE t1 ALTER INDEX idx_u1 INVISIBLE;
# 查看不可见索引关键词
KEY `idx_u1` (`u1` DESC) /*!80000 INVISIBLE */
# 解析查询时时全表扫描
mysql> explain select * from t1 order by u1 desc limit 3;
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+----------------+
| 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 50184 | 100.00 | Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+----------------+
1 row in set, 1 warning (0.01 sec)
# 执行SQL时即便force index也不可用,提示不存在
mysql> explain select * from t1 force index(idx_u1) order by u1 desc limit 3;
ERROR 1176 (42000): Key 'idx_u1' doesn't exist in table 't1'空间索引,
空间索引是对空间数据类型的字段建立的索引,MYSQL使用SPATIAL关键字进行扩展,使其能够在空间数据类型的语法上创建空间索引。
# 建表
CREATE TABLE `gis_position` (
`id` int NOT NULL,
`gis` geometry NOT NULL COMMENT '空间位置信息',
`geohash` varchar(20) GENERATED ALWAYS AS (st_geohash(`gis`,12)) VIRTUAL,
primary key(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='空间位置信息';
# 创建空间索引
mysql> alter table gis_position add SPATIAL KEY `idx_gis` (`gis`) ;
# 查看索引关键信息
SPATIAL KEY `idx_gis` (`gis`)全文索引,
# 创建全文索引
mysql> alter table t1 add fulltext index idx_fulltext_u4(`u4`) with parser ngram;
# 查看索引关键信息
FULLTEXT KEY `idx_fulltext_u4` (`u4`) /*!50100 WITH PARSER `ngram` */如果您认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",或者直接转发pyq,

近期更新的文章:
近期的热文:
文章分类和索引:
边栏推荐
猜你喜欢

MySQL - index explanation

PanGu-Coder:函数级的代码生成模型

MySQL-Execution Process + Cache + Storage Engine

MySQL-基础

Metasploit(MSF)基础超级详细版

Azure Synapse Analytics上创建用户并赋予权限

HCIP第一天

Enterprise training and reproduction guidebook - training and reasoning of the OpenPose model based on Huawei ModelArts platform, realizing the recognition of two behaviors of climbing and climbing ov

regular expression

通过建立新的SaaS业务来推动增长的六种方法
随机推荐
Inverter insulation detection detection function and software implementation
Go implements distributed locks
CollectionUtil:一个函数式风格的集合工具
ROS file system and related commands
spark read folder data
Agile, DevOps and Embedded Systems Testing
postgres groupby 合并字符串
HCIP第七天
LeetCode brush questions (7)
59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)
2022年防止网络攻击的15个网络安全实践,你学会了吗?
OC-NSNumber and NSValue are generally used for boxing and unboxing
LeetCode 2360. 图中的最长环
flutter在导航栏处实现对两个列表的点击事件
FormData上传二进制文件、对象、对象数组
【CV】OpenVINO installation tutorial
RIP综合实验
神经元网络
MySQL-多版本并发控制
PanGu-Coder: A function-level code generation model