- 本博客是《MySQL基礎》系列博客的第九部分,主要介紹MySQL中的索引
- 本博客既爲方便自己查看複習而作,亦爲你而作,望能有所裨益
- 學習交流請聯繫 [email protected]
基本概念
定義
- 索引(Index),在MySQL中也叫鍵(Key),是存儲引擎快速找到記錄的一種數據結構。
- 你可以將它形象地比喻爲目錄之類的東西,能夠快速檢索出你所需要的東西。
類型
類型 | 關鍵字 | 簡介 |
---|---|---|
普通索引 | INDEX | 最基本常用的索引,僅加速查詢,沒有任何限制 |
唯一索引 | UNIQUE | 唯一索引列的值必須唯一,但允許有空值,如果是組合索引,則列值的組合必須唯一 |
全文索引 | FULLTEXT | 僅可以適用於MyISAM引擎的數據表:作用於CHAR、VARCHAR、TEXT數據類型的列 |
主鍵索引 | UNIQUE INDEX | 一種特殊的唯一索引,不允許有空值,一般是在創建表的時候同時創建主鍵索引 |
組合索引 | INDEX() | 將幾個列作爲一條索引進行檢索,使用最左匹配原則 |
索引應用
示例數據表
由於索引是用來加快查詢速度的,因此越大型的數據集其查詢效率的優勢越明顯,因此我們需要在原有的數據表的基礎上再創建一個新的大型數據表用來學習:
create table payment1(
payment_id smallint(5) unsigned not null,
country varchar(50) not null,
country_id smallint(5) unsigned not null
)
select payment_id,country,country_id from payment,country;
創建完成後,生成一個大型數據集:
預備知識
爲了能讓其效果簡潔易懂,也爲了方便日後章節的學習,我們在這裏就要開始學習如何在命令行使用MySQL了。
- 如何在命令行連接數據庫見《MySQL基礎(一):安裝配置》
- 顯示當前賬戶下的數據庫
show databases
- 使用某個數據庫
use <數據庫名>
- 顯示該數據庫中的數據表
show tables
- 在當前數據庫下執行相關操作
- 查看該表所有的索引
show index from <數據表>
創建索引
如前面所示,我們在查詢該數據表的時候花了1.49秒,這個時間對於數據庫來說已經不算短了,因此我們需要通過創建索引來提高查詢效率。
create index <索引名> on <數據表>(索引字段)
這時我們可以查詢一下所創建的索引:
如果感覺這樣看太彆扭,可以在命令後面加下 \G
:
這時我們再來看下查詢效率,是不是很驚訝!
普通索引
添加普通索引的語句除了上面提及的
create index <索引名> on <數據表>(索引字段);
還有
alter table <數據表> add index <索引名>(索引字段);
注:還可以在創建表的時候直接添加索引
create table test1(
id int not null,
username varchar(50) not null,
index idx_username(username) -- 添加名爲idx_username的索引,索引字段爲username
);
唯一索引
create table test2(
id int not null,
username varchar(50),
unique index idx_username(username) -- 增加唯一索引
);
爲了測試唯一索引的特性,我們試着往新建的表中插入記錄:
insert into test2(id,username) values(1,'xy'); -- 添加數據
insert into test2(id,username) values(1,'xy'); -- 數據唯一插不進去
insert into test2(id,username) values(1,null); -- 空值不視爲唯一值
主鍵索引
create table test3(
id int primary key not null,
username varchar(14),
unique index idx_username(username) -- 添加主鍵索引
);
爲了測試主鍵索引的特性,我們試着往新建的表中插入記錄:
insert test3(id,username) values(1,'xy'); -- 添加數據
insert test3(id,username) values(1,'xy'); -- 數據唯一插不進去
insert test3(id,username) values(null,'xy'); -- 空值插不進去
組合索引
爲了更好地展示組合索引,我們創建一個包含多個字段的表:
create table payment2(
payment_id smallint(5) unsigned not null,
language_id tinyint(5) unsigned not null,
store_id tinyint(5) unsigned not null,
index idx_payment_language_store(payment_id,language_id,store_id)
-- 創建組合索引
)
select payment_id,language_id,store_id from payment,language,store;
除了在創建表的時候直接添加索引,也可以後續自行添加
create index idx_payment_language_store
on payment2(payment_id,language_id,store_id);
-- 創建組合索引
爲了測試組合索引的特性,即最左匹配原則,我們可以這樣做:
explain select * from payment2 where payment_id=1 and
language_id=1 and store_id=2;
-- 所有字段均參與查詢
explain select * from payment2 where payment_id=1 and language_id=1;
-- 去掉store_id字段
explain select * from payment2 where payment_id=1 and store_id=2;
-- 去掉language_id字段
explain select * from payment2 where language_id=1 and store_id=2;
-- 去掉payment_id字段
/* 顯示查詢的具體信息 組合索引遵循最左匹配原則 */
觀察上圖,細細品味何謂最左匹配原則吧 😃
修改索引
就我目前的知識而言,如果想修改索引的話只能先刪除當前索引,再創建新的索引
(如果有哪位兄弟姐妹知道其他方法,請告訴小弟一聲,感激不盡)
刪除索引
drop index <索引名> on <數據庫>.<數據表>
alter table <數據庫>.<數據表> drop index <索引名>