09_MySQL索引

一、索引介绍

索引是对数据库表中一列或者多列的值进行排序的一种结构,使用索引可提高数据库中特定数据的查询速度。

索引是一个单独的、存储在磁盘上的数据库结构,它们包含着对数据表里所有记录的引用指针。使用索引用于快速找出在某个或多个列中有一特定值得行,所有MySQL列类型都可以被索引,对相关列使用索引是提高查询操作速度的最佳途径。 索引是在存储引擎中实现的,因此,每种存储引擎的索引都不一定完全相同,并且每种存储引擎也不一定支持索引类型。根据存储引擎定义每个表的最大索引数和最大索引长度。所有存储引擎支持每个表至少16个索引,总索引长度至少为256字节。 MyISAM ,InnoDB支持btree索引 Memory 支持 btree和hash索引

索引的优势:

  1. 加快查询速度

  2. 创建唯一索引来保证数据表中数据的唯一性

  3. 实现数据的完整性,加速表和表之间的连接

  4. 减少分组和排序的时间

增加索引也有许多不利

索引的不利:

  1. 创建索引和维护索引要耗费时间,并且随着数据量的增加所耗费的时间也会增加。

  2. 索引需要占磁盘空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果有大量的索引,索引文件可能比数据文件更快达到最大文件尺寸。

  3. 当对表中的数据进行增加、删除和修改的时候,索引也要动态地维护,这样就降低了数据的维护速度。

索引的分类

  1. 唯一索引和普通索引 普通索引是MySQL中的基本索引类型,允许在定义索引的列中插入重复值和空值。唯一索引,索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。主键索引是一种特殊的唯一索引,不允许有空值。

  2. 单列索引和组合索引 单列索引即一个索引只包含单个列,一个表可以有多个单列索引。 组合索引指在表的多个字段组合上创建的索引。只有在查询条件中使用了这些字段的左边字段时,索引才会被使用。使用组合索引时遵循最左前缀集合。

  3. 全文索引 fulltext 全文索引类型为FULLTEXT,在定义索引的列上支持值得全文查找,允许在这些索引列中插入重复值和空值。全文索引可以在CHAR、VARCHAR或者TEXT类型的列上创建。MySQL中只有MyISAM存储引擎支持全文索引。

  4. 空间索引 空间索引是对空间数据类型的字段建立的索引,MySQL中的空间数据类型有4中,分别是:geometry、point、linstring和polygon 。MySQL使用SPATIAL关键字进行扩展,使得能够用于创建空间索引的列,必须将其声明为NOT NULL,空间索引只能在存储引擎为MyISAM的表中创建。

创建索引的规则

(1)创建索引并非是越多越好,一个表中如果有大量的索引,不仅占用磁盘空间,而且会影响insert、delete、update等语句的性能。因为当表中的数据更改时,索引也会进行调整和更新。

(2)数据量小得表最好不要创建索引,由于数据较少,查询花费的时间可能比遍历索引的时间还要长。

(3)避免对经常更新的数据创建索引。而对经常用于查询的字段应该创建索引。

(4)在条件表达式中经常用到的不同值较多的列创建索引。

(5)当唯一性是某种数据本身的特征时,我们创建唯一性索引 (6)在频繁进行排序或分组的列上建立索引,如果排序的列有多个,可以创建组合索引。

二、创建索引

Create index 创建索引 alter table 添加索引 Create table 表名[字段名 数据类型] [unique唯一索引|fulltext
全文索引|spatial空间索引] [index|key] [索引名] (col_name [length]) [asc |desc]

创表时创建索引

1. 创建普通索引
普通索引是最基础的索引类型,没有唯一性的限制。作用是只加快对数据的访问速度。

mysql> create table book
    -> (
    -> bookid int not null,
    -> bookname varchar(255) not null,
    -> authors varchar(255)  not null,
    -> info varchar(255) ,
    -> comment varchar(255),
    -> year_publication year not null,
    -> index (year_publication)
    -> );

//查看创建的索引

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)

TYPE的取值范围 System const ref eq_ref index all range

2.创建唯一索引

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

mysql> create table t1
   -> (
   -> id int not null,
   -> name char(22) not null,
   -> unique index t1(id)  //索引名字为t1
   -> );
Query OK, 0 rows affected (0.01 sec)
mysql> show create table t1\G
*************************** 1. row ***************************
      Table: t1
