当前位置:Gxlcms > 数据库问题 > Mysql常见索引介绍

Mysql常见索引介绍

时间:2021-07-01 10:21:17 帮助过:3人阅读

  注意2:在Key列值为MUL的一般是普通索引,可以使用show index from [表名]来确认索引类型,里面有索引类型列如果为BTREE那么就是普通索引

  注意3:如果没有指定索引名,那么默认索引名与字段名一致。

2)添加普通索引

  alter table [表名] add [ index | key ] [索引名称]([字段名]);

  1. mysql> alter table com_index1 add key (id);
  2. Query OK, 0 rows affected (0.11 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc com_index1;
  5. +-------+-------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+-------------+------+-----+---------+-------+
  8. | id | int(11) | YES | MUL | NULL | |
  9. | name | varchar(20) | YES | MUL | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 2 rows in set (0.00 sec)
  12. mysql> alter table com_index2 add index com_id(id);
  13. Query OK, 0 rows affected (0.01 sec)
  14. Records: 0 Duplicates: 0 Warnings: 0
  15. mysql> desc com_index2;
  16. +-------+-------------+------+-----+---------+-------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +-------+-------------+------+-----+---------+-------+
  19. | id | int(11) | YES | MUL | NULL | |
  20. | name | varchar(20) | YES | MUL | NULL | |
  21. +-------+-------------+------+-----+---------+-------+
  22. 2 rows in set (0.00 sec)
  23. mysql> alter table com_index add index (id);
  24. Query OK, 0 rows affected (0.01 sec)
  25. Records: 0 Duplicates: 0 Warnings: 0
  26. mysql> desc com_index;
  27. +-------+-------------+------+-----+---------+-------+
  28. | Field | Type | Null | Key | Default | Extra |
  29. +-------+-------------+------+-----+---------+-------+
  30. | id | int(11) | YES | MUL | NULL | |
  31. | name | varchar(20) | YES | MUL | NULL | |
  32. +-------+-------------+------+-----+---------+-------+
  33. 2 rows in set (0.00 sec)

3)删除普通索引

  alter table [表名] drop [ key | index ] [索引名];

  1. mysql> alter table com_index1 drop key name;
  2. Query OK, 0 rows affected (0.20 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc com_index1;
  5. +-------+-------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+-------------+------+-----+---------+-------+
  8. | id | int(11) | YES | MUL | NULL | |
  9. | name | varchar(20) | YES | | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 2 rows in set (0.00 sec)
  12. mysql> alter table com_index1 drop index id;
  13. Query OK, 0 rows affected (0.03 sec)
  14. Records: 0 Duplicates: 0 Warnings: 0
  15. mysql> desc com_index1;
  16. +-------+-------------+------+-----+---------+-------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +-------+-------------+------+-----+---------+-------+
  19. | id | int(11) | YES | | NULL | |
  20. | name | varchar(20) | YES | | NULL | |
  21. +-------+-------------+------+-----+---------+-------+
  22. 2 rows in set (0.01 sec)

(3).唯一索引

  设置唯一索引的列中所有值都只能出现一次,允许NULL。唯一索引一般只在创建数据表以及数据表没有数据之前创建或添加,如果已经存在数据,必须保证添加的列所有值具有唯一性。

1)创建唯一索引

  create table [表名] ([字段名] [字段类型] [字段约束],...,unique [索引名称]([字段名称]));

  1. mysql> create table uni_index(id int,name varchar(20),unique (id));
  2. Query OK, 0 rows affected (0.12 sec)
  3. mysql> desc uni_index;
  4. +-------+-------------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+-------------+------+-----+---------+-------+
  7. | id | int(11) | YES | UNI | NULL | |
  8. | name | varchar(20) | YES | | NULL | |
  9. +-------+-------------+------+-----+---------+-------+
  10. 2 rows in set (0.00 sec)

  注意:在Key列值为UNI的即是唯一索引。

