索引用于快速找出在某个列中有一特定值的行,如果不使用索引MySQL必须从第l条
记录开始读完整个表,直到找出相关的行.表越大,查询数据所花费的时间越多,如果表中查询的列有一个索引,MySQL能快速到达某个位置去搜寻数据文件,而不必查看所有数据.
索引是一个单独的、存储在磁盘上的数据库结构,它们包含着对数据表里所有记录的引用指针.使用索引用于快速找出在某个或多个列中有一特定值的行,所有MySQL列类型都可以被索引,对相关列使用索引是提高查询操作速度的最佳途径.
这里打个比方:比如我们要查找num=1000
,如果没有索引,那么MySQL将会遍历整个表,直到找到1000这一行为止
.如果有索引的情况下,当我们查找num时,MySQL不需要任何遍历,直接在索引里找到数据的位置,这样一来提高了数据库的查询效率.
索引与存储引擎?
索引是在存储引擎中实现的,因此每种存储引擎的索引都不一定完全相同,并且每种存诸引擎也不一定支持所有索引类型.根据存储引擎定义每个表的最大索引数和最大索引长度,存储引擎支持每个表至少16个
索引,总索引长度至少为256字节
.大多数存储引擎有更高的限制,MySQL中索引的存储类型有两种:1.BTREE和HASH,具体和表的存储引擎相关
,2.MyISAM和InnoDB存储引擎只支持BTREE索引
,3.MEMORY/HEAP存储引擎可以支持HASH和BTREE索引
.
MySQL索引的优点:
● 通过创建唯一索引,可以保证数据库表中每一行数据的唯一性 ● 可以大大加快数据的查询速度 ● 在实现数据的参考完整性方面,可以加速表和表之间的连接 ● 使用分组和排序子句进行数据查询时,也可以显著减少查询中分组和排序的时间
MySQL索引的缺点:
● 维护索引要花费大量时间,并且随着数据量的增加所耗费的时间也会增加 ● 索引占用磁盘空间,每一个索引要占一定得物理空间,如果有大量索引,索引文件可能比数据文件更快达到最大文件尺寸 ● 当对数据进行增删改查时,索引也需要动态调整,大大降低了数据的可维护性
MySQL索引的设计原则:
● 索引并非越多越好,一个表中如有大量的索引,不仅占用磁盘空间,而且会影响语句执行效率 ● 避免对经常更新的表进行过多的索引,并且索引中的列尽可能少 ● 对经常用于查询的字段应该创建索引,但要避免添加不必要的字段 ● 数据量小的表最好不要使用索引,如果添加索引可能非但达不到好的效果,反而适得其反 ● 在条件表达式中经常用到的不同值较多的列上建立索引,在不同值很少的列上不要建立索引 ● 当唯一性是某种数据本身的特征时,指定唯一索引,可以提高数据查询效率 ● 在频繁进行排序或分组(group by或order by操作)的列上建立索引
MySQL索引的分类有哪些?
1.普通索引:MySQL中的基本索引类型,允许在定义索引的列中插入重复值和空值. 2.唯一索引:索引列的值必须唯一,但允许有空值.如果是组合索引,则列值的组合必须唯一. 3.主键索引:一种特殊的唯一索引,不允许有空值. 4.单列索引:即一个索引只包含单个列,一个表可以有多个单列索引. 5.组合索引:指在表的多个字段组合上创建的索引,使用组合索引时遵循最左前缀集合. 6.全文索引:允许在这些索引列中插入重复值和空值,只有MyISAM存储引擎支持全文索引. 7.空间索引:对空间数据类型的字段建立的索引,空间索引只能在存储引擎为MyISAM的表中创建.
MariaDB支持多种方法在单个或多个列上创建索引,在创建表的定义语句create table
中指定索引列,使用alter table
语句在存在的表上创建索引,或使用create index
语句在已存在表上添加索引.
以上的知识点就是索引章节的全部内容啦,其实索引的实践环节还是很简单的,只需要了解几个命令就行啦,接下来我们来看一下索引的具体使用过程吧.
MariaDB 创建索引 使用CREATE TABLE
创建表时,除了可以定义列的数据类型,还可以定义主键约束、外键约束或者唯一性约束,而不论创建那种约束,在定义约束的同时相当于在指定列上创建了一个索引。创建表时创建索引的基本语法格式如下:
MariaDB [lyshark]> create table table_name [col_name data_type] [unique | fulltext | spatial] [index | key][index_name] (col_name [length]) [ASC | DESC ] # #[参数解释] unique | fulltext | spatial #可选参数,分别表示唯一索引,全文索引,空间索引index | key #两者作用相同,用来指定创建索引 col_name #需要创建索引的字段列,此列必须从数据表中定义的列中选择 length #可选参数,表示索引长度,只用字符串类型才能指定索引长度 ASC | DESC #指定升序或者降序的索引值存储
◆创建普通索引◆ 创建最基本的索引类型,没有唯一性之类的限制,其作用只是加对快数据的访问速度.
1.创建book
表的同时,在year_public
字段上建立普通索引,SQL语句如下:
MariaDB [lyshark]> create table book - > ( - > bookid int not null , - > bookname varchar (255 ) not null , - > authors varchar (255 ) not null , - > info varchar (255 ) null , - > comment varchar (255 ) null , - > year_public year not null , - > index(year_public) - > ); Query OK, 0 rows affected (0.03 sec) MariaDB [lyshark]> desc book; + | Field | Type | Null | Key | Default | Extra | + | bookid | int (11 ) | NO | | NULL | | | bookname | varchar (255 ) | NO | | NULL | | | authors | varchar (255 ) | NO | | NULL | | | info | varchar (255 ) | YES | | NULL | | | comment | varchar (255 ) | YES | | NULL | | | year_public | year (4 ) | NO | MUL | NULL | | + 6 rows in set (0.00 sec)
2.执行完语句后,我们可以使用show create table
查看表结构:
MariaDB [lyshark]> show create table book \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Create Table : CREATE TABLE `book` ( `bookid` int (11 ) NOT NULL , `bookname` varchar (255 ) NOT NULL , `authors` varchar (255 ) NOT NULL , `info` varchar (255 ) DEFAULT NULL , `comment` varchar (255 ) DEFAULT NULL , `year_public` year (4 ) NOT NULL , #成功建立了索引 KEY `year_public` (`year_public`) ) ENGINE= InnoDB DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
3.使用explain
语句查看索引是否正在使用,SQL语句如下:
MariaDB [lyshark]> explain select * from book where year_public= 990 \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * id: 1 select_type: SIMPLE table : book type: ref possible_keys: year_public key: year_public key_len: 1 ref : const rows : 1 Extra: Using index condition 1 row in set (0.00 sec)
对以上EXPLAIN语句输出结果的解释:
● select type
:指定所使用的SELECT查询类型,这里值为SIMPLE ● table
:指定数据库读取的数据表的名字 ● type
:指定了本数据表与其他数据表之间的关联关系 ● possible keys
:给出了MySQL在搜索数据记录时可选用的各个索引 ● key
行是MySQL实际选用的索引 ● key len
:行给出索引按字节计算的长度,key len数值越小,表示越快 ● ref
:行给出了关联关系中另一个数据表里的数据列的名字 ● rows
:行是MySQL在执行这个查询时预计会从这个数据表里读出的数据行的个数 ● extra
:行提供了与关联操作有关的信息
可以看到,possible keys
和key
的值都为year_public,说明查询时使用了索引.
◆创建唯一索引◆ 创建唯一索引的主要原因是减少查询索引列操作的执行时间
,尤其是对比较庞大的数据表.它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值.如果是组合索引,则列值的组合必须唯一.
创建一个表table_1
,在表中的id
字段上使用unique
关键字创建唯一索引
MariaDB [lyshark]> create table table_1 - > ( - > id int not null , - > name char (30 ) not null , - > unique index UniqIdx(id) - > ); Query OK, 0 rows affected (0.02 sec) MariaDB [lyshark]> show create table table_1 \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_1 Create Table : CREATE TABLE `table_1` ( `id` int (11 ) NOT NULL , `name` char (30 ) NOT NULL , UNIQUE KEY `UniqIdx` (`id`) #id字段已经成功建立了一个名为UniqIdx的唯一索引 ) ENGINE= InnoDB DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
◆创建单列索引◆ 单列索引是在数据表中的某一个字段上创建的索引
,一个表中可以创建多个单列索引.前面两个例子中创建的索引都为单列索引.
创建一个表table_2
,在表中的name字段
上创建单列索引.
MariaDB [lyshark]> create table table_2 - > ( - > id int not null , - > name char (50 ) null , - > index SingleIdx(name(20 )) - > ); Query OK, 0 rows affected (0.03 sec) MariaDB [lyshark]> show create table table_2 \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_2 Create Table : CREATE TABLE `table_2` ( `id` int (11 ) NOT NULL , `name` char (50 ) DEFAULT NULL , KEY `SingleIdx` (`name`(20 )) #name字段上已经成功建立了一个单列索引,名称为SingleIdx ) ENGINE= InnoDB DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
◆创建组和索引◆ 组合索引就是在多个字段上创建一个索引.
1.首先创建table_3
表,在表中的id,name,age
字段上建立组合索引,SQL语句如下:
MariaDB [lyshark]> create table table_3 - > ( - > id int not null , - > name char (30 ) not null , - > age int not null , - > info varchar (255 ), - > INDEX MultiIdx(id,name,age) - > ); Query OK, 0 rows affected (0.01 sec)
2.创建完成之后,我们来查看一下结果吧.
MariaDB [lyshark]> show create table table_3 \G * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_3 Create Table : CREATE TABLE `table_3` ( `id` int (11 ) NOT NULL , `name` char (30 ) NOT NULL , `age` int (11 ) NOT NULL , `info` varchar (255 ) DEFAULT NULL , KEY `MultiIdx` (`id`,`name`,`age`) ) ENGINE= InnoDB DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
3.测试,在table_3
表中,差查询id和name
字段,使用explain
语句查看索引的使用情况:
MariaDB [lyshark]> explain select * from table_3 where id= 1 and name= 'lyshark' \G * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * id: 1 select_type: SIMPLE table : table_3 type: ref possible_keys: MultiIdx key: MultiIdx key_len: 34 ref : const,const rows : 1 Extra: Using index condition 1 row in set (0.07 sec)
◆创建全文索引◆ FULLTEXT(全文索引)可以用于全文搜索,全文索引适合用于大型数据集
.只有MyISAM存储引擎支持
FULLTEXT索引,并且只为CHAR、VARCHAR和TEXT列
创建索引.索引总是对整个列进行,不支持局部(前缀)索引.
1.创建表table_4
在表中info字段
上建立一个全文索引,SQL语句如下:
MariaDB [lyshark]> create table table_4( - > id int not null , - > name char (40 ) not null , - > age int not null , - > info varchar (255 ), - > fulltext index FullTxtIdx(info) - > )engine= MyISAM; Query OK, 0 rows affected (0.00 sec) #注意:MySQL5.7 默认存储引擎室是InnoDB,在这里我们要改成MyISAM,不然索引会报错
2.结果如下,这样我们就创建了一个名为FullTxtIdx
的全文索引.
MariaDB [lyshark]> show create table table_4 \G * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_4 Create Table : CREATE TABLE `table_4` ( `id` int (11 ) NOT NULL , `name` char (40 ) NOT NULL , `age` int (11 ) NOT NULL , `info` varchar (255 ) DEFAULT NULL , FULLTEXT KEY `FullTxtIdx` (`info`) ) ENGINE= MyISAM DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
◆创建空间索引◆ 空间索引必须在MyISAM
类型的表中创建,且空间类型的字段必须为空.
1.创建一个表table_5
,在空间类型Geometry的字段
上创建空间索引,SQL语句如下:
MariaDB [lyshark]> create table table_5 - > ( - > g geometry not null , - > spatial index spatIdx(g) - > )engine= MyISAM; Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> show create table table_5 \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_5 Create Table : CREATE TABLE `table_5` ( `g` geometry NOT NULL , SPATIAL KEY `spatIdx` (`g`) ) ENGINE= MyISAM DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
可以看到,table_5表
的g字段
上创建了名称为spatIdex
的空间索引,注意:创建时间指定空间类型字段值的非空约束,并且表的存储引擎必须为MyISAM.
MariaDB 添加索引 在已经存在的表中创建索引,可以使用alter table
语句或者create index
语句,本节将介绍如何在一个已经存在的表上创建一个索引,首先先来看一下索引的添加语法,SQL语句如下:
alter table table_name add [unique | fulltext | spatial ] [ index | key ] [ index_name ](col_name[length],.....) [ asc | esc ] # #[参数解释] unique | fulltext | spatial #可选参数,分别表示唯一索引,全文索引,空间索引index | key #两者作用相同,用来指定创建索引 col_name #需要创建索引的字段列,此列必须从数据表中定义的列中选择 length #可选参数,表示索引长度,只用字符串类型才能指定索引长度 ASC | DESC #指定升序或者降序的索引值存储
◆添加普通索引◆ 在book表中bookname字段
上,将建立名为BKNameIdx的普通索引
.
1.首先添加索引前,使用show index语句
查看指定表中创建的索引
MariaDB [lyshark]> show index from book \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 1 Key_name: year_public Seq_in_index: 1 Column_name: year_public Collation : A Cardinality : 0 Sub_part: NULL Packed: NULL Null : Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec)
对以上语句输出结果的解释:
● Table
:表示创建索引的表 ● Non unique
:表示索引非唯一,1代表是非唯一索引,0代表唯一索引 ● Key name
:表示索引的名称 ● Seq in index
:表示该字段在索引中的位置,单列索引该值为1,组合索引为每个字段在索引定义中的顺序 ● Column name
:表示定义索引的列字段 ● Sub part
:表示索引的长度 ● Null
:表示该字段是否能为空值 ● Index type
:表示索弓引类型
可以看到,book表中已经存在了一个索引,即前面已经定义的名称为year publication索引
,该索引为非唯一索引.
2.使用alter table
在bookname字段
上添加索引,SQL语句如下:
MariaDB [lyshark]> alter table book add index BKNameIdx(bookname(30 )); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
3.使用show index
语句查看表中索引:
MariaDB [lyshark]> show index from book \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 1 Key_name: year_public Seq_in_index: 1 Column_name: year_public Collation : A Cardinality : 0 Sub_part: NULL Packed: NULL Null : Index_type: BTREE Comment: Index_comment: * * * * * * * * * * * * * * * * * * * * * * * * * * * 2. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 1 Key_name: BKNameIdx Seq_in_index: 1 Column_name: bookname Collation : A Cardinality : 0 Sub_part: 30 Packed: NULL Null : Index_type: BTREE Comment: Index_comment: 2 rows in set (0.00 sec)
可以看到表中有两个索引,一个通过alter table 语句
添加的名称为BKNameIdx的索引
,该索引为非唯一索引,长度为30
.
◆添加唯一索引◆ 在book表
的bookId字段
上建立名称为UniqidIdx的唯一索引
,SQL语句如下:
MariaDB [lyshark]> alter table book add unique index UniqidIdx(bookId); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> show index from book \G * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 0 Key_name: UniqidIdx #此处为创建的唯一索引。 Seq_in_index: 1 Column_name: bookid Collation : A Cardinality : 0 Sub_part: NULL Packed: NULL Null : Index_type: BTREE Comment: Index_comment:
可以看到,Non_unique属性为0
,表示名称为UniqidIdx的索引为唯一性索引
,创建唯一索引成功.
◆添加单列索引◆ 在book表
的comment字段
建立单列索引,SQL语句如下:
MariaDB [lyshark]> alter table book add index BkcmtIdx(comment(50 )); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> show index from book \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 4. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 1 Key_name: BkcmtIdx Seq_in_index: 1 Column_name: comment Collation : A Cardinality : 0 Sub_part: 50 Packed: NULL Null : YES Index_type: BTREE Comment: Index_comment: 4 rows in set (0.00 sec)
语句执行后,book表的comment字段
上建立了名称为BkcmtIdx的索引
,长度为50
,查询时只需要检索前50个
字符.
◆添加组和索引◆ 在book表
的authors和info字段
上建立组合索引,SQL语句如下:
MariaDB [lyshark]> alter table book add index BkAuAndInfoIdx(authors(30 ),info(50 )); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> show index from book \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 5. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 1 Key_name: BkAuAndInfoIdx Seq_in_index: 1 Column_name: authors Collation : A Cardinality : 0 Sub_part: 30 Packed: NULL Null : Index_type: BTREE Comment: Index_comment: * * * * * * * * * * * * * * * * * * * * * * * * * * * 6. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Non_unique: 1 Key_name: BkAuAndInfoIdx Seq_in_index: 2 Column_name: info Collation : A Cardinality : 0 Sub_part: 50 Packed: NULL Null : YES Index_type: BTREE Comment: Index_comment: 6 rows in set (0.00 sec)
可以看到,名称为BkAuAndInfoIdx索引
,有两个字段组成authors字段长度为30
,在组合索引中序列为1
该字段不许为空NULL,info字段长度为50
,在组合索引中的序号为2,该字段可以为空值NULL.
◆添加全文索引◆ 创建表table_6
在table_6表上使用alter table
创建全文索引.
1.首先创建表table_6
并指定存储引擎为MyISAM
,SQL语句如下:
MariaDB [lyshark]> create table table_6 - > ( - > id int not null , - > info char (255 ) - > )engine= MyISAM; Query OK, 0 rows affected (0.01 sec)
2.创建并查看全文索引:
MariaDB [lyshark]> alter table table_6 add fulltext index infoFTIdx(info); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> show index from table_6 \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_6 Non_unique: 1 Key_name: infoFTIdx Seq_in_index: 1 Column_name: info Collation : NULL Cardinality : NULL Sub_part: NULL Packed: NULL Null : YES Index_type: FULLTEXT Comment: Index_comment: 1 row in set (0.01 sec)
以上可知,table_6表
中已经创建了名称为infoFTIdx的索引
,该索引在info字段
上创建,类型为fulltext
,允许为空.
◆添加空间索引◆ 创建表table_7
,在table_7的空间数据类型字段g
上创建名称为spatIdx的空间索引
,SQL语句如下:
1.首先创建表结构.
MariaDB [lyshark]> create table table_7 - > ( - > g geometry not null - > )engine= MyISAM; Query OK, 0 rows affected (0.00 sec)
2.使用alter table
在table_7
上创建空间索引.
MariaDB [lyshark]> alter table table_7 add spatial index spatIdx(g); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> show index from table_7 \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : table_7 Non_unique: 1 Key_name: spatIdx #此处是空间索引的名称 Seq_in_index: 1 Column_name: g Collation : A Cardinality : NULL Sub_part: 32 Packed: NULL Null : Index_type: SPATIAL Comment: Index_comment: 1 row in set (0.00 sec)
MariaDB 删除索引 删除索引可以使用alter table
语句,下面我们介绍它的删除方法,例如:删除book表
中名称为UniqidIdx的唯一索引
,SQL语句如下:
1.首先查看一下名称为UniqidIdx
的索引,输入Show语句:
MariaDB [lyshark]> show create table book \G; * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Create Table : CREATE TABLE `book` ( `bookid` int (11 ) NOT NULL , `bookname` varchar (255 ) NOT NULL , `authors` varchar (255 ) NOT NULL , `info` varchar (255 ) DEFAULT NULL , `comment` varchar (255 ) DEFAULT NULL , `year_public` year (4 ) NOT NULL , UNIQUE KEY `UniqidIdx` (`bookid`), #下一步我们删除它 KEY `year_public` (`year_public`), KEY `BKNameIdx` (`bookname`(30 )), KEY `BkcmtIdx` (`comment`(50 )), KEY `BkAuAndInfoIdx` (`authors`(30 ),`info`(50 )) ) ENGINE= InnoDB DEFAULT CHARSET= latin1 1 row in set (0.00 sec)
2.删除UniqidIdx
唯一索引,SQL语句如下:
MariaDB [lyshark]> alter table book drop index UniqidIdx; #删除语句 Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> show create table book \G; #删除成功了 * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : book Create Table : CREATE TABLE `book` ( `bookid` int (11 ) NOT NULL , `bookname` varchar (255 ) NOT NULL , `authors` varchar (255 ) NOT NULL , `info` varchar (255 ) DEFAULT NULL , `comment` varchar (255 ) DEFAULT NULL , `year_public` year (4 ) NOT NULL , KEY `year_public` (`year_public`), KEY `BKNameIdx` (`bookname`(30 )), KEY `BkcmtIdx` (`comment`(50 )), KEY `BkAuAndInfoIdx` (`authors`(30 ),`info`(50 )) ) ENGINE= InnoDB DEFAULT CHARSET= latin1 1 row in set (0.00 sec)