Create Table: CREATE TABLE `t1` (
 `id` int(11) NOT NULL,
 `name` char(22) NOT NULL,
 UNIQUE KEY `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

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

mysql> create table t2
   -> (
   -> id int not null,
   -> name char(22),
   -> index t2(name)
   -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t2\G
*************************** 1. row ***************************
      Table: t2
Create Table: CREATE TABLE `t2` (
 `id` int(11) NOT NULL,
 `name` char(22) DEFAULT NULL,
 KEY `t2` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

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

mysql> create table t3
  -> (
  -> id int ,
  -> name char(30) not null,
  -> age int not null,
  -> info varchar(255),
  -> index t3(id,name,age)
  -> );
Query OK, 0 rows affected (0.03 sec)

mysql> show create table t3\G
*************************** 1. row ***************************
     Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) DEFAULT NULL,
`name` char(30) NOT NULL,
`age` int(11) NOT NULL,
`info` varchar(255) DEFAULT NULL,
KEY `t3` (`id`,`name`,`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

//验证组合索引的左前缀原则

mysql> explain select * from t3 where id = '999' and name ='zhangsan' and age='18'\G
#从索引列的最左边开始查看是可以使用到索引的
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t3
   partitions: NULL
         type: ref
possible_keys: t3
          key: t3
      key_len: 99
          ref: const,const,const
         rows: 1
     filtered: 100.00
        Extra: NULL
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from t3 where id = '999'\G #可以看到id列用到了索引
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t3
   partitions: NULL
         type: ref
possible_keys: t3
          key: t3
      key_len: 5
          ref: const
         rows: 1
     filtered: 100.00
        Extra: NULL
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from t3 where name = 'zhangsan'\G
#name列也是索引,但是查询没用从最左边开始,所以查询的时候没有用到索引
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t3
   partitions: NULL
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 6
     filtered: 16.67
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

5.全文索引
全文索引:FULLTEXT,可以用于全文搜索,MyISAM存储引擎支持,并且只为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 t4(info(100))
    -> )  ENGINE=MyISAM;

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 `t4` (`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 t5(g)
   -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t5\G
*************************** 1. row ***************************
      Table: t5
Create Table: CREATE TABLE `t5` (
 `g` geometry NOT NULL,
 SPATIAL KEY `t5` (`g`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

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

Alter table 表名 add [unique唯一索引|fulltext全文索引|spatial空间索引] [index|key] [默认索引名] (定义索引名[length]) [asc|desc]

1.添加普通索引

mysql> alter table book add index boott1(bookname(30));
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

//查看

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`),
  KEY `boott1` (`bookname`(30))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

//或者

mysql> show index from book\G
*************************** 1. row ***************************
        Table: book
   Non_unique: 1
     Key_name: year_publication
 Seq_in_index: 1
  Column_name: year_publication
    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: boott1
 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)

2.添加唯一性索引

mysql> alter table book add unique index bookt2(bookid);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

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,
  UNIQUE KEY `bookt2` (`bookid`),
  KEY `year_publication` (`year_publication`),
  KEY `boott1` (`bookname`(30))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

3.添加单列索引

mysql> alter table book add index bookt13(comment(50));
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

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,
  UNIQUE KEY `bookt2` (`bookid`),
  KEY `year_publication` (`year_publication`),
  KEY `boott1` (`bookname`(30)),
  KEY `bookt13` (`comment`(50))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

4. 添加组合索引

mysql> alter table book add index bookt4(comment(50),info(50));
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

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,
  UNIQUE KEY `bookt2` (`bookid`),
  KEY `year_publication` (`year_publication`),
  KEY `boott1` (`bookname`(30)),
  KEY `bookt13` (`comment`(50)),
  KEY `bookt4` (`comment`(50),`info`(50))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

5.添加全文索引

mysql> create table t6
    -> (
    -> id int not null,
    -> info char(255)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> alter table t6 add fulltext index t6(info);
Query OK, 0 rows affected, 1 warning (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show create table t6 \G
*************************** 1. row ***************************
       Table: t6
Create Table: CREATE TABLE `t6` (
  `id` int(11) NOT NULL,
  `info` char(255) DEFAULT NULL,
  FULLTEXT KEY `t6` (`info`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

6.添加空间索引

mysql> create table t7
    -> (
    -> g geometry not null
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> alter table t7 add spatial index t7(g);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t7 \G
*************************** 1. row ***************************
       Table: t7
Create Table: CREATE TABLE `t7` (
  `g` geometry NOT NULL,
  SPATIAL KEY `t7` (`g`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

创建索引create index方式

Create index 已存在的表上创建索引 Create [unique唯一性索引|fulltext全文索引|spatial空间索引] index 索引名 On 表名(col_name[lenth]..[asc|desc]

用create table创建的索引可以不添加索引名(不添加索引名默认为字段名) 但是用create index 添加索引名必须加索引名

先创建一个book1的表

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

1.普通索引

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

2.单列索引

mysql> create index tt2 on book1(comment(44));
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

3.组合索引

mysql> create index tt3 on book1(bookname,comment(44));
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

4.唯一性索引

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

5.全文索引

mysql> create table tt6 
    -> ( id int not null,
    -> info char(255)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> create fulltext index fulldex on tt6(info);
Query OK, 0 rows affected, 1 warning (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 1

6.空间索引

mysql> create table tt7  ( g geometry not null );
Query OK, 0 rows affected (0.00 sec)

mysql> create spatial index spatialt1 on tt7(g);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

三、删除索引

注:添加AUTO_INCREMENT 的约束字段的唯一索引不能删除

Alter table 表名 drop index 索引名;

查看book中有多少索引,准备开始删除

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,
  UNIQUE KEY `bookt2` (`bookid`),
  KEY `year_publication` (`year_publication`),
  KEY `boott1` (`bookname`(30)),
  KEY `bookt13` (`comment`(50)),
  KEY `bookt4` (`comment`(50),`info`(50))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

1.用ALTER TABLE 删除

mysql> alter table book drop index bookt2;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

2.用DROP INDEX删除

mysql> drop index boott1 on book;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

删除表中的列时,如果要删除的列为索引的组成部分,则该列也会从索引中删除。如果组成索引的所有列都被删除,那么整个索引将被删除。

猜你喜欢

转载自blog.csdn.net/weixin_45310323/article/details/111316037