前言:
在 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+树,会占用额定的存储空间;其次当对表中的数据进行减少、删除、批改时,索引也须要动静的保护,升高了数据的保护速度。所以咱们创立索引时还是须要依据业务来思考的,一个表中倡议不要加过多索引。