温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

创建索引的方法步骤有哪些

发布时间:2021-10-09 17:27:42 来源:亿速云 阅读:187 作者:iii 栏目:数据库

本篇内容主要讲解“创建索引的方法步骤有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“创建索引的方法步骤有哪些”吧!

创建索引的方法步骤有哪些

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+树,会占用额外的存储空间;其次当对表中的数据进行增加、删除、修改时,索引也需要动态的维护,降低了数据的维护速度。所以我们创建索引时还是需要根据业务来考虑的,一个表中建议不要加过多索引。

到此,相信大家对“创建索引的方法步骤有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI