目录
[2.1show index from tablename](#2.1show index from tablename)
[2.2show keys from tablename](#2.2show keys from tablename)
一、索引介绍
索引:是排序的快速查找的特殊数据结构,定义作为查找条件的字段上,又称为键key,索引通过存储引擎实现
1.索引的概念
- 索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址(类似于C语言的链表通过指针指向数据记录的内存地址)
- 使用索引后可以不用扫描全表来定位某行的数据,而是先通过索引表找到该行数据对应的物理地址然后访问相应的数据,因此能加快数据库的查询速度。
- 索引就好比是一本书的目录,可以根据目录中的页码快速找到所需的内容。
- 索引是表中一列或者若干列值排序的方法。
- 建立索引的目的是加快对表中记录的查找或排序。
- 需要额外的磁盘空间
2.索引的作用
- 加快查询速度,提高数据库性能
- 设置了合适的索引之后,数据库利用各种快速定位技术,能够大大加快查询速度,这是创建索引的最主要的原因。
- 当表很大或查询涉及到多个表时,使用索引可以成千上万倍地提高查询速度。避免排序和使用临时表
- 可以降低数据库的IO成本(减少io次数),并且索引还可以降低数据库的排序成本。将随机I/O转为顺序I/O
- 通过创建唯一性索引,可以保证数据表中每一行数据的唯一性。
- 可以加快表与表之间的连接。
- 在使用分组和排序时,可大大减少分组和排序的时间。
- 建立索引在搜索和恢复数据库中的数据时能显著提高性能
3.索引的缺点
- 索引需要占用额外的磁盘空间
- 在插入和修改数据时要花费更多的时间,因为索引也要随之变动
对于 MyISAM 引擎而言,索引文件和数据文件是分离的,索引文件用于保存数据记录的地址。而 InnoDB 引擎的表数据文件本身就是索引文件。
4.创建索引的原则依据
索引虽可以提升数据库查询的速度,但并不是任何情况下都适合创建索引。因为索引本身会消耗系统资源,在有索引的情况下,数据库会先进行索引查询,然后定位到具体的数据行,如果索引使用不当,反而会增加数据库的负担。
- 表的主键、外键必须有索引。因为主键具有唯一性,外键关联的是主表的主键,查询时可以快速定位。
- 记录数超过300行的表应该有索引。如果没有索引,每次查询都需要把表遍历一遍,会严重影响数据库的性能。
- 经常与其他表进行连接的表,在连接字段上应该建立索引。
- 唯一性太差的字段不适合建立索引。
- 更新太频繁地字段不适合创建索引。
- 经常出现在 where 子句中的字段,特别是大表的字段,应该建立索引。
- 在经常进行 GROUP BY、ORDER BY 的字段上建立索引;
- 索引应该建在选择性高的字段上。
- 索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引。
5.索引优化
- 独立地使用列:尽量避免其参与运算,独立的列指索引列不能是表达式的一部分,也不能是函数的参数,在where条件中,始终将索引列单独放在比较符号的一侧,尽量不要在列上进行运算(函数操作和表达式操作)
- 左前缀索引:构建指定索引字段的左侧的字符数,要通过索引选择性(不重复的索引值和数据表的记录总数的比值)来评估,尽量使用短索引,如果可以,应该制定一个前缀长度
- 多列索引:AND操作时更适合使用多列索引,而非为每个列创建单独的索引
- 选择合适的索引列顺序:无排序和分组时,将选择性最高放左侧
- 只要列中含有NULL值,就最好不要在此列设置索引,复合索引如果有NULL值,此列在使用时也不会使用索引
- 对于经常在where子句使用的列,最好设置索引
- 对于有多个列where或者order by子句,应该建立复合索引
- 对于like语句,以 % 或者 _ 开头的不会使用索引,以 % 结尾会使用索引
- 尽量不要使用not in和<>操作,虽然可能使用索引,但性能不高
- 不要使用RLIKE正则表达式会导致索引失效
- 查询时,能不要就不用,尽量写全字段名,比如:select id,name,age from students;
- 大部分情况连接效率远大于子查询
- 在有大量记录的表分页时使用limit
- 对于经常使用的查询,可以开启查询缓存
- 多使用explain和profile分析查询语句
- 查看慢查询日志,找出执行时间长的sql语句优化
Mysql的优化哪些字段/场景适合创建索引,哪些不适合?
- 小字段
- 唯一性强的字段
- 更新不频繁,但查询率很高的字段
- 表记录超过300+行
- 主键、外键、唯一键
二、索引的分类和创建
1.索引分类
- 普通索性:最基本的索引类型,没有唯一性之类的限制
- 唯一索引:与普通索引类似,但区别是唯一索引列的每个值都是唯一
- 主键索引:是一种特殊的唯一索引,必须指定为"Primary key"
- 组合索引:可以是单列上创建的索引,也可以是在多列上创建的索引
- 全文索引:适合在进行模糊查询的时候使用,可用于在一篇文章中检索文本信息
1.1普通索引
最基本的索引类型,没有唯一性之类的限制。
sql
mysql> create table class(id int not null,name char(8),grade decimal(4,2),remarrk text);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into class values(2,'wyb',92,'this is svip');
Query OK, 1 row affected (0.00 sec)
mysql> insert into class values(3,'xzq',96,'this is vip');
Query OK, 1 row affected (0.00 sec)
mysql> insert into class values(4,'zs',95,'he is single');
Query OK, 1 row affected (0.00 sec)
mysql> select * from class;
+----+------+-------+--------------+
| id | name | grade | remark |
+----+------+-------+--------------+
| 1 | cxk | 88.00 | this is svip |
| 2 | wyb | 92.00 | this is svip |
| 3 | xzq | 96.00 | this is vip |
| 4 | zs | 95.00 | he is single |
+----+------+-------+--------------+
4 rows in set (0.00 sec)
1.1.1直接创建索引
CREATE INDEX 索引名 ON 表名 (列名[(length)])
(列名(length)):length是可选项。如果忽略 length 的值,则使用整个列的值作为索引。如果指定使用列前的 length 个字符来创建索引,这样有利于减小索引文件的大小。
索引名建议以"_index"结尾。
sql
mysql> show create table class;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
KEY "grade_index" ("grade"),
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> create table test(id int not null,name varchar(15),cardid char(18) not null,index id_index (id));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table test;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE "test" (
"id" int(11) NOT NULL,
"name" varchar(15) DEFAULT NULL,
"cardid" char(18) NOT NULL,
KEY "id_index" ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.1.2修改表方式创建
ALTER TABLE 表名 ADD INDEX 索引名 (列名)
sql
mysql> desc class;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(8) | YES | | NULL | |
| grade | decimal(4,2) | YES | MUL | NULL | |
| remark | text | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table class add index name_index (name);
#增加一条索引 索引名称为name_index 索引调用name列
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table class;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
KEY "grade_index" ("grade"),
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select name from class;
+------+
| name |
+------+
| cxk |
| wyb |
| xzq |
| zs |
+------+
4 rows in set (0.00 sec)
1.1.3创建表的时候指定索引
CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名))
sql
mysql> desc class
-> ;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(8) | YES | | NULL | |
| grade | decimal(4,2) | YES | | NULL | |
| remark | text | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> create index grade_index on class(grade);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select grade from class;
+-------+
| grade |
+-------+
| 88.00 |
| 92.00 |
| 95.00 |
| 96.00 |
+-------+
4 rows in set (0.00 sec)
sql
mysql> show create table class;
#显示创建数据表的过程
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
KEY "grade_index" ("grade")
#直接创建索引 索引名称为"grade_index" 索引class的grade列
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.2唯一索引
与普通索引类似,但区别是唯一索引列的每个值都唯一。
唯一索引允许有空值(注意和主键不同)。如果是用组合索引创建,则列值的组合必须唯一。添加唯一键将自动创建唯一索引。
unique key 就是唯一索引,唯一索引就是唯一键
1.2.1直接创建唯一索引
CREATE UNIQUE INDEX 索引名 ON 表名(列名)
sql
mysql> create unique index address_index on class(address);
#创建唯一索引 索引名为address_index 索引class数据表中的address列
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table class;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
KEY "grade_index" ("grade"),
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.2.2修改表方式创建
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名)
sql
mysql> alter table class add unique phone_index (phone);
#新增一条唯一索引 索引表为class数据表 索引列为phone
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table class;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
UNIQUE KEY "phone_index" ("phone"),
KEY "grade_index" ("grade"),
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.2.3创建表的时候指定
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名))
sql
mysql> create table test2 (id int,name char(6),grade varchar(16),unique grade_index (grade));
#创建test2数据表 数据结构为id 整数型 6固定字节;grade 字节16位 唯一索引为grade列
Query OK, 0 rows affected (0.10 sec)
mysql> show create table test2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE "test2" (
"id" int(11) DEFAULT NULL,
"name" char(6) DEFAULT NULL,
"grade" varchar(16) DEFAULT NULL,
UNIQUE KEY "grade_index" ("grade")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.3主键索引
是一种特殊的唯一索引,必须指定为"PRIMARY KEY"。
一个表只能有一个主键,不允许有空值。 添加主键将自动创建主键索引。
1.3.1创建表的时候指定
CREATE TABLE 表名 ([...],PRIMARY KEY (列名))
sql
mysql> create table test3(id int,name char(8),primary key(id));
#创建test3数据表 数据结构为id name 唯一主键为id primary key为指定主键
Query OK, 0 rows affected (0.00 sec)
mysql> show create table test3;
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
| test3 | CREATE TABLE "test3" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.3.2修改表方式创建
ALTER TABLE 表名 ADD PRIMARY KEY (列名)
sql
mysql> alter table test add primary key(name);
#修改test数据表的主键索引 逐渐修改为name
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from test\G;
*************************** 1. row ***************************
Table: test
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: test
Non_unique: 1
Key_name: id_index
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.00 sec)
ERROR:
No query specified
1.4组合索引------单列所用和多列索引
可以是单列上创建的索引,也可以是在多列上创建的索引。需要满足最左原则,因为select语句的 where条件是依次从左往右执行的,所以在使用select语句查询时where条件使用的字段顺序必须和组合索引中的排序一致,否则索引将不会生效。
sql
mysql> create table test4 (id int not null,name varchar(8),cardid char(11),index test_index (id,name));
#新建一个test4数据表 数据结构为id name cardid 组合索引为id,name
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test4 values(1,'wuyq',111111);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test4 values(2,'fangwl',222222);
Query OK, 1 row affected (0.00 sec)
mysql> select name,id from test4;
#使用的是select遍历的功能查找到所需要的数据 按照索引从左到右检索的顺序,则不会触发组合索引
+--------+----+
| name | id |
+--------+----+
| wuyq | 1 |
| fangwl | 2 |
+--------+----+
2 rows in set (0.00 sec)
mysql> select id,name from test4;
#使用的是组合索引的功能查找到所需要的数据
+----+--------+
| id | name |
+----+--------+
| 1 | wuyq |
| 2 | fangwl |
+----+--------+
2 rows in set (0.00 sec)
mysql> select name,id from test4;
#使用的是select遍历的功能查找到所需要的数据 按照索引从左到右检索的顺序,则不会触发组合索引
+--------+----+
| name | id |
+--------+----+
| wuyq | 1 |
| fangwl | 2 |
+--------+----+
2 rows in set (0.00 sec)
mysql> show create table test4;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test4 | CREATE TABLE "test4" (
"id" int(11) NOT NULL,
"name" varchar(8) DEFAULT NULL,
"cardid" char(11) DEFAULT NULL,
KEY "test_index" ("id","name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
组合索引创建的字段顺序是其触发索引的查询顺序
1.5全文索引
适合在进行模糊查询的时候使用,可用于在一篇文章中检索文本信息。
在 MySQL5.6 版本以前FULLTEXT 索引仅可用于 MyISAM 引擎,在 5.6 版本之后 innodb 引擎也支持 FULLTEXT 索引。全文索引可以在 CHAR、VARCHAR 或者 TEXT 类型的列上创建。每个表只允许有一个全文索引。
1.5.1直接创建索引
sql
mysql> create fulltext index remark_index on class(remark);
#创建全文索引 索引名称为remark_index 索引表为class 索引列为remark
Query OK, 0 rows affected, 1 warning (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show create table class;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
"cardid" int(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
UNIQUE KEY "phone_index" ("phone"),
KEY "grade_index" ("grade"),
KEY "name_index" ("name"),
FULLTEXT KEY "remark_index" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
1.5.2修改表方式创建
sql
mysql> select * from test3;
Empty set (0.00 sec)
mysql> select * from test4;
+----+--------+--------+
| id | name | cardid |
+----+--------+--------+
| 1 | wuyq | 111111 |
| 2 | fangwl | 222222 |
+----+--------+--------+
2 rows in set (0.00 sec)
mysql> alter table test4 add fulltext name_index (name);
Query OK, 0 rows affected, 1 warning (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> select name from test4;
+--------+
| name |
+--------+
| wuyq |
| fangwl |
+--------+
2 rows in set (0.00 sec)
1.5.3创建表的时候指定索引
sql
mysql> create table test5(id int not null,name char(6),fulltext name_index (name));
Query OK, 0 rows affected (0.58 sec)
1.5.4使用全文索引查询
sql
mysql> select * from class where remark 'this is svip';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''this is svip'' at line 1
mysql> select * from class where match(remark) against('this is svip');
+----+------+-------+--------------+---------+-------+--------+
| id | name | grade | remark | address | phone | cardid |
+----+------+-------+--------------+---------+-------+--------+
| 1 | cxk | 88.00 | this is svip | nanjing | 110 | 111111 |
| 2 | wyb | 92.00 | this is svip | beijing | 112 | 222222 |
+----+------+-------+--------------+---------+-------+--------+
2 rows in set (0.00 sec)
sql
mysql> select * from class;
+----+------+-------+--------------+----------+-------+--------+
| id | name | grade | remark | address | phone | cardid |
+----+------+-------+--------------+----------+-------+--------+
| 1 | cxk | 88.00 | this is svip | nanjing | 110 | 111111 |
| 2 | wyb | 92.00 | this is svip | beijing | 112 | 222222 |
| 3 | xzq | 96.00 | this is vip | shanghai | 114 | 333333 |
| 4 | zs | 95.00 | he is single | yunnan | 119 | 444444 |
+----+------+-------+--------------+----------+-------+--------+
4 rows in set (0.00 sec)
mysql> select * from class where address='nanjing';
+----+------+-------+--------------+---------+-------+--------+
| id | name | grade | remark | address | phone | cardid |
+----+------+-------+--------------+---------+-------+--------+
| 1 | cxk | 88.00 | this is svip | nanjing | 110 | 111111 |
+----+------+-------+--------------+---------+-------+--------+
1 row in set (0.00 sec)
1.6索引分类总结
- 普通索引:针对所有,没有特殊的需求/规则
- 唯一索引:针对唯一性的字段,可以出现空值
- 组合索引:多列/多字段组合形式的索引
- 全文索引:可以在char/varchar/text上创建,Mysql为了优化对文本内容搜索的一种机制
- 主键索引:针对唯一性字段,且不可为空,同时一张表只允许包含一个主键索引
1.7创建索引总结
- 在创建的时候,直接指定index
- alter修改表结构的时候,进行add添加index
- 直接创建索引index
主键索引:直接创建主键即可
2.查看索引
2.1show index from tablename
sql
mysql> show index from class;
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| class | 0 | address_index | 1 | address | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 0 | phone_index | 1 | phone | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 1 | grade_index | 1 | grade | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 1 | name_index | 1 | name | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 1 | remark_index | 1 | remark | NULL | 4 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5 rows in set (0.00 sec)
mysql> show index from class\G;
*************************** 1. row ***************************
Table: class
Non_unique: 0
Key_name: address_index
Seq_in_index: 1
Column_name: address
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: class
Non_unique: 0
Key_name: phone_index
Seq_in_index: 1
Column_name: phone
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: class
Non_unique: 1
Key_name: grade_index
Seq_in_index: 1
Column_name: grade
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 4. row ***************************
Table: class
Non_unique: 1
Key_name: name_index
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 5. row ***************************
Table: class
Non_unique: 1
Key_name: remark_index
Seq_in_index: 1
Column_name: remark
Collation: NULL
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: FULLTEXT
Comment:
Index_comment:
5 rows in set (0.00 sec)
ERROR:
No query specified
2.2show keys from tablename
sql
mysql> show keys from class;
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| class | 0 | address_index | 1 | address | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 0 | phone_index | 1 | phone | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 1 | grade_index | 1 | grade | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 1 | name_index | 1 | name | A | 4 | NULL | NULL | YES | BTREE | | |
| class | 1 | remark_index | 1 | remark | NULL | 4 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5 rows in set (0.00 sec)
mysql> show keys from class\G;
*************************** 1. row ***************************
Table: class
Non_unique: 0
Key_name: address_index
Seq_in_index: 1
Column_name: address
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: class
Non_unique: 0
Key_name: phone_index
Seq_in_index: 1
Column_name: phone
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: class
Non_unique: 1
Key_name: grade_index
Seq_in_index: 1
Column_name: grade
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 4. row ***************************
Table: class
Non_unique: 1
Key_name: name_index
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 5. row ***************************
Table: class
Non_unique: 1
Key_name: remark_index
Seq_in_index: 1
Column_name: remark
Collation: NULL
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: FULLTEXT
Comment:
Index_comment:
5 rows in set (0.00 sec)
ERROR:
No query specified
2.3索引字段总结
索引字段名称 | 含义 |
---|---|
table | 数据表名 |
Non_unique | 如果索引内容唯一,则为 0;如果可以不唯一,则为 1 |
Seq_in_index | 索引中的列序号,从 1 开始。 limit 2,3 |
Column_name | 列名称 |
Collation | 列以什么方式存储在索引中。在 MySQL 中,有值'A'(升序)或 NULL(无分类) |
Cardinality | 索引中唯一值数目的估计值 |
Sub_part | 如果列只是被部分地编入索引,则为被编入索引的字符的数目(zhangsan)。如果整列被编入索引,则为 NULL |
Packed | 指示关键字如何被压缩。如果没有被压缩,则为 NULL |
Null | 如果列含有 NULL,则含有 YES。如果没有,则该列含有 NO。 |
Index_type | 用过的索引方法(BTREE, FULLTEXT, HASH, RTREE) |
Comment | 备注 |
3.删除索引
3.1直接删除索引
DROP INDEX 索引名 ON 表名
sql
mysql> show create table class;
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
"cardid" int(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
UNIQUE KEY "phone_index" ("phone"),
KEY "grade_index" ("grade"),
KEY "name_index" ("name"),
FULLTEXT KEY "remark_index" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
mysql> drop index name_index on class;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table class;
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
"cardid" int(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
UNIQUE KEY "phone_index" ("phone"),
KEY "grade_index" ("grade"),
FULLTEXT KEY "remark_index" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
3.2修改表方式删除索引
ALTER TABLE 表名 DROP INDEX 索引名
sql
mysql> show create table class;
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
"cardid" int(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
UNIQUE KEY "phone_index" ("phone"),
KEY "grade_index" ("grade"),
FULLTEXT KEY "remark_index" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
mysql> alter table class drop index phone_index;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table class;
+-------+-----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
"cardid" int(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
KEY "grade_index" ("grade"),
FULLTEXT KEY "remark_index" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
mysql> alter table class drop index grade_index;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table class;
+-------+----------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------+
| class | CREATE TABLE "class" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
"grade" decimal(4,2) DEFAULT NULL,
"remark" text,
"address" char(20) DEFAULT NULL,
"phone" char(11) DEFAULT NULL,
"cardid" int(11) DEFAULT NULL,
UNIQUE KEY "address_index" ("address"),
FULLTEXT KEY "remark_index" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3.3删除主键索引
ALTER TABLE 表名 DROP PRIMARY KEY
sql
mysql> show create table test3;
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
| test3 | CREATE TABLE "test3" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL,
PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table test3 drop primary key;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test3;
+-------+--------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------+
| test3 | CREATE TABLE "test3" (
"id" int(11) NOT NULL,
"name" char(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)