索引是一種特殊的文件,包含了對數據表中所有記錄的引用指針。InnoDB引擎的數據庫,其上的索引是表空間的一個組成部分。
(1).索引的優缺點
優點:加快搜索速度,減少查詢時間
缺點:索引是以文件的形式存儲,如果索引過多,會占用磁盤較大的空間。而且影響insert、update、delete的執行時間。
索引中的數據必須與數據表中的人數據同步,如果索引過多,當表中數據更新,索引也要同步更新,這就降低了效率。
(2).普通索引
最基本的索引,不具備唯一性,僅加快查詢速度。
1)創建普通索引
create table [表名] ([字段名] [字段類型] [字段約束],...,[ index | key ] [索引名稱]([字段名稱]));
mysql> create table com_index1(id int,name varchar(20),key (name)); Query OK, 0 rows affected (0.02 sec) mysql> desc com_index1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> create table com_index2(id int,name varchar(20),index com_name(name)); Query OK, 0 rows affected (0.11 sec) mysql> desc com_index2; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.03 sec) mysql> create table com_index(id int,name varchar(20),index (name)); Query OK, 0 rows affected (0.02 sec) mysql> desc com_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
注意1:索引文件是一表一個,所以在沒有外鍵約束的情況下,只要表內沒有重名索引即可。
注意2:在Key列值為MUL的一般是普通索引,可以使用show index from [表名]來確認索引類型,里面有索引類型列如果為BTREE那么就是普通索引。
注意3:如果沒有指定索引名,那么默認索引名與字段名一致。
2)添加普通索引
alter table [表名] add [ index | key ] [索引名稱]([字段名]);
mysql> alter table com_index1 add key (id); Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | | name | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> alter table com_index2 add index com_id(id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index2; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | | name | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> alter table com_index add index (id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | | name | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
3)刪除普通索引
alter table [表名] drop [ key | index ] [索引名];
mysql> alter table com_index1 drop key name; Query OK, 0 rows affected (0.20 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> alter table com_index1 drop index id; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
(3).唯一索引
設置唯一索引的列中所有值都只能出現一次,允許NULL。唯一索引一般只在創建數據表以及數據表沒有數據之前創建或添加,如果已經存在數據,必須保證添加的列所有值具有唯一性。
1)創建唯一索引
create table [表名] ([字段名] [字段類型] [字段約束],...,unique [索引名稱]([字段名稱]));
mysql> create table uni_index(id int,name varchar(20),unique (id)); Query OK, 0 rows affected (0.12 sec) mysql> desc uni_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | UNI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
注意:在Key列值為UNI的即是唯一索引。
2)添加唯一索引(盡量不要用,用也盡量在存放數據之前)
alter table [表名] add unique [索引名]([字段名]);
mysql> alter table uni_index add unique (name); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc uni_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | UNI | NULL | | | name | varchar(20) | YES | UNI | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
3)刪除唯一索引
alter table [表名] drop key [索引名];
mysql> alter table uni_index drop key name; Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc uni_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | UNI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
(4).主鍵索引
查詢數據庫按主鍵查詢是最快的,每個數據表只允許存在一個主鍵,主鍵必須唯一,不允許為NULL。主鍵和唯一索引一樣,一般只在創建數據表以及數據表沒有數據之前創建或添加,如果已經存在數據,必須保證添加的列所有值具有唯一性。
1)創建主鍵
create table [表名] ([字段名] [字段類型] [字段約束],...,primary key [索引名稱]([字段名稱]));
create table [表名] ([字段名] [字段類型] [字段約束] [ primary key | key ],[字段名] [字段類型] [字段約束]...);
mysql> create table pri_index1 (id int,primary key (id)); Query OK, 0 rows affected (0.01 sec) mysql> desc pri_index1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec) mysql> create table pri_index2 (id int key); Query OK, 0 rows affected (0.01 sec) mysql> desc pri_index2; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> create table pri_index3 (id int primary key); Query OK, 0 rows affected (0.02 sec) mysql> desc pri_index3; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)
注意:在Key列值為PRI的即是主鍵索引。
2)添加主鍵(盡量不要用,用也盡量在存放數據之前)
alter table [表名] add primary key [索引名]([字段名]);
mysql> create table pri_index4 (id int); Query OK, 0 rows affected (0.02 sec) mysql> desc pri_index4; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> alter table pri_index4 add primary key (id); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc pri_index4; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)
3)刪除主鍵
alter table [表名] drop primary key;
mysql> alter table pri_index4 drop primary key; Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc pri_index4; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec)
(5).復合索引
一個索引可以包含一個或一個以上的列,當索引包含兩個及以上的列時,此時的索引被稱之為復合索引。基本創建、添加、刪除方式沒有改變,只是字段名部分可以為一個以上,之間由逗號隔開。
創建
mysql> create table comp_index(id int,name varchar(20), primary key (id,name)); Query OK, 0 rows affected (0.01 sec) mysql> desc comp_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | PRI | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
添加
mysql> create table comp_index1(id int,name varchar(20)); Query OK, 0 rows affected (0.25 sec) mysql> alter table comp_index1 add primary key (id,name); Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc comp_index1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | PRI | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
刪除
mysql> alter table comp_index drop primary key; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc comp_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(20) | NO | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
(6).全文索引
全文索引也稱全文檢索,是目前搜索引擎使用的一種關鍵技術。它能夠利用多種算法智能分析出文本文字中關鍵詞的頻率和重要性,然后按照一定的算法規則智能篩選出想要的結果。3.2開始支持全文索引,但無法正確支持中文;5.7.6開始內置ngram全文檢索插件,用來支持中文。
舊版的全文索引只能用在MyISAM數據庫引擎的表上,但5.6.24上InnoDB也加入了全文索引。不夠只支持char、varchar和text的字段類型。
1)創建全文索引
create table [表名] ([字段名] [字段類型] [字段約束],...,fulltext key [索引名稱]([字段名稱]));
mysql> create table ful_index(id int,name varchar(20),age varchar(20),fulltext key (name)); Query OK, 0 rows affected (0.25 sec) mysql> desc ful_index; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | MUL | NULL | | | age | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> show index from ful_index; +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec)
注意:這里Key的值雖然是MUL,但使用show index from [表名]可以看到實際索引類型是FULLTEXT。
2)添加全文索引
alter table [表名] add fulltext key [索引名]([字段名]);
mysql> alter table ful_index add fulltext key (age); Query OK, 0 rows affected (0.26 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from ful_index; +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | | ful_index | 1 | age | 1 | age | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.01 sec)
3)刪除全文索引
alter table [表名] drop key [索引名];
mysql> alter table ful_index drop key age; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from ful_index; +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec)
(7).索引創建原則
索引並不是越多越好:1.數據量不大時不需要創建索引;
2.列中值變化種類不多不需要創建索引;
3.經常排序(order by)和分組(group by)的列需要建立索引;
4.列中值需要具有唯一性,可以使用唯一索引或主鍵索引,但每個表中只能存在一個主鍵索引,並且不能為NULL。