Linux系统——Mysql索引详解

目录

一、索引介绍

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")
) 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 索引名 (列名)

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")
) 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 索引名 (列名))

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列
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------+
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 specified

1.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")
) 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直接创建索引
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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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 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

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 specified

2.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 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 表名

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: 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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |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")
) 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: 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")
) 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: 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")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------+
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")
) 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: 0mysql> 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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/762525.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql 空间查询 多边形内的点

数据库查询 # 1新增空间point类型坐标字段 ALTER TABLE gaoxin_isdp.business_master ADD COLUMN location2 point NULL AFTER location;# 2从原字段更新点位字段&#xff0c;原字段poi1是字符串106.474596,29.464360 UPDATE business_master SET location POINT(substr(poi…

基于Springboot+Vue的前后端分离的简单Demo案例(一)

后端创建Springboot项目 创建数据库表结构及表信息 添加依赖&#xff08;pom.xml&#xff09; <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/X…

微服务网关介绍

1. 为什么是Spring Cloud Gateway 一句话&#xff0c;Spring Cloud已经放弃Netflix Zuul了。现在Spring Cloud中引用的还是Zuul 1.x版本&#xff0c;而这个版本是基于过滤器的&#xff0c;是阻塞IO&#xff0c;不支持长连接。Zuul 2.x版本跟1.x的架构大一样&#xff0c;性能也有…

GraalVM详细安装及打包springboot、java、javafx使用教程(打包普通JAVA项目篇)

前言 在当前多元化开发环境下&#xff0c;Java作为一种广泛应用的编程语言&#xff0c;其应用部署效率与灵活性的重要性日益凸显。Spring Boot框架以其简洁的配置和强大的功能深受开发者喜爱&#xff0c;而JavaFX则为开发者提供了构建丰富桌面客户端应用的能力。然而&#xff…

ArcGis 地图文档

ArcGis官网 https://developers.arcgis.com/labs/android/create-a-starter-app/ Arcgis for android 加载谷歌、高德和天地图 https://blog.csdn.net/qq_19688207/article/details/108125778 AeroMap图层地址: API_KEY: 7e95eae2-a18d-34ce-beaa-894d6a08c5a5 街道图&#xf…

Python模块与包管理使用pip与virtualenv【第151篇—模块与包管理】

Python模块与包管理&#xff1a;使用pip与virtualenv 在Python开发中&#xff0c;模块和包管理是至关重要的&#xff0c;它们使得代码的组织、重用和共享变得更加简单和高效。本文将介绍两个Python生态系统中最常用的工具&#xff1a;pip和virtualenv。通过这些工具&#xff0…

探索医用制氧机的种类及其应用场景

医用制氧机是一种能够制取高纯度氧气的制氧设备&#xff0c;广泛应用于各种场景。随着科技的进步和行业需求的增加&#xff0c;医用制氧机的种类和应用领域也在不断扩展。本文将探索医用制氧机的多元种类及其应用领域&#xff0c;以更好地了解这一设备的重要性。 一、医用制氧机…

docker构建镜像命令

编写dockerfile文件 例子1; FROM oraclelinux:7-slim ENV release19 ENV update13 RUN curl -o /etc/yum.repos.d/public-yum-ol7.repo https://yum.oracle.com/public-yum-ol7.repo && \yum-config-manager --enable ol7_oracle_instantclient && \yum in…

Git:分布式版本控制系统

目录 Git的特点和功能常见的功能和对应的命令 Git的特点和功能 Git是一个分布式版本控制系统&#xff0c;用于跟踪和管理项目的代码变更。它是由Linus Torvalds在2005年创建的&#xff0c;旨在管理Linux内核的开发。Git具有以下特点和功能&#xff1a; 分布式版本控制&#xf…

企业数字化问题,一个系统能否解决?