2)添加唯一索引(尽量不要用,用也尽量在存放数据之前)

  alter table [表名] add unique [索引名]([字段名]);

  1. mysql> alter table uni_index add unique (name);
  2. Query OK, 0 rows affected (0.03 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc uni_index;
  5. +-------+-------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+-------------+------+-----+---------+-------+
  8. | id | int(11) | YES | UNI | NULL | |
  9. | name | varchar(20) | YES | UNI | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 2 rows in set (0.00 sec)

3)删除唯一索引

  alter table [表名] drop key [索引名];

  1. mysql> alter table uni_index drop key name;
  2. Query OK, 0 rows affected (0.13 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc uni_index;
  5. +-------+-------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+-------------+------+-----+---------+-------+
  8. | id | int(11) | YES | UNI | NULL | |
  9. | name | varchar(20) | YES | | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 2 rows in set (0.00 sec)

(4).主键索引

  查询数据库按主键查询是最快的,每个数据表只允许存在一个主键,主键必须唯一,不允许为NULL主键和唯一索引一样,一般只在创建数据表以及数据表没有数据之前创建或添加,如果已经存在数据,必须保证添加的列所有值具有唯一性。

1)创建主键

  create table [表名] ([字段名] [字段类型] [字段约束],...,primary key [索引名称]([字段名称]));

  create table [表名] ([字段名] [字段类型] [字段约束] [ primary key | key ],[字段名] [字段类型] [字段约束]...);

  1. mysql> create table pri_index1 (id int,primary key (id));
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> desc pri_index1;
  4. +-------+---------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+---------+------+-----+---------+-------+
  7. | id | int(11) | NO | PRI | NULL | |
  8. +-------+---------+------+-----+---------+-------+
  9. 1 row in set (0.01 sec)
  10. mysql> create table pri_index2 (id int key);
  11. Query OK, 0 rows affected (0.01 sec)
  12. mysql> desc pri_index2;
  13. +-------+---------+------+-----+---------+-------+
  14. | Field | Type | Null | Key | Default | Extra |
  15. +-------+---------+------+-----+---------+-------+
  16. | id | int(11) | NO | PRI | NULL | |
  17. +-------+---------+------+-----+---------+-------+
  18. 1 row in set (0.00 sec)
  19. mysql> create table pri_index3 (id int primary key);
  20. Query OK, 0 rows affected (0.02 sec)
  21. mysql> desc pri_index3;
  22. +-------+---------+------+-----+---------+-------+
  23. | Field | Type | Null | Key | Default | Extra |
  24. +-------+---------+------+-----+---------+-------+
  25. | id | int(11) | NO | PRI | NULL | |
  26. +-------+---------+------+-----+---------+-------+
  27. 1 row in set (0.00 sec)

  注意:在Key列值为PRI的即是主键索引。

2)添加主键(尽量不要用,用也尽量在存放数据之前)

  alter table [表名] add primary key [索引名]([字段名]);

  1. mysql> create table pri_index4 (id int);
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> desc pri_index4;
  4. +-------+---------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+---------+------+-----+---------+-------+
  7. | id | int(11) | YES | | NULL | |
  8. +-------+---------+------+-----+---------+-------+
  9. 1 row in set (0.00 sec)
  10. mysql> alter table pri_index4 add primary key (id);
  11. Query OK, 0 rows affected (0.04 sec)
  12. Records: 0 Duplicates: 0 Warnings: 0
  13. mysql> desc pri_index4;
  14. +-------+---------+------+-----+---------+-------+
  15. | Field | Type | Null | Key | Default | Extra |
  16. +-------+---------+------+-----+---------+-------+
  17. | id | int(11) | NO | PRI | NULL | |
  18. +-------+---------+------+-----+---------+-------+
  19. 1 row in set (0.00 sec)

