MySql索引详解-各种索引的定义与区别和应用


什么是索引?索引的作用,有无索引的区别。

一、索引基础:增删改查

以下场景既适用于单值索引也适用于复合索引。

1.新增索引的几种方式

只有主键索引不适用create index 其他均是适用的。

-- 方式一:
create index idx_building_id on tb_mdm_unit(building_id) using btree comment '楼栋id';
-- 方式二:
alter table tb_mdm_unit add index idx_building_id(building_id) using btree comment '楼栋id';
-- 方式三:这里也可以在声明列时直接声明
drop table if exists testTable;
create table testTable(
id tinyint  comment 'id',
column1 tinyint not null comment '列1',
column2 BIGINT not null comment '列2',
column3 MEDIUMINT not null comment '列3',
column4 DATETIME not null comment '列4',
column5 date not null comment '列5',
column6 TIMESTAMP not null comment '列6',
column7 year not null comment '列7',
column8 GEOMETRY not null comment '列8',
column9 varchar(20)  not null comment '列9',
-- PRIMARY key(id) using BTREE comment '主键',
PRIMARY key(id,column1) using BTREE comment '复合主键',
UNIQUE key colu1_idx(column1) using BTREE comment '唯一索引',
UNIQUE key colu1_cdx(column1,column2) using BTREE comment '复合唯一索引',
index colu3_idx(column3) using BTREE comment '普通索引1',
index dolu3_cdx(column3,column4) using BTREE comment '复合普通索引1',
fulltext colu9_idx(column9) comment '全文索引',
SPATIAL index colu8_idx(column8) comment '空间索引'
);

2.删除索引的几种方式

drop index 这种删除方式不适用于主键索引,其他索引均可适用。

drop index idx_building_id on tb_mdm_unit;
alter table tb_mdm_unit drop index idx_building_id;

3.修改索引的几种方式

下面的修改只有在mysql8中才可以使用,是用来将索引标识未可见不可见的(查看可见不可见可以使用show index from table)。索引不可见以后,条件使用索引字段将不走索引。如果是想将单列索引修改成复合索引,这种只能是删了重建才可以。

-- 修改索引可见(mysql8.0以后支持)
alter table tb_mdm_unit alter index idx_building_id visible;
-- 修改索引不可见(mysql8.0以后支持)
alter table tb_mdm_unit alter index idx_building_id invisible;

4.查询索引的几种方式

-- 方式一:查看建表语句,此时可以看到
show create table tb_mdm_floor;
-- 方式二:查看表的所有索引
show index from tb_mdm_floor;
-- 方式三:查看所有列(会标明列上的索引)
show columns from tb_mdm_floor;

二、索引的分类

从功能上对索引进行划分的话,可以划分为以下几种索引。

1.主键索引

非null且唯一,可以为将任意字段类型设置为主键,不过对于blob/text添加主键时必须指明主键长度(其实就是前缀索引),因为blob存储的是二进制文件,text存储的是文本,都比较长,如果使用原始信息做主键,会导致主键过长,从而影响查询效率,下面是测试使用各个字段做主键的sql。
主键使用切记不要更新主键,更新会导致BTree的重新计算主键索引位置,很耗时。一般不应该操作主键的更新。
下面是验证各种数据类型是否可以作为主键的sql

drop table if exists testTable;
create table testTable(
id integer comment '主键',
column1 tinyint not null comment '列1',
column2 BIGINT not null comment '列2',
column3 MEDIUMINT not null comment '列3',
column4 DATETIME not null comment '列4',
column5 date not null comment '列5',
column6 TIMESTAMP not null comment '列6',
column7 year not null comment '列7',
column8 char not null comment '列8',
column9 varchar(20)  not null comment '列9',
column10 blob not null comment '列11',
column11 text not null comment '列12',
column12 enum('one','two','three') not null comment '列13',
column13 set('one','two')  comment '列14'
);

alter table testTable add primary key(id) comment '测试主键'; alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column1) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column2) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column3) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column4) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column5) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column6) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column7) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column8) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column9) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column10(10)) comment '测试主键';alter table testTable drop PRIMARY key ; -- blob 只能添加前缀索引类型的主键
alter table testTable add primary key(column11(10)) comment '测试主键';alter table testTable drop PRIMARY key ; -- text 只能添加前缀索引类型的主键
alter table testTable add primary key(column12) comment '测试主键';alter table testTable drop PRIMARY key ;
alter table testTable add primary key(column13) comment '测试主键';alter table testTable drop PRIMARY key ;

主键的新增

-- 方式一:修改表添加
alter table testTable add primary key(column13) comment '测试主键';

-- 方式二:建表字段上指定
drop table if exists testTable;
create table testTable(
id tinyint PRIMARY key auto_increment comment '测试主键'
);

-- 方式三: 建表末尾指定
drop table if exists testTable;
create table testTable(
id tinyint  comment 'id',
PRIMARY key(id) using BTREE comment '测试主键'
);

删除主键

alter table testTable drop PRIMARY key ;

主键的查看,使用索引的通用查看方法即可(第一节中)。此外主键可以是复合索引,主键也可以是前缀索引。如下举例:

