索引就像一本書的目錄同樣,若是在字段上創建索引,那麼以索引爲列的查詢條件時能夠加快查詢的速度,這個就是MySQL優化的重要部分mysql
整個表的每一條記錄的主鍵值在表內都是惟一的,用來惟一標識一條記錄sql
查詢數據庫,按主鍵查詢是最快的,每一個表只能有一個主鍵列,可是能夠有多個普通索引列。主鍵列要求列的全部內容必須惟一,而普通索引列不要求內容必須惟一數據庫
不管創建主鍵索引仍是普通的索引,都要在表的對應列上建立,能夠對單列建立索引,也能夠對多列建立索引緩存
建立主鍵索引的方法(在建立表的時候)--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:添加索引的時間取決於表中的數據的大小
一張表的一個字段惟一值很少的狀況下,但願建立一個惟一值更少的索引,因此就能夠把多個字段聯合起來建立索引(字段越多建立聯合索引,惟一值就越少)bash
在建立表的時候加上聯合索引 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個字符建立索引,這樣能夠節省建立索引的空間,以及下降讀取金和更新維護索引消耗的系統資源spa
建立前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後選擇數據的列上,另外咱們要儘可能選擇在惟一值多的大表上的列建立索引
建立索引的基本知識小結: 一、 索引相似書籍的目錄,會加快查詢數據的速度。 二、 要在表的列(字段)上建立索引。 三、 索引會加快查詢速度,可是也會會影響更新的速度,由於更新要維護索引數據。 四、 索引列並非越多越好,要在頻繁查詢的表語句where後的條件列上建立索引。 五、 小表或重複值不少的列上能夠不建索引,要在大表以及重複值少的條件列上建立索引. 六、 多個列聯合索引有前綴生效特性。 七、 當字段內容前N個字符己經接近惟一時,能夠對字段的前N個字符建立索引。 八、 索引從工做方式區分,有主鍵,惟一,普通索引。 九、 索引類型會有BTREE (默認)和hash (適合作緩存(內存數據庫)等