mysql索引建立及应用(MYSQL创建索引,这些知识应该了解)
类别:数据库 浏览量:1675
时间:2021-11-03 12:57:12 mysql索引建立及应用
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+树,会占用额外的存储空间;其次当对表中的数据进行增加、删除、修改时,索引也需要动态的维护,降低了数据的维护速度。所以我们创建索引时还是需要根据业务来考虑的,一个表中建议不要加过多索引。
原文地址:https://mp.weixin.qq.com/s?__biz=MzI2NTA3OTY2Nw==&mid=2647636131&idx=1&sn=3e421df081e97c544c102e21b3a30ea9&chksm=f299c6bfc5ee4fa98b31e6620975332213ae239d3919ccddb1100b07831a08c4ac6a4919eb88&mpshare=1&
您可能感兴趣
- mysql索引的类型和方法(MySQL覆盖索引的使用示例)
- mysql发生系统错误(MySQL too many connections错误的原因及解决)
- mysql如何解析binlog(MySQL的binlog日志使用详解)
- mysql千万级别数据查询优化(mysql千万级数据量根据索引优化查询速度的实现)
- 宝塔mysql怎么设置优化(宝塔面板mysql内存占用高如何优化)
- thinkphp5.1手动连接mysql数据库(thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例)
- cent os7.0 安装mysql(mysql8.0.23 linuxcentos7安装完整超详细教程)
- mysql开启审计日志会导致性能下降(MySQL 一则慢日志监控误报的问题分析与解决)
- php7用什么替代mysql(mysqli扩展无法在PHP7下升级问题的解决)
- mysql索引知识点总结(MySQL 8.0 之索引跳跃扫描Index Skip Scan)
- mysql图形化管理工具
- mysql删除表数据的语句(Mysql实现定时清空一张表的旧数据并保留几条数据推荐)
- mysql读写分离代码实现(MySQL Router实现MySQL的读写分离的方法)
- dockermysql配置详解(Docker 部署Mysql 服务和Redis 服务的方法)
- mysql主从同步原理详解(MySQL双主主主架构配置方案)
- mysql如何删除整个表及数据(Mysql删除数据以及数据表的方法实例)
- 为什么现在社会越来越卷了(现在社会为什么发展那么快呢)
- 直播带货能赚到很多钱吗(直播带货能赚到很多钱吗现在)
- 做网红真的很能赚钱吗(做网红真的很能赚钱吗)
- 10句英语常用(英语常用900句)
- 爱情能当饭吃吗(爱情能当饭吃吗说说)
- 白T恤穿法(白t恤)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9