建立索引,這些知識應該瞭解

前言:mysql

在 MySQL 中,基本上每一個表都會有索引,有時候也須要根據不一樣的業務場景添加不一樣的索引。索引的創建對於數據庫高效運行是很重要的,本篇文章將介紹下建立索引相關知識及注意事項。sql

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 權限。code

另外說明下,刪除索引也是可使用 alter table tb_name drop index xxx 和 drop index xxx on tb_name 兩種方式,分別須要 alter 和 index 權限。索引

索引的優勢顯而易見是能夠加速查詢,但建立索引也是有代價的。首先每創建一個索引都要爲它創建一棵B+樹,會佔用額外的存儲空間;其次當對錶中的數據進行增長、刪除、修改時,索引也須要動態的維護,下降了數據的維護速度。因此咱們建立索引時仍是須要根據業務來考慮的,一個表中建議不要加過多索引。rem

相關文章
相關標籤/搜索