一、尽可能不要有空判断的语句,由于空判断将致使全表扫描,而不是索引扫描。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")