MySQL创建索引需要了解的


Posted in MySQL onApril 08, 2021

前言: 

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

1.创建索引方法

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

# 建表时指定索引
CREATE TABLE `t_index` (
  `increment_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `col1` int(11) NOT NULL,
  `col2` varchar(20) NOT NULL,
  `col3` varchar(50) NOT NULL,
  `col4` int(11) NOT NULL,
 `col5` varchar(50) NOT NULL,
  PRIMARY KEY (`increment_id`),
  UNIQUE KEY `uk_col1` (`col1`),
  KEY `idx_col2` (`col2`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='测试索引';

# 创建索引(两种方法)
# 普通索引
alter table `t_index` add index idx_col3 (col3); 
create index idx_col3 on t_index(col3);
# 唯一索引
alter table `t_index` add unique index uk_col4 (col4);
create unique index uk_col4 on t_index(col4);
# 联合索引
alter table `t_index` add index idx_col3_col4 (col3,col4);
create index idx_col3_col4 on t_index(col3,col4);
# 前缀索引
alter table `t_index` add index idx_col5 (col5(20)); 
create index idx_col5 on t_index(col5(20));

# 查看表索引
mysql> show index from t_index;
+---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t_index |          0 | PRIMARY  |            1 | increment_id | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| t_index |          0 | uk_col1  |            1 | col1         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| t_index |          1 | idx_col2 |            1 | col2         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| t_index |          1 | idx_col3 |            1 | col3         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2.创建索引所需权限

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

# 测试用户的权限
mysql> show grants;
+-------------------------------------------------------------------------------------+
| Grants for testuser@%                                                               |
+-------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'testuser'@'%'                                                |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER ON `testdb`.* TO 'testuser'@'%' |
+-------------------------------------------------------------------------------------+

# alter table 方式创建索引
mysql> alter table `t_index` add index idx_col2 (col2);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

# create index 方式创建索引
mysql>  create index idx_col3 on t_index(col3);
ERROR 1142 (42000): INDEX command denied to user 'testuser'@'localhost' for table 't_index'

# create index 方式创建索引还需要index权限 赋予index权限后再执行
mysql> create index idx_col3 on t_index(col3);
Query OK, 0 rows affected (0.04 sec)
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创建索引需要了解的的详细内容,更多关于MySQL创建索引的资料请关注三水点靠木其它相关文章!

MySQL 相关文章推荐
多表查询、事务、DCL
Apr 05 MySQL
MySQL Innodb关键特性之插入缓冲(insert buffer)
Apr 08 MySQL
MySQL Router的安装部署
Apr 24 MySQL
浅谈mysql执行过程以及顺序
May 12 MySQL
MySQL约束超详解
Sep 04 MySQL
mysql5.7的安装及Navicate长久免费使用的实现过程
Nov 17 MySQL
分享MySQL常用 内核 Debug 几种常见方法
Mar 17 MySQL
为什么MySQL不建议使用SELECT *
Apr 03 MySQL
MySql分区类型及创建分区的方法
Apr 13 MySQL
MySQL数据库 安全管理
May 06 MySQL
MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
Jun 14 MySQL
MySQL深分页问题解决思路
Dec 24 MySQL
MySQL 使用SQL语句修改表名的实现
详解Mysql 函数调用优化
Apr 07 #MySQL
MySQL复制问题的三个参数分析
Apr 07 #MySQL
MySQL pt-slave-restart工具的使用简介
Apr 07 #MySQL
MySQL主从复制断开的常用修复方法
Apr 07 #MySQL
MySQL infobright的安装步骤
Apr 07 #MySQL
MySQL表的增删改查基础教程
You might like
博士208HAF收音机实习报告
2021/03/02 无线电
pdo中使用参数化查询sql
2011/08/11 PHP
对PHP新手的一些建议(PHP学习经验总结)
2014/08/20 PHP
YII2 实现多语言配置的方法分享
2017/01/11 PHP
来自chinaz的ajax获取评论代码
2008/05/03 Javascript
javascript prototype,executing,context,closure
2008/12/24 Javascript
JS在IE下缺少标识符的错误
2014/07/23 Javascript
JavaScript中document对象使用详解
2015/01/06 Javascript
javascript常用正则表达式汇总
2015/07/31 Javascript
jQuery实现的超酷苹果风格图标滑出菜单效果代码
2015/09/16 Javascript
在JavaScript中call()与apply()区别
2016/01/22 Javascript
Javascript的表单验证-提交表单
2016/03/18 Javascript
微信小程序 页面跳转和数据传递实例详解
2017/01/19 Javascript
纯JavaScript实现实时反馈系统时间
2017/10/26 Javascript
理理Vue细节(推荐)
2019/04/16 Javascript
在vue中axios设置timeout超时的操作
2020/09/04 Javascript
不管你的Python报什么错,用这个模块就能正常运行
2018/09/14 Python
Python编写合并字典并实现敏感目录的小脚本
2019/02/26 Python
Python安装Flask环境及简单应用示例
2019/05/03 Python
PyQt5笔记之弹出窗口大全
2019/06/20 Python
Python程序打包工具py2exe和PyInstaller详解
2019/06/28 Python
Python 依赖库太多了该如何管理
2019/11/08 Python
Python类中的装饰器在当前类中的声明与调用详解
2020/04/15 Python
Laura官网:加拿大女性的顶级时尚目的地
2019/09/20 全球购物
计算机应用职专应届生求职信
2013/11/12 职场文书
大学四年职业生涯规划书范文
2014/01/02 职场文书
售后服务承诺书范文
2014/03/26 职场文书
服务型党组织建设典型材料
2014/05/07 职场文书
经营目标管理责任书
2014/07/25 职场文书
民族学专业大学生职业规划范文:清晰未来的构想
2014/09/20 职场文书
兵马俑的导游词
2015/02/02 职场文书
任命书怎么写
2015/03/02 职场文书
试用期工作表现自我评价
2015/03/06 职场文书
2015年环境监察工作总结
2015/07/23 职场文书
MySQL 慢查询日志深入理解
2021/04/22 MySQL
Python 详解通过Scrapy框架实现爬取CSDN全站热榜标题热词流程
2021/11/11 Python