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