alter table testTable add PRIMARY key(id,column1) using btree comment '复合主键索引';
alter table testTable add PRIMARY key(column9(10)) using btree comment '前缀主键索引';

2.唯一索引

非null,可以使用在任何类型的字段上。没有特别要求,都可以使用,可以建立复合唯一索引,前缀唯一索引(这个使用需要慎重),其他的和主键索引的创建删除都没有任何的区别,这里就不重复列举了。

3.普通索引

对字段类型没有要求,可以创建在任何数据类型上,用以提升查询效率。普通索引同样可以是复合索引,也可以是前缀索引,使用上便是第一节的列举情况。

4.复合索引

其实就是普通索引的变种,支持多列共同构建一个索引。使用参考第一节,这里不重复列举了。

5.全文索引

全文索引功能类似于ES,可以根据关键字进行快速查找,在不使用全文索引的情况下,模糊匹配查询 like ‘%张三’,这种会导致索引失效。因此mysql也是在引入了全文索引fulltext,用以支持模糊查询的快速查找,全文索引和模糊查询的效率不是一个量级的。==在MySql5.6以前只有MYISAM支持fulltext,在5.6及以后INNODB和MYISAM均支持全文索引了,且全文索引只能建立在字符串类型上,如char、varchar、text等数据类型。==这里介绍全文索引的基础用法,不深入探讨。
全文索引的新增:

-- 方式一:建表时添加全文索引
drop table if exists testTable;
create table testTable(
id tinyint  comment 'id',
column1 tinyint not null comment '列1',
column8 text not null  comment '经纬度列',
column9 varchar(50) not null comment '列9',
fulltext index colu9_idx(column9) comment '全文索引',
fulltext index colu8_idx(column8,column9) comment '复合全文索引'
);

-- 方式二:使用create index 添加
create fulltext index colu9_fullindex on testTable(column9) comment '全文索引';
-- 方式三:使用alter table 添加
alter table testTable add fulltext index colu8_idx(column8,column9) comment '复合全文索引';

全文索引的删除查看和普通索引没有任何区别,这里不重复说了,来看下全文索引的使用吧:

MATCH (col1,col2,...) AGAINST (expr [search_modifier])
search_modifier:
  {
       IN NATURAL LANGUAGE MODE
     | IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
     | IN BOOLEAN MODE
     | WITH QUERY EXPANSION
  }

使用举例如下:

-- 插入数据
insert into testTable values
(1,0,'11','zhangsan'),
(2,0,'11','zhangsanlisi'),
(3,0,'11','zhangsanlisiwangwu'),
(4,0,'11','zhangsanlisiwangwuzhaoli'),
(5,0,'11','zhangsanlisiwangwuzhaoliqianqizhangsan');

-- 使用布尔模式进行模糊匹配查找
select * from testTable where MATCH(column9) against('zhang*' in boolean mode);

上面的执行截图:
在这里插入图片描述

6.空间索引

空间索引只能针对空间类型的字段进行建立,mysql中的空间索引和全文索引实现都不是BTREE,所以即使是INNODB也存在多种不同的索引数据结构。mysql的空间类型和对应的索引使用范围不广。mysql中的空间数据类型有4种,分别是GEOMETRY、POINT、LINESTRING、POLYGON。创建空间索引的列,必须将其声明为NOT NULL。
空间索引的建立:

-- 方式一
create spatial index colu8_idx on testTable(column8) comment '空间索引';
-- 方式二
alter table testTable add spatial index colu8_idx(column8) comment '空间索引';
-- 方式三
drop table if exists testTable;
create table testTable(
id tinyint  comment 'id',
column1 tinyint not null comment '列1',
column2 BIGINT not null comment '列2',
column3 MEDIUMINT not null comment '列3',
column4 DATETIME not null comment '列4',
column5 date not null comment '列5',
column6 TIMESTAMP not null comment '列6',
column7 year not null comment '列7',
column8 GEOMETRY not null comment '列8',
column9 varchar(20)  not null comment '列9',
-- PRIMARY key(id) using BTREE comment '主键',
PRIMARY key(id,column1) using BTREE comment '复合主键',
UNIQUE key colu1_idx(column1) using BTREE comment '唯一索引',
UNIQUE key colu1_cdx(column1,column2) using BTREE comment '复合唯一索引',
index colu3_idx(column3) using BTREE comment '普通索引1',
index dolu3_cdx(column3,column4) using BTREE comment '复合普通索引1',
fulltext colu9_idx(column9) comment '全文索引',
SPATIAL index colu8_idx(column8) comment '空间索引'
);

空间索引的删除和查看等都和普通索引没有什么区别,这里就不重复写入了。

三、总结

这里介绍的都是基础的索引的增删改查,和一些索引使用的案例以及注意事项。我们常用的其实还是主键索引、唯一索引、普通索引,复合索引、前缀索引(字符串或者二进制类型建立普通索引时可以建立前缀索引),其他的全文和空间索引其实是不常用的。这里列出来作为了解吧。

猜你喜欢

转载自blog.csdn.net/m0_46897923/article/details/127877587