本篇內(nèi)容主要講解“創(chuàng)建索引的方法步驟有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“創(chuàng)建索引的方法步驟有哪些”吧!
成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站建設、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務石柱土家族,10多年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108
1.創(chuàng)建索引方法
創(chuàng)建索引可以在建表時指定,也可以建表后使用 alter table 或 create index 語句創(chuàng)建索引。下面展示下幾種常見的創(chuàng)建索引場景。
# 建表時指定索引 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='測試索引'; # 創(chuàng)建索引(兩種方法) # 普通索引 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); # 聯(lián)合索引 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 | | | +---------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
如果你用的不是 root 賬號,那創(chuàng)建索引就要考慮權限問題了,是不是需要 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 方式創(chuàng)建索引 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 方式創(chuàng)建索引 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 方式創(chuàng)建索引還需要index權限 賦予index權限后再執(zhí)行 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 方式創(chuàng)建索引需要 alter 權限,使用 create index 方式創(chuàng)建索引需要 index 權限。
另外說明下,刪除索引也是可以使用 alter table `tb_name` drop index xxx 和 drop index xxx on tb_name 兩種方式,分別需要 alter 和 index 權限。
索引的優(yōu)點顯而易見是可以加速查詢,但創(chuàng)建索引也是有代價的。首先每建立一個索引都要為它建立一棵B+樹,會占用額外的存儲空間;其次當對表中的數(shù)據(jù)進行增加、刪除、修改時,索引也需要動態(tài)的維護,降低了數(shù)據(jù)的維護速度。所以我們創(chuàng)建索引時還是需要根據(jù)業(yè)務來考慮的,一個表中建議不要加過多索引。
到此,相信大家對“創(chuàng)建索引的方法步驟有哪些”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
本文題目:創(chuàng)建索引的方法步驟有哪些
轉載來源:http://jinyejixie.com/article30/pgseso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、移動網(wǎng)站建設、全網(wǎng)營銷推廣、營銷型網(wǎng)站建設、品牌網(wǎng)站制作、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)