MySQL索引(六)


一、什么是索引

  索引就像一本書的目錄一樣,如果在字段上建立索引,那么以索引為列的查詢條件時可以加快查詢的速度,這個就是MySQL優化的重要部分

二、創建主鍵索引

  整個表的每一條記錄的主鍵值在表內都是唯一的,用來唯一標識一條記錄

  查詢數據庫,按主鍵查詢是最快的,每個表只能有一個主鍵列,但是可以有多個普通索引列。主鍵列要求列的所有內容必須唯一,而普通索引列不要求內容必須唯一

  無論建立主鍵索引還是普通的索引,都要在表的對應列上創建,可以對單列創建索引,也可以對多列創建索引

創建主鍵索引的方法(在創建表的時候)--PRI
create table test1(
id int(4) not null auto_increment,
name char(20) not null,
age tinyint(2) not null default '0',
dept varchar(16) default Null,
primary key(id), 			# 主鍵索引
key index_name(name)                      
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


查看主鍵索引
show index from test;

為創建好的表用sql添加主鍵(基本上不用)
alter table test change id id int primary key auto_increment;

PS:如果主鍵索引有auto_increment自增參數是不能刪除索引的,在工作中主鍵索引也不可能刪除

三、創建普通索引

創建普通索引(創建完表隨時會加)--MUL
在創建表的時候
create table test2(
id int(4) not null auto_increment,
name char(20) not null,
age tinyint(2) not null default '0',
dept varchar(16) default Null,
primary key(id), 				
key index_name(name)          # 普通索引            
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

查看索引
show index from test1;

刪除普通索引
alter table test1 drop index index_name;
drop index index_name on test1;

創建表后增加普通索引
alter table test1 add index index_name(name);


PS:添加索引的時間取決於表中的數據的大小

四、創建聯合索引

  一張表的一個字段唯一值不多的情況下,希望創建一個唯一值更少的索引,所以就可以把多個字段聯合起來創建索引(字段越多創建聯合索引,唯一值就越少)

在創建表的時候加上聯合索引
create table test3(
id int(4) not null auto_increment,
name char(20) not null,
age tinyint(2) not null default '0',
dept varchar(16) default Null,
primary key(id), 				
key index_name(name),
key index_name_dept(name,dept)  # 聯合索引                    
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

創建聯合索引
create index index_name_dept on test(name,dept);
alter table test add index index_name_dept(name,dept);

對字段的前n個字符創建聯合索引
create index index_name_dept on test(name(8),dept(10));
alter table test add index index_name_dept(name(8),dept(10));

刪除聯合索引
alter table test drop index index_name_dept;
drop index index_name_dept on test;

主鍵也可以創建聯合索引
create table test4(
id int(4) not null auto_increment,
name char(20) not null,
age tinyint(2) not null default '0',
dept varchar(16) default Null,
primary key(id,age),            # 主鍵的聯合索引				
key index_name(name),
key index_name_dept(name,dept)  # 聯合索引                    
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


PS:建立索引的原則:我們盡量在唯一值多的大表上建立索引

PS:聯合索引的前綴特性
  index(a,b,c)僅a,ab,abc三個查詢條件會走索引
  盡量把最常用的作為查詢條件的列,放在第一的位置

五、創建唯一索引(非主鍵)

創建非主鍵的唯一索引(相當於和主鍵類似)--UNI 可以為空
create unique index uni_index_name on test(name);

刪除索引
alter table test drop index uni_index_name;
drop index uni_index_name on test;

查看索引
show index from test;

六、給字段的前n個字符創建索引

  當一個字段的內容的前N個字符已經接近唯一的時候,我們可以對前n個字符創建索引,這樣可以節省創建索引的空間,以及降低讀取金和更新維護索引消耗的系統資源

創建前n個字符的索引
create index index_dept on test(dept(8));
alter table test add index index_dept(dept(8));

刪除索引
alter table test drop index index_dept;
drop index index_dept on test;

查看索引
show index from test;

七、關於索引的問題

既然索引可以加快查詢的速度,那么就可以給所有的列加上索引吧?
因為索引不但占用系統空間,而且更新數據時還需要維護索引數據,因此索引是雙刃劍,並不是越多越好,如:數十到幾百行的小表上無需創建索引,更新頻繁,讀取比較少的表要少建索引

需要在哪些列上創建索引呢?
select user from mysql.user where host='' 索引一定要創建在where后的條件列上,如果是連表查詢要創建在連接的列上,而不是創建在select后選擇數據的列上,另外我們要盡量選擇在唯一值多的大表上的列創建索引

八、索引的總結

創建索引的基本知識小結:
1、 索引類似書籍的目錄,會加快查詢數據的速度。
2、 要在表的列(字段)上創建索引。
3、 索引會加快查詢速度,但是也會會影響更新的速度,因為更新要維護索引數據。
4、 索引列並不是越多越好,要在頻繁查詢的表語句where后的條件列上創建索引。
5、 小表或重復值很多的列上可以不建索引,要在大表以及重復值少的條件列上創建索引.
6、 多個列聯合索引有前綴生效特性。
7、 當字段內容前N個字符己經接近唯一時,可以對字段的前N個字符創建索引。
8、 索引從工作方式區分,有主鍵,唯一,普通索引。
9、 索引類型會有BTREE (默認)和hash (適合做緩存(內存數據庫)等

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM