色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

MySQL創建索引需要了解的

瀏覽:125日期:2023-10-04 07:40:00
前言:

在 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創建索引需要了解的的詳細內容,更多關于MySQL創建索引的資料請關注好吧啦網其它相關文章!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 国产在线一二三区 | 日本在线亚州精品视频在线 | a级毛片网站 | 国产亚洲小视频 | 欧美一区二区日韩一区二区 | 久久一级黄色片 | 国产成人精品高清在线观看99 | 成人www视频网站免费观看 | 97国内免费久久久久久久久久 | 美国免费一级片 | 亚洲成人免费在线 | 久久黄色一级视频 | 亚洲视频男人的天堂 | 一区二区中文字幕在线观看 | 日韩精品一区二区三区免费观看 | 久草国产在线播放 | 国产免费一区二区三区在线 | a毛片久久免费观看 | 国产一区视频在线 | 国产在线观看一区二区三区 | 成人在线观看午夜 | 日韩高清一区二区 | 一本久 | 一级一黄在线观看视频免费 | 美女a毛片 | 欧美精品xxxⅹ欧美 欧美精品不卡 | 久久久久亚洲香蕉网 | 国产精品亚洲综合 | 亚洲国产福利精品一区二区 | 在线国产视频 | 中文字幕天堂最新版在线网 | 精品久久久久久亚洲 | 日韩欧美成末人一区二区三区 | 欧美一区二区三区视频在线观看 | 真实国产普通话对白乱子子伦视频 | 亚洲偷自拍另类图片二区 | 亚洲国产精品a在线 | 久久在线视频播放 | 在线视频一区二区 | 国产主播福利精品一区二区 | 中国性孕妇孕交在线 |