创建索引,这些知识应该了解
前言:
在 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技术 出处:https://www.cnblogs.com/kunjian/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。有需要沟通的,可以站内私信,文章留言,或者关注『MySQL技术』公众号私信我。一定尽力回答。 |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· 一文搞懂MCP协议与Function Call的区别
· 如何不购买域名在云服务器上搭建HTTPS服务