MySQL四種常用索引型別

2021-08-07 15:36:43 字數 4483 閱讀 3764

提到mysql優化,索引優化是必不可少的。其中一種優化方式 ——索引優化,新增合適的索引能夠讓專案的併發能力和抗壓能力得到明顯的提公升。

我們知道專案效能的瓶頸主要是在」查(select)」語句,要提公升」查」這一效能,mysql索引是必不可少的。接下來總結一下mysql常見的四種索引

一. 四種索引(主鍵索引/普通索引/全文索引/唯一索引)

1.索引的新增

1.1主鍵索引的新增

當一張表,把某個列設為主鍵的時候,則該列就是主鍵索引

[sql]view plain

copy

print?

create

table a(  

id int

primary

key auto_increment,  

name

varchar(20) not

null

default

」);  

//這裡id就是表的主鍵  

create table a(

id int primary key auto_increment,

name varchar(20) not null default ''

);//這裡id就是表的主鍵

如果當建立表時沒有指定主鍵索引,也可以在建立表之後新增:

alter table table_name add primary key (column name);

1.2普通索引

普通索引一般是在建表後再新增的,

create index 索引名 on table_name(column1,column2);

alter table table_name add index 索引名(column1,column2);

1.3全文索引

[sql]view plain

copy

print?

create

table c(  

id int

primary

key auto_increment ,  

title varchar(20),  

content text,  

fulltext(title,content)  

)engine=myisam charset utf8;  

insert

into c(title,content) values

(』mysql tutorial』,『dbms stands for database …』),  

(』how to use mysql well』,『after you went through a …』),  

(』optimizing mysql』,『in this tutorial we will show …』),  

(』1001 mysql tricks』,『1. never run mysqld as root. 2. …』),  

(』mysql vs. yoursql』,『in the following database comparison …』),  

(』mysql security』,『when configured properly, mysql …』);  

create table c(

id int primary key auto_increment ,

title varchar(20),

content text,

fulltext(title,content)

)engine=myisam charset utf8;

insert into c(title,content) values

('mysql tutorial','dbms stands for database ...'),

('how to use mysql well','after you went through a ...'),

('optimizing mysql','in this tutorial we will show ...'),

('1001 mysql tricks','1. never run mysqld as root. 2. ...'),

('mysql vs. yoursql','in the following database comparison ...'),

('mysql security','when configured properly, mysql ...');

使用全文索引常見的錯誤:

select * from c where content like 「%mysql%」;

這裡並不會使用全文索引,可以用explain進行檢視。正確用法:

select *  from c where match(title,content) against (『mysql』);

備註:1.  在mysql中fulltext 索引只針對 myisam生效

2.  mysql自己提供的fulltext針對英文生效->sphinx(coreseek)技術處理中文

3.  使用方法是 match(欄位名..) against(『關鍵字』)

1.4唯一索引

[sql]view plain

copy

print?

create

table d(id int

primary

key auto_increment , name

varchar(32) unique)  

create table d(id int primary key auto_increment , name varchar(32) unique)
d表中name就是唯一索引,唯一索引可以有多個null,不能是重複的內容

相比主鍵索引,主鍵字段不能為null,也不能重複

2. 查詢索引

show indexes from table_name;

show keys from table_name;

3.刪除索引

alter table table_name drop index 索引名;

二. 索引的機制

2.1 為什麼我們新增完索引後查詢速度為變快?

傳統的查詢方法,是按照表的順序遍歷的,不論查詢幾條資料,mysql需要將表的資料從頭到尾遍歷一遍

在我們新增完索引之後,mysql一般通過btree演算法生成乙個索引檔案,在查詢資料庫時,找到索引檔案進行遍歷(折半查詢大幅查詢效率),找到相應的鍵從而獲取資料

2.2 索引的代價

1. 建立索引是為產生索引檔案的,占用磁碟空間

2. 索引檔案是乙個二叉樹型別的檔案,可想而知我們的dml操作同樣也會對索引檔案進行修改,所以效能會下降

2.3 在哪些column上使用索引?

1 .較頻繁的作為查詢條件字段應該建立索引 

2. 唯一性太差的字段不適合建立索引,儘管頻繁作為查詢條件,例如gender性別字段

3. 更新非常頻繁的字段不適合作為索引

4.不會出現在where子句中的字段不該建立索引

總結: 滿足以下條件的字段,才應該建立索引.

a: 肯定在where條經常使用 b: 該字段的內容不是唯一的幾個值 c: 字段內容不是頻繁變化

三.索引使用注意事項

1.對於建立的多列索引,只要查詢條件使用了最左邊的列,索引一般就會被使用。

比如我們對title,content 新增了復合索引

select * from table_name where title = 『test』;會用到索引

select * from table_name where content = 『test』;不會用到索引

2.對於使用like的查詢,查詢如果是 『%a』不會使用到索引 ,而 like 『a%』就會用到索引。最前面不能使用%和_這樣的變化值

3.如果條件中有or,即使其中有條件帶索引也不會使用。

4.如果列型別是字串,那一定要在條件中將資料使用引號引用起來。

四.如何檢視索引使用的情況:

show status like『handler_read%』;

注意: 

handler_read_key:這個值越高越好,越高表示使用索引查詢到的次數。

handler_read_rnd_next:這個值越高,說明查詢低效。

mysql的四種索引型別

一 索引的型別 mysql索引的四種型別 主鍵索引 唯一索引 普通索引和全文索引。通過給字段新增索引可以提高資料的讀取速度,提高專案的併發能力和抗壓能力。索引優化時mysql中的一種優化方式。索引的作用相當於圖書的目錄,可以根據目錄中的頁碼快速找到所需的內容。主鍵索引 主鍵是一種唯一性索引,但它必須...

說說mysql的四種索引型別

我們都知道 mysql索引的四種型別 主鍵索引 唯一索引 普通索引和全文索引。通過給字段新增索引可以提高資料的讀取速度,提高專案的併發能力和抗壓能力。索引優化時mysql中的一種優化方式。索引的作用相當於圖書的目錄,可以根據目錄中的頁碼快速找到所需的內容,借助參考案例 www.dc3688.com ...

mysql欄位型別索引 mysql的四種索引型別

一 索引的型別 mysql索引的四種型別 主鍵索引 唯一索引 普通索引和全文索引。通過給字段新增索引可以提高資料的讀取速度,提高專案的併發能力和抗壓能力。索引優化時mysql中的一種優化方式。索引的作用相當於圖書的目錄,可以根據目錄中的頁碼快速找到所需的內容。主鍵索引 主鍵是一種唯一性索引,但它必須...