码迷,mamicode.com
首页 > 数据库 > 详细

MySQL索引

时间:2020-05-10 09:12:00      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:text   arc   搜索   charset   fulltext   基本   查询   全文索引   com   

MySQL索引类型详解

前言:
索引是对数据库表中一列或者多列的值进行排序的一种结构,使用索引可提高数据库中特定数据的查询速度。
索引是一个单独的、存储在磁盘上的数据库结构,它们包含着对数据表里所有记录的引用指针。使用索引用于
快速找出在某个或多个列中有一特定值得行,所有MySQL列类型都可以被索引,对相关列使用索引是提高查询
操作速度的最佳途径。

索引的优势:

  1. 加快查询速度
  2. 创建唯一索引来保证数据表中数据的唯一性
  3. 实现数据的完整性,加速表和表之间的连接
  4. 减少分组和排序的时间

增加索引也有许多不利,主要表现在如下几个方面:

  1. 创建索引和维护索引要耗费时间,并且随着数据量的增加所耗费的时间也会增加。
  2. 索引需要占磁盘空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果有大量的
    索引,索引文件可能比数据文件更快达到最大文件尺寸。
  3. 当对表中的数据进行增加、删除和修改的时候,索引也要动态地维护,这样就降低了数据的维护速度。

一、索引的分类
1、唯一索引和普通索引
普通索引:是MySQL中的基本索引类型,允许在定义索引的列中插入重复值和空值。
唯一索引:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。
主键索引:是一种特殊的唯一索引,不允许有空值。
2、单列索引和组合索引
单列索引:即一个索引只包含单个列,一个表可以有多个单列索引;
组合索引:指在表的多个字段组合上创建的索引。只有在查询条件中使用了这些字段的左边字段时,索引才会被使用。使用组合索引时遵循最左前缀集合。
3、全文索引( fulltext)
全文索引类型为FULLTEXT,在定义索引的列上支持值得全文查找,允许在这些索引列
中插入重复值和空值。全文索引可以在CHAR、VARCHAR或者TEXT类型的列上创建。MySQL 5.7.xx之前只有MyISAM存储引擎支持全文索引。
4、空间索引
空间索引是对空间数据类型的字段建立的索引,MySQL中的空间数据类型有4中,分别是:
geometry、point、linstring和polygon 。MySQL使用SPATIAL关键字进行扩展,使得能够用于创建空间索引的列,必须将其声明为NOT NULL,同样,在MySQL 5.7.xx之前,空间索引只能在存储引擎为MyISAM的表中创建。
5、创建索引的规则

(1)创建索引并非是越多越好,一个表中如果有大量的索引,不仅占用磁盘空间,而且会影响
insert、delete、update等语句的性能。因为当表中的数据更改时,索引也会进行调整和更新。
(2)数据量小得
表最好不要创建索引,由于数据较少,查询花费的时间可能比遍历索引的时间还要长。
(3)避免对经常更新的数
据创建索引。而对经常用于查询的字段应该创建索引。
(4)在条件表达式中经常用到的不同值较多的列创建索引
(5)当唯一性是某种数据本身的特征时,我们创建唯一性索引 (6)在频繁进行排序或分组的列上建立索引,如果排
序的列有多个,可以创建组合索引

二、创建表的同时创建索引
1、创建普通索引

mysql> 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_publication year not null,
-> index(year_publication)
-> );
Query OK, 0 rows affected (0.37 sec)
查看创建的索引
mysql> 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_publication year(4) NOT NULL,
KEY year_publication (year_publication)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

使用explain判断索引是否正在被使用

mysql> explain select * from book where year_publication=1999\G
1. row
id: 1
select_type: SIMPLE
table: book
partitions: NULL
type: ref
possible_keys: year_publication
key: year_publication
key_len: 1
ref: const
rows: 1
filtered: 100.00
Extra: Using index condition
1 row in set, 1 warning (0.00 sec)

2、唯一索引
创建唯一索引 唯一索引主要原因是减少查询索引列操作的执行时间。尤其是对比比较庞大的数据
表。与普通索引类似,不同点在于:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必
须唯一

