一、儘可能不要有空判斷的語句,由於空判斷將致使全表掃描,而不是索引掃描。java
對於空判斷這種狀況,能夠考慮對這個列建立數據庫默認值數據庫
//空判斷將致使全表掃描 select small_id from small where sunshine is null; //能夠考慮在須要常常null判斷的列,增長默認值。假設有默認值:空字符串 select small_id from small where sunshine ="";
二、儘可能不要使用不等於條件,由於,這會致使全表掃描blog
對於不等於這種狀況,考慮改成範圍查詢解決索引
//不等於條件 將致使全表掃描 select sunshine from small where small_id <> 18; //可改成範圍查詢解決。假設18已是最大值了 select sunshine from small where small_id < 18
三、儘可能不要使用or條件,由於,這會致使全表掃描字符串
對於or這種狀況,能夠改成 分別查詢,而後 union allio
//or條件,將致使所有掃描 select sunshine from small where small_id = 6 or small_id = 8; //改成分別查詢,而後 union all select sunshine from small where small_id = 6 union all select sunshine from small where small_id = 8;
四、儘可能不要使用左右模糊查詢,由於,這會致使全表掃描class
對於左右模糊查詢的狀況,試着改成右側模糊查詢,這樣是能夠索引查找的select
//左右模糊查詢,將致使全表掃描 select small_id from small where sunshine like '%fun%'; //改成右側模糊查詢 select small_id from small where sunshine like 'fun%';
五、儘可能不要使用in條件,由於,這會致使全表掃描,宜用exists代替nio
//in查詢,將致使全表掃描 select sunshine from small where small_id in (select sun_id from sun where index = "shine"); //改成exists select sunshine from small where small_id exists (select sun_id from sun where index ="shine")