创建索引,这些知识应该了解

数据库 MySQL
在 MySQL 中,基本上每个表都会有索引,有时候也需要根据不同的业务场景添加不同的索引。索引的建立对于数据库高效运行是很重要的,本篇文章将介绍下创建索引相关知识及注意事项。

[[391872]]

 1.创建索引方法

创建索引可以在建表时指定,也可以建表后使用 alter table 或 create index 语句创建索引。下面展示下几种常见的创建索引场景。

 

  1. # 建表时指定索引 
  2. CREATE TABLE `t_index` ( 
  3.   `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键'
  4.   `col1` int(11) NOT NULL
  5.   `col2` varchar(20) NOT NULL
  6.   `col3` varchar(50) NOT NULL
  7.   `col4` int(11) NOT NULL
  8.  `col5` varchar(50) NOT NULL
  9.   PRIMARY KEY (`increment_id`), 
  10.   UNIQUE KEY `uk_col1` (`col1`), 
  11.   KEY `idx_col2` (`col2`) 
  12. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='测试索引'
  13.  
  14. # 创建索引(两种方法) 
  15. # 普通索引 
  16. alter table `t_index` add index idx_col3 (col3);  
  17. create index idx_col3 on t_index(col3); 
  18. # 唯一索引 
  19. alter table `t_index` add unique index uk_col4 (col4); 
  20. create unique index uk_col4 on t_index(col4); 
  21. # 联合索引 
  22. alter table `t_index` add index idx_col3_col4 (col3,col4); 
  23. create index idx_col3_col4 on t_index(col3,col4); 
  24. # 前缀索引 
  25. alter table `t_index` add index idx_col5 (col5(20));  
  26. create index idx_col5 on t_index(col5(20)); 
  27.  
  28. # 查看表索引 
  29. mysql> show index from t_index; 
  30. +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  31. Table   | Non_unique | Key_name | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
  32. +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  33. | t_index |          0 | PRIMARY  |            1 | increment_id | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  34. | t_index |          0 | uk_col1  |            1 | col1         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  35. | t_index |          1 | idx_col2 |            1 | col2         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  36. | t_index |          1 | idx_col3 |            1 | col3         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               | 
  37. +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 

2.创建索引所需权限

如果你用的不是 root 账号,那创建索引就要考虑权限问题了,是不是需要 create、alter 权限就行了呢?下面我们来具体看下。

 

  1. # 测试用户的权限 
  2. mysql> show grants; 
  3. +-------------------------------------------------------------------------------------+ 
  4. | Grants for testuser@%                                                               | 
  5. +-------------------------------------------------------------------------------------+ 
  6. GRANT USAGE ON *.* TO 'testuser'@'%'                                                | 
  7. GRANT SELECTINSERTUPDATEDELETECREATEALTER ON `testdb`.* TO 'testuser'@'%' | 
  8. +-------------------------------------------------------------------------------------+ 
  9.  
  10. alter table 方式创建索引 
  11. mysql> alter table `t_index` add index idx_col2 (col2); 
  12. Query OK, 0 rows affected (0.05 sec) 
  13. Records: 0  Duplicates: 0  Warnings: 0 
  14.  
  15. create index 方式创建索引 
  16. mysql>  create index idx_col3 on t_index(col3); 
  17. ERROR 1142 (42000): INDEX command denied to user 'testuser'@'localhost' for table 't_index' 
  18.  
  19. create index 方式创建索引还需要index权限 赋予index权限后再执行 
  20. mysql> create index idx_col3 on t_index(col3); 
  21. Query OK, 0 rows affected (0.04 sec) 
  22. Records: 0  Duplicates: 0  Warnings: 0 

从上面测试可以看出,使用 alter table 方式创建索引需要 alter 权限,使用 create index 方式创建索引需要 index 权限。

另外说明下,删除索引也是可以使用 alter table `tb_name` drop index xxx 和 drop index xxx on tb_name 两种方式,分别需要 alter 和 index 权限。

索引的优点显而易见是可以加速查询,但创建索引也是有代价的。首先每建立一个索引都要为它建立一棵B+树,会占用额外的存储空间;其次当对表中的数据进行增加、删除、修改时,索引也需要动态的维护,降低了数据的维护速度。所以我们创建索引时还是需要根据业务来考虑的,一个表中建议不要加过多索引。

责任编辑:华轩 来源: MySQL技术
相关推荐

2021-04-08 20:50:17

创建索引MySQL

2022-10-26 07:21:15

网络视频开发

2020-12-09 18:16:48

容器云开发CaaS

2020-04-24 09:39:13

网络攻击恶意软件网络安全

2017-12-22 10:48:00

AI深度学习迁移学习

2019-07-18 05:00:31

ARPIP网络协议

2018-03-16 10:36:56

SSD固态硬盘闪存

2019-11-25 21:46:12

数据湖云计算数据仓库

2016-01-29 16:02:06

虚拟化

2021-06-15 06:50:08

索引字段数据

2015-07-15 16:53:55

IP游戏基础知识

2024-04-10 12:36:41

硬件代码

2021-06-11 09:33:33

索引SQL语句

2016-02-19 09:33:14

无线知识无线技术2016

2013-07-03 10:48:58

设计师iOS应用iOS人机交互

2019-05-21 16:19:46

前端性能优化图片

2021-10-25 14:55:38

Linux技巧命令

2013-03-20 17:58:41

虚拟内存程序员

2021-04-27 22:27:19

手机安卓苹果

2023-03-02 11:52:00

自定义自动配置
点赞
收藏

51CTO技术栈公众号