mysql> create table t1
-> (
-> id int not null,
-> name char(30) not null,
-> unique index uniqidx(id)
-> );
Query OK, 0 rows affected (0.11 sec)
查看
mysql> show create table t1\G
1. row
Table: t1
Create Table: CREATE TABLE t1 (
id int(11) NOT NULL,
name char(30) NOT NULL,
UNIQUE KEY uniqidx (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

3、单列索引
单列索引:是在数据表中的某一字段上创建的索引,一个表中可以创建多个单列索引

3:单列索引
mysql> create table t2
-> (
-> id int not null,
-> name char(50) null,
-> index singleidx(name)
-> );
Query OK, 0 rows affected (0.00 sec)
查看
mysql> show create table t2\G
1. row
Table: t2
Create Table: CREATE TABLE t2 (
id int(11) NOT NULL,
name char(50) DEFAULT NULL,
KEY singleidx (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

4、组合索引
组合索引:是在多个字段上创建一个索引。遵循最左前缀原则。最左前缀 索引最左边的列来匹配行。

mysql> create table t3
-> (
-> id int not null,
-> name char(30) not null,
-> age int not null,
-> info varchar(255),
-> index multidx(id,name,age)
-> );
Query OK, 0 rows affected (0.00 sec)
查看组合索引
mysql> show create table t3\G
1. row
Table: t3
Create Table: CREATE TABLE t3 (
id int(11) NOT NULL,
name char(30) NOT NULL,
age int(11) NOT NULL,
info varchar(255) DEFAULT NULL,
KEY multidx (id,name,age)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

5、全文索引
全文索引:FULLTEXT,可以用于全文搜索,支持为CHAR\VARCHAR和TEXT 列。索引总是对整个列进行,不支持局部索引,适合大型数据的表创建

创建
mysql> create table t4
-> (
-> id int not null,
-> name char(30) not null,
-> age int not null,
-> info varchar(255),
-> fulltext index fullidx(info(100))
-> )engine=myisam;
Query OK, 0 rows affected (0.01 sec)
查看
mysql> show create table t4\G
1. row
Table: t4
Create Table: CREATE TABLE t4 (
id int(11) NOT NULL,
name char(30) NOT NULL,
age int(11) NOT NULL,
info varchar(255) DEFAULT NULL,
FULLTEXT KEY fullidx (info)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

6、空间索引
空间索引:必须在MyISAM类型的表中创建,且空间类型的字段必须为非空

mysql> create table t5
-> (
-> g geometry not null,
-> spatial index spaidx(g)
-> )engine=myisam;
Query OK, 0 rows affected (0.00 sec)
查看
mysql> show create table t5\G
1. row
Table: t5
Create Table: CREATE TABLE t5 (
g geometry NOT NULL,
SPATIAL KEY spaidx (g)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

三、在已经存在的表上创建索引

mysql> alter table book add index bknameidx(bookname(30));

添加唯一索引

mysql> alter table book add unique index uniqidex(bookid);

添加单列索引

mysql> alter table book add index bkidex(comment(50));

添加全文索引

mysql> create table t6
-> (
-> id int not null,
-> info char(255)
-> )engine=myisam;
Query OK, 0 rows affected (0.01 sec)
mysql> alter table t6 add fulltext index infofulidx(info);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

添加组合索引

mysql> alter table book add index bkauthandinfoidx(authors(20),info(50));

添加控件索引

mysql> CREATE TABLE t7
-> (
-> g GEOMETRY NOT NULL
-> )ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table t7 add spatial index spatidx(g);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

创建一个表

mysql> create table book1 (bookid int not null, bookname varchar(255) not null,
-> authors varchar(255) not null, info varchar(255) null, comment varchar(255) null,
-> year_publication year not null );
Query OK, 0 rows affected (0.01 sec)

普通索引

mysql> create index bknameidex on book1(bookname);
Query OK, 0 rows affected (0.34 sec)
Records: 0 Duplicates: 0 Warnings: 0

单列索引

mysql> create index bkcmtidex on book1 (comment(50));
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

组合索引

mysql> create index bkauthandinfoidex on book1(authors(30),info(50));
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

全文索引

mysql> drop table t6;
Query OK, 0 rows affected (0.00 sec)
mysql> create table t6 ( id int not null, info char(255))engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE FULLTEXT INDEX FullIdex ON t6(info);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

唯一性索引

mysql> create unique index uniqidx on book1(bookid);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

空间索引

mysql> drop table t7;
Query OK, 0 rows affected (0.00 sec)
mysql> create table t7 ( g geometry not null )engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> create spatial index spaidx on t7(g);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

删除索引

查看book中有几个索引准备开始删除
mysql> show create table book1\G
1. row
Table: book1
Create Table: CREATE TABLE book1 (
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_publication year(4) NOT NULL,
UNIQUE KEY uniqidx (bookid),
KEY bknameidex (bookname),
KEY bkcmtidex (comment(50)),
KEY bkauthandinfoidex (authors(30),info(50))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
用alter table删除
mysql> ALTER TABLE book1 DROP INDEX BKNameIdex;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
用drop index删除
mysql> show create table t7\G
1. row
Table: t7
Create Table: CREATE TABLE t7 (
g geometry NOT NULL,
SPATIAL KEY spaidx (g)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> drop index spaidx on t7;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

MySQL索引

标签:text   arc   搜索   charset   fulltext   基本   查询   全文索引   com   

原文地址:https://blog.51cto.com/14471717/2493756

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!