- 索引的概念
- 数据库建立索引的原则
- 查看索引
-
- 语法格式
- 示例
- 显示信息蚕食描述
- 普通索引
-
- 概述
- 创建普通索引
-
- 创建方式
- 示例
- 删除索引
-
- 删除索引的方式
- 示例
- 唯一索引
-
- 概述
- 创建唯一索引
-
- 语法格式
- 示例
- 主键索引
-
- 概述
- 创建主键索引
- 全文索引
-
- 概述
- 创建全文索引
-
- 创建方法
- 示例
- 组合索引
-
- 概述
- 创建组合索引
-
- 创建方式
索引的概念
- 是一个排序的列表,存储着索引值和这个值所对应的物理地址
- 无须对整个表进行扫描,通过物理地址就可以找到所需数据
- 是表中一列或者若干列值排序的方法
- 需要额外的磁盘空间
数据库建立索引的原则
- 确定针对该表的操作是大量的查询操作还是大量的增删改操作;
- 尝试建立索引来帮助特定的查询。检查自己的sql语句,为那些频繁在where子句中出现的字段建立索引;
- 尝试建立复合索引来进一步提高系统性能。修改复合索引将消耗更长时间,同时复合索引也占磁盘空间;
- 对于小型的表,建立索引可能会影响性能;
- 应该避免对具有较少值的字段进行索引;
- 避免选择大型数据类型的列作为索引。
查看索引
语法格式
1SHOW INDEX FROM 表名; 2SHOW KEYS FROM表名 ;
示例
1mysql> create table grade( 2 -> 学号 int(16) not null, 3 -> 姓名 char(16) not null, 4 -> 班级 char(16) not null, 5 -> 成绩 int(3) not null, 6 -> primary key(学号)); 7Query OK, 0 rows affected (0.01 sec) 8mysql> show keys from grade; //第一种查看方式 9+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 10| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 11+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 12| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 13+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 141 row in set (0.01 sec) 15 16mysql> show index from grade; //第二种查看方式 17+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 18| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 19+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 20| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 21+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 221 row in set (0.00 sec) 23mysql> show index from grade \G; //末尾加上\G表示竖向查看 24*************************** 1. row *************************** 25 Table: grade 26 Non_unique: 0 27 Key_name: PRIMARY 28 Seq_in_index: 1 29 Column_name: 学号 30 Collation: A 31 Cardinality: 0 32 Sub_part: NULL 33 Packed: NULL 34 Null: 35 Index_type: BTREE 36 Comment: 37Index_comment: 381 row in set (0.00 sec) 39
显示信息蚕食描述
参数
描述
Table
表的名称
Non_unique
索引值得唯一性,0表示唯一,1表示不唯一
Key_name
索引的名称
Seq_in_index
索引的序列号,从1开始
Column_name
列的名称
普通索引
概述
- 最基本的索引类型,没有唯─性之类的限制
- 创建普通索引的方式
创建普通索引
创建方式
1 1. 创建表时创建索引 2 2. CREATE INDEX 索引名 ON 表名 (列名); 3 3. ALTER TABLE 表名 ADD INDEX 索引名 (列名);
示例
1mysql> create table ltp( 2 -> id int(4) not null primary key auto_increment, 3 -> name varchar(10) not null, 4 -> score decimal not null, 5 -> hobby int(2) not null default '1', 6 -> index index_scrore (score)); 7Query OK, 0 rows affected (0.01 sec) 8 9mysql> show keys from ltp; 10+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 11| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 12+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 13| tp | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | 14| tp | 1 | index_scrore | 1 | score | A | 0 | NULL | NULL | | BTREE | | | 15+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 16mysql> create index name on grade(姓名); //使用create方式新增索引 17Query OK, 0 rows affected (0.01 sec) 18Records: 0 Duplicates: 0 Warnings: 0 19 20mysql> show keys from grade; 21+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 22| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 23+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 24| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 25| grade | 1 | name | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 26+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 272 rows in set (0.00 sec) 28mysql> alter table grade add index 姓名(姓名); //使用alter方式新建索引 29Query OK, 0 rows affected, 1 warning (0.00 sec) 30Records: 0 Duplicates: 0 Warnings: 1 31 32mysql> show keys from grade; //查看到新增了一条姓名索引 33+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 34| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 35+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 36| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 37| grade | 1 | name | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 38| grade | 1 | 姓名 | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 39+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 403 rows in set (0.00 sec) 41
删除索引
删除索引的方式
1DROP INDEX 索引名 ON 表名; 2ALTERTABLE 表名 DROP INDEX 索引名;
示例
1mysql> drop index name on grade; //删除name索引 2Query OK, 0 rows affected (0.00 sec) 3Records: 0 Duplicates: 0 Warnings: 0 4 5mysql> show index from grade; //查看,name索引已经被删除 6+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 7| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 8+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 9| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 10| grade | 1 | 姓名 | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 11+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 122 rows in set (0.00 sec) 13 14mysql> alter table grade drop index 姓名; //删除姓名索引 15Query OK, 0 rows affected (0.00 sec) 16Records: 0 Duplicates: 0 Warnings: 0 17 18mysql> show keys from grade; //查看,删除成功 19+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 20| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 21+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 22| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 23+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 241 row in set (0.00 sec) 25
唯一索引
概述
- “普通索引”基本相同
- 与普通索引的区别是索引列的所有值只能出现一次,即必须唯一
- 创建唯一索引的方式
创建唯一索引
语法格式
11.创建表时创建索引 22.CREATE UNIQUE INDEX 索引名 ON 表名(列名); 33.ALTER TABLE 表名 ADD UNIQUE 索引名(列名);
示例
1mysql> create table lllx ( //创建表的方式创建 2 -> id int(4) not null primary key auto_increment, 3 -> name varchar(10) not null, 4 -> score decimal not null, 5 -> hobby int(2) not null default '1', 6 -> unique index index_scrore (score)); 7Query OK, 0 rows affected (0.01 sec) 8 9mysql> show keys from lllx; 10+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 11| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 12+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 13| lllx | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | 14| lllx | 0 | index_scrore | 1 | score | A | 0 | NULL | NULL | | BTREE | | | 15+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 162 rows in set (0.00 sec) 17 18mysql> create unique index name on grade(姓名); //新建name索引 19Query OK, 0 rows affected (0.00 sec) 20Records: 0 Duplicates: 0 Warnings: 0 21 22mysql> show keys from grade; //查看索引,新建成功 23+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 24| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 25+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 26| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 27| grade | 0 | name | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 28+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 292 rows in set (0.00 sec) 30 31mysql> alter table grade drop index name; //删除name索引 32Query OK, 0 rows affected (0.00 sec) 33Records: 0 Duplicates: 0 Warnings: 0 34 35 36mysql> alter table grade add unique 姓名(姓名); //新建姓名索引 37Query OK, 0 rows affected (0.00 sec) 38Records: 0 Duplicates: 0 Warnings: 0 39 40mysql> show index from grade; //查看,新建成功 41+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 42| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 43+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 44| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 45| grade | 0 | 姓名 | 1 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 46+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 472 rows in set (0.00 sec) 48 49mysql> alter table grade drop index 姓名; //删除姓名索引 50Query OK, 0 rows affected (0.00 sec) 51Records: 0 Duplicates: 0 Warnings: 0 52
主键索引
概述
- 是一种特殊的唯一索引,指定为“PRIMARY KEY",
- 一个表只能有一个主键,不允许有空值
- 创建表时必须创建,创建后不能删除
创建主键索引
1mysql> create table test( //创建表的方式创建索引 2 -> id int(10) not null auto_increment, 3 -> title char(255) not null, 4 -> primary key (`id`)); 5Query OK, 0 rows affected (0.01 sec) 6 7mysql> show keys from test; 8+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 9| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 10+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 11| test | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | 12+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 131 row in set (0.00 sec) 14
全文索引
概述
- MySQL从3.23.23版开始支持全文索引和全文检索
- 索引类型为FULLTEXT
- 可以在CHAR、VARCHAR或者TEXT类型的列上创建
创建全文索引
创建方法
1在创建表时创建索引 2CREATE FULLTEXT INDEX 索引名 ON 表名(列名); 3ALTER TABLE 表名 ADD FULLTEXT 索引名(列名);
示例
1mysql> create table article ( 新建方式创建索引 2 -> 标题 char(48) not null, 3 -> 目录 varchar(255) default null, 4 -> 正文 varchar(8096) not null, 5 -> primary key (标题), 6 -> fulltext (正文)); 7Query OK, 0 rows affected (0.34 sec) 8 9mysql> show keys from article; 10+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 11| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 12+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 13| article | 0 | PRIMARY | 1 | 标题 | A | 0 | NULL | NULL | | BTREE | | | 14| article | 1 | 正文 | 1 | 正文 | NULL | 0 | NULL | NULL | | FULLTEXT | | | 15+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 162 rows in set (0.00 sec) 17 18mysql> alter table article add fulltext page(目录); //alter方式新创建page索引 19Query OK, 0 rows affected (0.02 sec) 20Records: 0 Duplicates: 0 Warnings: 0 21 22mysql> show keys from article; //查看,创建成功 23+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 24| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 25+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 26| article | 0 | PRIMARY | 1 | 标题 | A | 0 | NULL | NULL | | BTREE | | | 27| article | 1 | 正文 | 1 | 正文 | NULL | 0 | NULL | NULL | | FULLTEXT | | | 28| article | 1 | page | 1 | 目录 | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | 29+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 303 rows in set (0.00 sec) 31 32mysql> drop index page on article; //删除page索引 33Query OK, 0 rows affected (0.02 sec) 34Records: 0 Duplicates: 0 Warnings: 0 35 36mysql> create fulltext index mulu on article(目录); //使用create方式创建mulu索引 37Query OK, 0 rows affected (0.02 sec) 38Records: 0 Duplicates: 0 Warnings: 0 39 40mysql> show index from article; 查看,创建成功 41+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 42| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 43+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 44| article | 0 | PRIMARY | 1 | 标题 | A | 0 | NULL | NULL | | BTREE | | | 45| article | 1 | 正文 | 1 | 正文 | NULL | 0 | NULL | NULL | | FULLTEXT | | | 46| article | 1 | mulu | 1 | 目录 | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | 47+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 483 rows in set (0.00 sec) 49 50
组合索引
概述
- 可以是单列上创建的索引,也可以是在多列上创建的索引
- 最左原则,从左往右依次执行
- 创建组合索引的方式
创建组合索引
创建方式
11.创建表时创建索引 22.CREATE UNIQUE INDEX 索引名 ON 表名(列名1,列名2,……); 33.ALTER TABLE 表名 ADD UNIQUE 索引名(列名1,列名2,……); 4 5 6mysql> create table users ( //创建表的方式创建 7 -> name char(9), 8 -> age int(3), 9 -> sex tinyint(1), 10 -> index user (name,age,sex)); 11Query OK, 0 rows affected (0.00 sec) 12mysql> show keys from user; 13+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 14| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 15+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 16| user | 1 | user | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | | 17| user | 1 | user | 2 | age | A | 0 | NULL | NULL | YES | BTREE | | | 18| user | 1 | user | 3 | sex | A | 0 | NULL | NULL | YES | BTREE | | | 19+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 203 rows in set (0.01 sec) 21 22mysql> create unique index student on grade(学号,姓名,成绩); //给学号,姓名,成绩这几列创建索引,名为student 23Query OK, 0 rows affected (0.01 sec) 24Records: 0 Duplicates: 0 Warnings: 0 25 26mysql> show index from grade; //查看,创建成功 27+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 28| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 29+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 30| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 31| grade | 0 | student | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 32| grade | 0 | student | 2 | 姓名 | A | 0 | NULL | NULL | | BTREE | | | 33| grade | 0 | student | 3 | 成绩 | A | 0 | NULL | NULL | | BTREE | | | 34+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 354 rows in set (0.00 sec) 36 37mysql> drop index student on grade; //删除student索引 38Query OK, 0 rows affected (0.01 sec) 39Records: 0 Duplicates: 0 Warnings: 0 40 41mysql> alter table grade add fulltext 学生(姓名,班级); //给姓名,班级两列创建全文索引,索引名为学生 42Query OK, 0 rows affected, 1 warning (0.05 sec) 43Records: 0 Duplicates: 0 Warnings: 1 44 45mysql> show keys from grade; 查看,创建成功 46+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 47| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 48+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 49| grade | 0 | PRIMARY | 1 | 学号 | A | 0 | NULL | NULL | | BTREE | | | 50| grade | 1 | 学生 | 1 | 姓名 | NULL | 0 | NULL | NULL | | FULLTEXT | | | 51| grade | 1 | 学生 | 2 | 班级 | NULL | 0 | NULL | NULL | | FULLTEXT | | | 52+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 533 rows in set (0.00 sec)