在企业数字化转型的过程中&#xff0c;很多企业都会遇到各种各样的问题。有时候&#xff0c;这些问题并不仅仅是技术上的挑战&#xff0c;而更多地涉及到企业管理和运营的主要问题。贪大求全、策略失误、软件公司的“上游路线”等现象&#xff0c;都可能成为企业数字化建设失败…

java并发编程之 volatile关键字

1、简单介绍一下JMM Java 内存模型&#xff08;Java Memory Model 简称JMM&#xff09;是一种抽象的概念&#xff0c;并不真实存在&#xff0c;指一组规则或规范&#xff0c;通过这组规范定义了程序中各个变量的访问方式。java内存模型(JMM)屏蔽掉各种硬件和操作系统的内存访问…

快速区分清楚图形渲染中的AABB,KD树和BVH这些概念

快速区分清楚图形渲染中的AABB&#xff0c;KD树和BVH这些概念 主要想形象去区分好这些术语&#xff0c;目的是扫盲&#xff0c;先开好坑&#xff0c;内容持续填充。 0.先摆出这些词的全称 AABB&#xff1a; 原名&#xff1a;axis aligned bounding box&#xff1b;中文直译名…

流畅的 Python 第二版(GPT 重译)(二)

第三章&#xff1a;字典和集合 Python 基本上是用大量语法糖包装的字典。 Lalo Martins&#xff0c;早期数字游牧民和 Pythonista 我们在所有的 Python 程序中都使用字典。即使不是直接在我们的代码中&#xff0c;也是间接的&#xff0c;因为dict类型是 Python 实现的基本部分。…

科技助力高质量发展:新质生产力的崛起与企业数字化转型

引言 随着科技的飞速发展&#xff0c;我们正逐渐步入数字化智能时代&#xff0c;这个时代不仅为企业带来了无限的机遇&#xff0c;也让其面对前所未有的挑战。在这个快速变革的时代&#xff0c;企业必须不断调整自己的经营策略&#xff0c;适应数字化转型的浪潮&#xff0c;以…

使用appuploder上架App Store流程

使用appuploder流程笔记 1.如何没有账号去apple官网注册一个&#xff0c;地址&#xff1a;https://developer.apple.com/account 2.下载解压appuploder&#xff0c;双击打开&#xff0c;用刚刚注册的账号登录&#xff0c;下载地址&#xff1a;http://www.applicationloader.n…

PHP连接达梦数据库

PDO是一种在PHP中连接数据库的接口&#xff0c;可以通过PDO接口使用PHP连接达梦数据库。 1、安装PHP环境 检查当前环境是否安装PHP [rootlocalhost ~]# php -v 当前环境并未安装PHP&#xff0c;需要进行安装&#xff0c;选择安装PHP7.3版本。 2、安装 epel-release源和源管…

人工智能时代的引领者:AI提示工程激发大语言模型的无限潜能

文章目录 一、AI提示工程的概念与定义二、AI提示工程的应用领域三、AI提示工程的技术创新与突破四、AI提示工程的未来发展趋势《AI提示工程实战&#xff1a;从零开始利用提示工程学习应用大语言模型》亮点内容简介作者简介目录 一、AI提示工程的概念与定义 在当今日新月异的科…

分类预测 | Matlab实现BiTCN双向时间卷积神经网络数据分类预测/故障识别

分类预测 | Matlab实现BiTCN双向时间卷积神经网络数据分类预测/故障识别 目录 分类预测 | Matlab实现BiTCN双向时间卷积神经网络数据分类预测/故障识别分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Matlab实现BiTCN双向时间卷积神经网络数据分类预测/故障识别。 2.自…

mysql四种事务隔离级别,2024金三银四

TransactionDefinition.PROPAGATION_MANDATORY&#xff1a;如果当前存在事务&#xff0c;则加入该事务&#xff1b;如果当前没有事务&#xff0c;则抛出异常。 TransactionDefinition.PROPAGATION_NESTED&#xff1a;如果当前存在事务&#xff0c;则创建一个事务作为当前事务的…