群晖wordpress搭建网站,网站建设及管理,建设部网站证书查询,wordpress 图片压缩目录
一、索引介绍
1.索引的概念
2.索引的作用
3.索引的缺点
4.创建索引的原则依据
5.索引优化
二、索引的分类和创建
1.索引分类
1.1普通索引
1.1.1直接创建索引
1.1.2修改表方式创建
1.1.3创建表的时候指定索引
1.2唯一索引
1.2.1直接创建唯一索引
1.2.2修改表…目录
一、索引介绍
1.索引的概念
2.索引的作用
3.索引的缺点
4.创建索引的原则依据
5.索引优化
二、索引的分类和创建
1.索引分类
1.1普通索引
1.1.1直接创建索引
1.1.2修改表方式创建
1.1.3创建表的时候指定索引
1.2唯一索引
1.2.1直接创建唯一索引
1.2.2修改表方式创建
1.2.3创建表的时候指定
1.3主键索引
1.3.1创建表的时候指定
1.3.2修改表方式创建
1.4组合索引——单列所用和多列索引
1.5全文索引
1.5.1直接创建索引
1.5.2修改表方式创建
1.5.3创建表的时候指定索引
1.5.4使用全文索引查询
1.6索引分类总结
1.7创建索引总结
2.查看索引
2.1show index from tablename
2.2show keys from tablename
2.3索引字段总结
3.删除索引
3.1直接删除索引
3.2修改表方式删除索引
3.3删除主键索引 一、索引介绍
索引是排序的快速查找的特殊数据结构定义作为查找条件的字段上又称为键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普通索引 最基本的索引类型没有唯一性之类的限制。
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”结尾。 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)1.1.2修改表方式创建
ALTER TABLE 表名 ADD INDEX 索引名 (列名)
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: 0mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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 索引名 (列名))
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: 0mysql select grade from class;
-------
| grade |
-------
| 88.00 |
| 92.00 |
| 95.00 |
| 96.00 |
-------
4 rows in set (0.00 sec)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列
) ENGINEInnoDB DEFAULT CHARSETutf8 |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)1.2唯一索引
与普通索引类似但区别是唯一索引列的每个值都唯一。 唯一索引允许有空值注意和主键不同。如果是用组合索引创建则列值的组合必须唯一。添加唯一键将自动创建唯一索引。 unique key 就是唯一索引唯一索引就是唯一键 1.2.1直接创建唯一索引
CREATE UNIQUE INDEX 索引名 ON 表名(列名)
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: 0mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
1.2.2修改表方式创建
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名)
mysql alter table class add unique phone_index (phone);
#新增一条唯一索引 索引表为class数据表 索引列为phone
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)1.2.3创建表的时候指定
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名))
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
1.3主键索引
是一种特殊的唯一索引必须指定为“PRIMARY KEY”。 一个表只能有一个主键不允许有空值。 添加主键将自动创建主键索引。
1.3.1创建表的时候指定
CREATE TABLE 表名 ([...],PRIMARY KEY (列名))
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
-------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)1.3.2修改表方式创建
ALTER TABLE 表名 ADD PRIMARY KEY (列名)
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: testNon_unique: 0Key_name: PRIMARYSeq_in_index: 1Column_name: nameCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: Index_type: BTREEComment:
Index_comment:
*************************** 2. row ***************************Table: testNon_unique: 1Key_name: id_indexSeq_in_index: 1Column_name: idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: Index_type: BTREEComment:
Index_comment:
2 rows in set (0.00 sec)ERROR:
No query specified1.4组合索引——单列所用和多列索引
可以是单列上创建的索引也可以是在多列上创建的索引。需要满足最左原则因为select语句的 where条件是依次从左往右执行的所以在使用select语句查询时where条件使用的字段顺序必须和组合索引中的排序一致否则索引将不会生效。
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec) 组合索引创建的字段顺序是其触发索引的查询顺序 1.5全文索引
适合在进行模糊查询的时候使用可用于在一篇文章中检索文本信息。 在 MySQL5.6 版本以前FULLTEXT 索引仅可用于 MyISAM 引擎在 5.6 版本之后 innodb 引擎也支持 FULLTEXT 索引。全文索引可以在 CHAR、VARCHAR 或者 TEXT 类型的列上创建。每个表只允许有一个全文索引。
1.5.1直接创建索引
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: 1mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
1.5.2修改表方式创建
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: 1mysql select name from test4;
--------
| name |
--------
| wuyq |
| fangwl |
--------
2 rows in set (0.00 sec)1.5.3创建表的时候指定索引
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使用全文索引查询
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)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 addressnanjing;
-------------------------------------------------------
| 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创建索引总结 在创建的时候直接指定indexalter修改表结构的时候进行add添加index直接创建索引index 主键索引直接创建主键即可 2.查看索引
2.1show index from tablename
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: classNon_unique: 0Key_name: address_indexSeq_in_index: 1Column_name: addressCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 2. row ***************************Table: classNon_unique: 0Key_name: phone_indexSeq_in_index: 1Column_name: phoneCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 3. row ***************************Table: classNon_unique: 1Key_name: grade_indexSeq_in_index: 1Column_name: gradeCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 4. row ***************************Table: classNon_unique: 1Key_name: name_indexSeq_in_index: 1Column_name: nameCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 5. row ***************************Table: classNon_unique: 1Key_name: remark_indexSeq_in_index: 1Column_name: remarkCollation: NULLCardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: FULLTEXTComment:
Index_comment:
5 rows in set (0.00 sec)ERROR:
No query specified2.2show keys from tablename
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: classNon_unique: 0Key_name: address_indexSeq_in_index: 1Column_name: addressCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 2. row ***************************Table: classNon_unique: 0Key_name: phone_indexSeq_in_index: 1Column_name: phoneCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 3. row ***************************Table: classNon_unique: 1Key_name: grade_indexSeq_in_index: 1Column_name: gradeCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 4. row ***************************Table: classNon_unique: 1Key_name: name_indexSeq_in_index: 1Column_name: nameCollation: ACardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
*************************** 5. row ***************************Table: classNon_unique: 1Key_name: remark_indexSeq_in_index: 1Column_name: remarkCollation: NULLCardinality: 4Sub_part: NULLPacked: NULLNull: YESIndex_type: FULLTEXTComment:
Index_comment:
5 rows in set (0.00 sec)ERROR:
No query specified2.3索引字段总结
索引字段名称含义table数据表名Non_unique如果索引内容唯一则为 0如果可以不唯一则为 1Seq_in_index索引中的列序号从 1 开始。 limit 2,3Column_name列名称Collation列以什么方式存储在索引中。在 MySQL 中有值‘A’升序或 NULL无分类Cardinality索引中唯一值数目的估计值Sub_part如果列只是被部分地编入索引则为被编入索引的字符的数目(zhangsan)。如果整列被编入索引则为 NULLPacked指示关键字如何被压缩。如果没有被压缩则为 NULLNull如果列含有 NULL则含有 YES。如果没有则该列含有 NO。Index_type用过的索引方法BTREE, FULLTEXT, HASH, RTREEComment备注
3.删除索引
3.1直接删除索引
DROP INDEX 索引名 ON 表名
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |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: 0mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |1 row in set (0.00 sec)
3.2修改表方式删除索引
ALTER TABLE 表名 DROP INDEX 索引名
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
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: 0mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
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: 0mysql 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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
-----------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
3.3删除主键索引
ALTER TABLE 表名 DROP PRIMARY KEY
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)
) ENGINEInnoDB DEFAULT CHARSETutf8 |
-------------------------------------------------------------------------------------------------------------------------------------------------
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: 0mysql show create table test3;
---------------------------------------------------------------------------------------------------------------------------
| Table | Create Table |
---------------------------------------------------------------------------------------------------------------------------
| test3 | CREATE TABLE test3 (id int(11) NOT NULL,name char(8) DEFAULT NULL
) ENGINEInnoDB DEFAULT CHARSETutf8 |
---------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)