3)删除主键

  alter table [表名] drop primary key;

  1. mysql> alter table pri_index4 drop primary key;
  2. Query OK, 0 rows affected (0.13 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc pri_index4;
  5. +-------+---------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+---------+------+-----+---------+-------+
  8. | id | int(11) | NO | | NULL | |
  9. +-------+---------+------+-----+---------+-------+
  10. 1 row in set (0.01 sec)

(5).复合索引

  一个索引可以包含一个或一个以上的列,当索引包含两个及以上的列时,此时的索引被称之为复合索引。基本创建、添加、删除方式没有改变,只是字段名部分可以为一个以上,之间由逗号隔开。

  创建

  1. mysql> create table comp_index(id int,name varchar(20), primary key (id,name));
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> desc comp_index;
  4. +-------+-------------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+-------------+------+-----+---------+-------+
  7. | id | int(11) | NO | PRI | NULL | |
  8. | name | varchar(20) | NO | PRI | NULL | |
  9. +-------+-------------+------+-----+---------+-------+
  10. 2 rows in set (0.00 sec)

  添加

  1. mysql> create table comp_index1(id int,name varchar(20));
  2. Query OK, 0 rows affected (0.25 sec)
  3. mysql> alter table comp_index1 add primary key (id,name);
  4. Query OK, 0 rows affected (0.17 sec)
  5. Records: 0 Duplicates: 0 Warnings: 0
  6. mysql> desc comp_index1;
  7. +-------+-------------+------+-----+---------+-------+
  8. | Field | Type | Null | Key | Default | Extra |
  9. +-------+-------------+------+-----+---------+-------+
  10. | id | int(11) | NO | PRI | NULL | |
  11. | name | varchar(20) | NO | PRI | NULL | |
  12. +-------+-------------+------+-----+---------+-------+
  13. 2 rows in set (0.00 sec)

  删除

  1. mysql> alter table comp_index drop primary key;
  2. Query OK, 0 rows affected (0.04 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> desc comp_index;
  5. +-------+-------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +-------+-------------+------+-----+---------+-------+
  8. | id | int(11) | NO | | NULL | |
  9. | name | varchar(20) | NO | | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 2 rows in set (0.00 sec)

(6).全文索引

  全文索引也称全文检索,是目前搜索引擎使用的一种关键技术。它能够利用多种算法智能分析出文本文字中关键词的频率和重要性,然后按照一定的算法规则智能筛选出想要的结果。3.2开始支持全文索引,但无法正确支持中文;5.7.6开始内置ngram全文检索插件,用来支持中文。

  旧版的全文索引只能用在MyISAM数据库引擎的表上,但5.6.24上InnoDB也加入了全文索引。不够只支持char、varchar和text的字段类型。

1)创建全文索引

  create table [表名] ([字段名] [字段类型] [字段约束],...,fulltext key [索引名称]([字段名称]));

  1. mysql> create table ful_index(id int,name varchar(20),age varchar(20),fulltext key (name));
  2. Query OK, 0 rows affected (0.25 sec)
  3. mysql> desc ful_index;
  4. +-------+-------------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+-------------+------+-----+---------+-------+
  7. | id | int(11) | YES | | NULL | |
  8. | name | varchar(20) | YES | MUL | NULL | |
  9. | age | varchar(20) | YES | | NULL | |
  10. +-------+-------------+------+-----+---------+-------+
  11. 3 rows in set (0.00 sec)
  12. mysql> show index from ful_index;
  13. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  14. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  15. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  16. | ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
  17. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  18. 1 row in set (0.00 sec)

  注意:这里Key的值虽然是MUL,但使用show index from [表名]可以看到实际索引类型是FULLTEXT。

2)添加全文索引

  alter table [表名] add fulltext key [索引名]([字段名]);

  1. mysql> alter table ful_index add fulltext key (age);
  2. Query OK, 0 rows affected (0.26 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> show index from ful_index;
  5. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  6. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  7. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  8. | ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
  9. | ful_index | 1 | age | 1 | age | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
  10. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  11. 2 rows in set (0.01 sec)

3)删除全文索引

  alter table [表名] drop key [索引名];

  1. mysql> alter table ful_index drop key age;
  2. Query OK, 0 rows affected (0.05 sec)
  3. Records: 0 Duplicates: 0 Warnings: 0
  4. mysql> show index from ful_index;
  5. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  6. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  7. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  8. | ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
  9. +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  10. 1 row in set (0.00 sec)

(7).索引创建原则

  索引并不是越多越好:1.数据量不大时不需要创建索引;

            2.列中值变化种类不多不需要创建索引;

            3.经常排序(order by)和分组(group by)的列需要建立索引;

            4.列中值需要具有唯一性,可以使用唯一索引或主键索引,但每个表中只能存在一个主键索引,并且不能为NULL。

Mysql常见索引介绍

标签:prim   inno   group   record   csharp   分组   情况   允许   cte   

人气教程排行