mysql索引型別和方式 mysql索引型別和方式

2021-10-17 21:11:13 字數 3152 閱讀 7721

索引

資料庫的索引就像一本書的目錄,能夠加快資料庫的查詢速度。

mysql索引有四種primary、index、unique、fulltext, 其中primary、index、unique是一類,fulltext是一類。

這四種都是單列索引,也就是他們都是作用於單個一列,所以也稱單列索引;但是所以乙個索引也可以作用於多個列上,稱為組合索引或復合索引。

單列索引

新建一張測試表

create table t_user( id int not null,username varchar(16) not null);

(1)primary:主鍵索引。索引列唯一且不能為空;一張表只能有乙個主鍵索引(主鍵索引通常在建表的時候就指定)

create table t_user(id int not null,username varchar(16) not null,primary key(id))

(2)normal:普通索引。索引列沒有任何限制;

建表時指定

create table t_user(id int not null,username varchar(16) not null,index username_index(username(16))) //給列username建普通索引username_index

alter語句指定

alter table t_user add index u_index (username) //給列username建普通索引 u_index

刪除索引

drop index u_index on t_user //刪除表t_user中的索引u_index

(3)unique:唯一索引。索引列的值必須是唯一的,但允許有空;

建表時指定

create table t_user(id int not null,username varchar(16) not null,unique u_index(username)) //給列username新增唯一索引t_user

alter語句指定

alter table t_user add unique u_index(username) //給列t_user新增唯一索引u_index

刪除索引

drop index u_index on t_user

(4)fulltext:全文搜尋的索引。fulltext 用於搜尋很長一篇文章的時候,效果最好。用在比較短的文字,如果就一兩行字的,普通的 index 也可以。索引的新建和刪除和上面一致,這裡不再列舉...

組合索引(復合索引)

新建一張表

create table t_user(id int not null,username varchar(16) not null,city varchar(10),phone varchar(10),primary key(id) )

組合索引就是把多個列加入到統一個索引中,如新建的表t_user,我們給username+city+phone建立乙個組合索引

alter table t_user add index name_city_phone(username,city,phone) //組合普通索引

alter table t_user add unique name_city_phone(username,city,phone) //組合唯一索引

這樣的組合索引,其實相當於分別建立了(useranme,city,phone   username,city   username,phone)三個索引。

為什麼沒有(city,phone)索引呢?這是因為mysql組合查詢「最左字首」的結果。簡單的理解就是只從最左邊開始組合。

並不是查詢語句包含這三列就會用到該組合索引:

這樣的查詢語句才會用到建立的組合索引

select * from t_user where username="parry" and city="廣州" and phone="180"

select * from t_user where username="parry" and city="廣州"

select * from t_user where username="parry" and phone="180"

這樣的查詢語句是不會用到建立的組合索引

select * from t_user where city="廣州" and phone="180"

select * from t_user where city="廣州"

select * from t_user where phone="180"

索引不足之處

(1)索引提高了查詢的速度,但是降低了insert、update、delete的速度,因為在插入、修改、刪除資料時,還要同時操作一下索引檔案;

(2)建立索引未見會占用一定的磁碟空間。

索引使用注意事項

(1)只要列中包含null值將不會被包含在索引中,組合索引只要有一列含有null值,那麼這一列對於組合索引就是無效的,所以我們在設計資料庫的時候最好不要讓字段的預設值為null;

(2)使用短索引

如果可能應該給索引指定乙個長度,例如:乙個varchar(255)的列,但真實儲存的資料只有20位的話,在建立索引時應指定索引的長度為20,而不是預設不寫。如下

alter table t_user add index u_index(username(16)) 優於 alter table t_user add index u_index(username)

使用短索引不僅能夠提高查詢速度,而且能節省磁碟操作以及i/o操作。

(3)索引列排序

mysql在查詢的時候只會使用乙個索引,因此如果where子句已經使用了索引的話,那麼order by中的列是不會使用索引的,所以order by盡量不要包含多個列的排序,如果非要多列排序,最好使用組合索引。

(4)like 語句

一般情況下不是鼓勵使用like,如果非使用,那麼需要注意 like"%aaa%"不會使用索引;但like「aaa%」會使用索引。

(5)不使用 not in和<>操作

索引方式 hash和 btree比較

(1)hash

用於對等比較,如"="和" <=>"

(2)btree

btree索引看名字就知道索引以樹形結構儲存,通常用在像 "=,>,>=,

通過比較發現,我們常用的是btree索引方式,當然mysql預設就是btree方式。

MySQL 索引型別 索引方式

一 索引型別 1 normal 表示普通索引 2 unique 表示唯一索引,不允許重複的索引 3 full text 表示全文索引,用於在一篇文章中,檢索文字資訊的。二 建立索引的原則 1 選擇唯一性索引 2 為經常需要排序 分組和聯合操作的字段建立索引 3 為常作為查詢條件的字段建立索引 4 限...

mysql索引框架 MySQL架構和MySQL索引

1.mysql架構 1.1邏輯架構圖 1.1.1connection pool 連線池 管理緩衝使用者連線,執行緒處理等需要快取的需求。負責監聽對mysql server的各種請求,接收連線請求,所有連線請求到執行緒管理模組。每乙個連線上mysql server的客戶端請求都會被分配 或建立 乙個連...

mysql索引型別介紹 mysql索引型別介紹

索引型別介紹 主鍵索引 primary key 要求關鍵字不能重複,也不能為null,同時增加主鍵約束 主鍵索引定義時,不能命名 唯一索引 unique index 要求關鍵字不能重複,同時增加唯一約束 普通索引 index 對關鍵字沒有要求 全文索引 fulltext key 關鍵字的 不是所有欄...