標籤:bsp span 演算法 子查詢 結構 快速 重複 主鍵索引 條件查詢
上一章給大家說的是資料庫的視圖,預存程序等等操作,這章主要講索引,以及索引注意事項,如果想看前面的文章,url如下:
- MYSQL入門全套(第一部)
- MYSQL入門全套(第二部)
索引簡介
索引是對資料庫表中一個或多個列(例如,employee 表的姓名 (name) 列)的值進行排序的結構。如果想按特定職員的姓來尋找他或她,則與在表中搜尋所有的行相比,索引有助於更快地擷取資訊。
例如這樣一個查詢:select * from table1 where id=10000。如果沒有索引,必須遍曆整個表,直到ID等於10000的這一行被找到為止;有了索引之後(必須是在ID這一列上建立的索引),即可在索引中尋找。由於索引是經過某種演算法最佳化過的,因而尋找次數要少的多。可見,索引插敘的速度要比沒有索引的速度要快很多
MySQL中常見索引有:
下面就應用一下索引吧
索引操作
一、普通索引(index)
普通所以只有一個功能,就是加快尋找速度。操作如下
1、先建立一個表
create table tab1( nid int not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, extra text, index ix_name (name))
2、建立索引
create index 索引名稱 on 表名(列名)
3、刪除索引
drop 索引名稱 on 表名;
4、查看索引
show index from 表名;
5、注意事項(對於建立索引時如果是BLOB 和 TEXT 類型,必須指定length。)
create index index_name on tab1(extra(32));
二、唯一索引(unique)
唯一性索引unique index和一般索引normal index最大的差異就是在索引列上增加了一層唯一約束。添加唯一性索引的資料列可以為空白,但是只要存在資料值,就必須是唯一的。
1、建立表+唯一索引
create table tab2( nid int not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, extra text, unique ix_name (name) -- 重點在這裡)
2、建立索引
create unique index 索引名 on 表名(列名)
3、刪除索引
drop unique index 索引名 on 表名
三、主鍵索引
在資料庫圖表中為表定義一個主鍵將自動建立主鍵索引,主鍵索引是唯一索引的特殊類型。主鍵索引要求主鍵中的每個值是唯一的。當在查詢中使用主鍵索引時,它還允許快速存取資料。資料不可為空
1、建立表+主鍵索引
create table in1( nid int not null auto_increment, name varchar(32) not null, email varchar(64) not null, extra text, primary key(nid), index zhang (name))
2、建立主鍵
alter table 表名 add primary key(列名);
3、刪除主鍵
alter table 表名 drop primary key;alter table 表名 modify 列名 int, drop primary key;
四、複合式索引
複合式索引,就是組合查詢的意思嘛嘻嘻,將兩列或者多列組合成一個索引進行查詢
其應用情境為:頻繁的同時使用n列來進行查詢,如:where name = ‘張岩林‘ and email = 666。
1、建立表
create table in3( nid int not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, extra text)
2、建立複合式索引
create index ix_name_email on in3(name,email);
如上建立複合式索引之後,查詢有的會使用索引,有的不會:
- name and email -- 使用索引
- name -- 使用索引
- email -- 不使用索引
索引注意事項
1、正確使用索引
資料庫表中添加索引後能夠讓查詢資料庫速度飛快,但前提必須是正確的使用索引來查詢,如果以錯誤的方式使用,則即使建立索引也會不奏效。
下面這些情況不會使用到索引:
1、like ‘%xx‘ select * from tb1 where name like ‘%cn‘;2、使用函數 select * from tb1 where reverse(name) = ‘張岩林‘;3、or select * from tb1 where nid = 1 or email=‘[email protected]‘; 特別的:當or條件中有未建立索引的列才失效,以下會走索引 select * from tb1 where nid = 1 or name = ‘zhangyanlin‘; select * from tb1 where nid = 1 or email = ‘[email protected]‘ and name = ‘aylin‘4、類型不一致 如果列是字串類型,傳入條件是必須用引號引起來,不然... select * from tb1 where name = 999;5、 != select * from tb1 where name != ‘aylin‘ 特別的:如果是主鍵,則還是會走索引 select * from tb1 where nid != 1236、 > select * from tb1 where name > ‘alex‘ 特別的:如果是主鍵或索引是整數類型,則還是會走索引 select * from tb1 where nid > 123 select * from tb1 where num > 1237、order by select email from tb1 order by name desc; 當根據索引排序時候,選擇的映射如果不是索引,則不走索引 特別的:如果對主鍵排序,則還是走索引: select * from tb1 order by nid desc; 8、 複合式索引最左首碼 如果複合式索引為:(name,email) name and email -- 使用索引 name -- 使用索引 email -- 不使用索引
2、其他注意事項
避免使用select *
count(1)或count(列) 代替 count(*)
建立表時盡量時 char 代替 varchar
表的欄位順序固定長度的欄位優先
複合式索引代替多個單列索引(經常使用多個條件查詢時)
盡量使用短索引
使用串連(JOIN)來代替子查詢(Sub-Queries)
連表時注意條件類型需一致
索引散列值(重複少)不適合建索引,例:性別不適合
3、執行計畫
explain + 查詢SQL - 用於顯示SQL執行資訊參數,根據參考資訊可以進行SQL最佳化
mysql> explain select * from tb2;+----+-------------+-------+------+---------------+------+---------+------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-------+------+---------------+------+---------+------+------+-------+| 1 | SIMPLE | tb2 | ALL | NULL | NULL | NULL | NULL | 2 | NULL |+----+-------------+-------+------+---------------+------+---------+------+------+-------+1 row in set (0.00 sec)
詳細介紹
4、limit分頁
分頁功能是個值得關注的問題,因為我們會一直用到
每頁顯示10條:當前 118 120, 125倒序: 大 小 980 970 7 6 6 5 54 43 3221 19 98 下一頁: select * from tb1 where nid < (select nid from (select nid from tb1 where nid < 當前頁最小值 order by nid desc limit 每頁資料 *【頁碼-當前頁】) A order by A.nid asc limit 1) order by nid desc limit 10; select * from tb1 where nid < (select nid from (select nid from tb1 where nid < 970 order by nid desc limit 40) A order by A.nid asc limit 1) order by nid desc limit 10;上一頁: select * from tb1 where nid < (select nid from (select nid from tb1 where nid > 當前頁最大值 order by nid asc limit 每頁資料 *【當前頁-頁碼】) A order by A.nid asc limit 1) order by nid desc limit 10; select * from tb1 where nid < (select nid from (select nid from tb1 where nid > 980 order by nid asc limit 20) A order by A.nid desc limit 1) order by nid desc limit 10;
至此mysql講解到此就結束啦,喜歡的給點個贊喲
-此文章轉載-轉載自:http://www.cnblogs.com/aylin/p/5777289.html
MySQL(三)