1. 程式人生 > >Mysql學習---索引的學習 180101

Mysql學習---索引的學習 180101

ble 目錄 默認 index lte 用戶 image 適用於 order

索引:約束 + 快速查找

索引是數據庫中用於幫助用戶快速查詢數據的一種數據結構。類似於字典中的目錄,查找字典內容時可以根據目錄查找到數據的存放位置,然後直接獲取即可。

問:為什麽索引可以這麽快?[類似書的目錄]

答:Mysql默認情況下,掃描一行一行的掃描所有表中的數據,直到遇到我們需要的數據,才結束查詢。有了索引之後,Mysql會對我們創建了索引的列單獨創建一個文件,同時將表中列中的數據進行一個轉換[將name列中的hhh轉換為數字],然後按照B+樹的順序排序,也根據B+樹查找

技術分享圖片

MySQL中常見的索引:索引都是唯一的

普通索引 - 加速查找,重復數據重復查找

唯一索引 - 加速查找,約束列數據不能重復,數據可以為null

主鍵索引 - 加速查找,約束列數據不能重復,數據不能為null

組合索引 - 多列可以創建一個索引文件

註意:索引適用於不常修改的列,僅用於查詢操作

普通索引: 加速查找

創建普通索引:

方案一:創建表的時候創建索引

方案二:手動創建

方案一:創建表的時候創建索引

create table tb1(
    nid int not null auto_increment primary key,
    name  varchar(32) not null,
    email varchar(64) not null,
    extra text,
    index ix_name (name)   # 為neme列創建普通索引,普通索引數據可以重復
)engine=innodb default charset=‘utf8‘         

方案二:手動創建

create index ix_email on tb1(email) # 為email列創建索引

刪除普通索引:
drop index ix_email;

查看普通索引:
show index from tb1;

技術分享圖片

唯一索引:加速查詢 和 唯一約束(可含null)

創建唯一索引:

方案一:創建表的時候創建索引

方案二:手動創建

方案一:創建表的時候創建索引

create table tb2(
    nid int not null auto_increment primary key,
    name  varchar(32) not null,
    email varchar(64) not null,
    extra text,
    unique ix_name (name)   # 為neme列創建唯一索引,唯一索引數據不可以重復
)engine=innodb default charset=‘utf8‘    

方案二:手動創建

方案二:手動創建
create unique index ix_email on tb2(email) # 為email列創建唯一索引

刪除唯一索引:
drop unique index ix_email;

查看唯一索引:
show index from tb2;

主鍵索引:加快查找、不能重復 和 不能為空

創建主鍵索引:

方案一:創建表的時候創建索引

方案二:創建表的時候創建索引 --> primary key(nid)

方案三:手動創建,一個表只能有一個主鍵

方案一:創建表的時候創建索引

create table tb3(
     nid int not null auto_increment primary key,   # 為nid列創建主鍵索引,主鍵索引數據不可以重復, 不可以為null
    name  varchar(32) not null,
    email varchar(64) not null,
    extra text,
)engine=innodb default charset=‘utf8‘ 

方案二:創建表的時候創建索引 --> primary key(nid)

create table tb3(
    nid int not null auto_increment,
    name  varchar(32) not null,
    email varchar(64) not null,
    extra text,
    primary key (nid)   # 為nid列創建主鍵索引,主鍵索引數據不可以重復, 不可以為null
)engine=innodb default charset=‘utf8‘ 

方案三:手動創建,一個表只能有一個主鍵

alter table tb3 add primary key(email) # 重復會報錯, Multiple primary key defined

刪除主鍵索引:
alter table tb3 drop primary key;

查看主鍵索引:
show index from tb3;

組合索引:多列創建一個索引

- 普通組合索引: 無約束,且遵循最左匹配

- 聯合唯一索引: 有約束,兩列數據同時不相同,才能插入,不然報錯

技術分享圖片

創建組合索引:

方案一:創建表的時候創建索引

方案二:手動創建

方案一:創建表的時候創建索引

create table tb4(
     nid int not null auto_increment primary key,# 為nid列創建主鍵索引,主鍵索引數據不可以重復, 不可以為null
    name  varchar(32) not null,
    email varchar(64) not null,
    extra text,
    index(name, email)       # 創建普通組合索引
  # unique(name, email)      # 創建唯一組合索引
)engine=innodb default charset=‘utf8‘  

方案二:手動創建

方案二:手動創建
create index ix_name_email on in4(name,email);  # 重復會報錯, Multiple primary key defined

刪除主鍵索引:
drop index ix_name_email;

查看主鍵索引:
show index from tb4;

按照索引查找方式分

覆蓋索引 + 合並索引

技術分享圖片

如何有效的使用索引:

數據庫表中添加索引後確實會讓查詢速度起飛,但前提必須是正確的使用索引來查詢,如果以錯誤的方式使用,則即使建立索引也會不奏效。

-- 使用做匹配的like
	select * from student where sname like ‘%hhh‘  # 不推薦,因為左查找,先匹配%後匹配hhh,匹配範圍是All
	select * from student where sname like ‘hhh%‘   # 推薦,因為左查找,先匹配cnn,匹配範圍是range
-- SQL語句中盡量不要使用函數
	
-- 類型不一致,導致索引查詢失敗
   select * from tb1 where name = 999;
-- 特別的,Mysql中使用小於,等於的時候默認是使用索引的,但是大於的時候就不走索引了

【更多參見】http://www.cnblogs.com/wupeiqi/articles/5716963.html

Mysql學習---索引的學習 180101