为了保证索引的有效性,最好在大数量下测试
1 如果mysql使用全表扫描要比使用索引快,则不使用索引 (所以如果实际测试中发现索引没生效,很可能是全表扫描和使用索引的效率差别不大)
2 如果查询条件 字段类型是字符串,则要加上引号,否则索引会失效
select * from table_emp where id !=1 应改为id!='1
3 如果在查询条件(建了索引)中用了or,则索引不生效(这就是尽量少用or的原因),如果使用了or,还想索引生效,则需要将or条件中的每个列都加上索引
select * from table_emp where id !=1 or name !='阿三' 需要为name建立索引
4 like 查询以 '%'开头
select * from table_emp where name like '%阿' 索引失效 ('阿%'索引有效)
5 对于多列索引,没有使用第一列,则不会使用索引 alter table table_emp add index sy(name,age,score);
索引sy就为多列索引,多列索引在以下几中情况下才能有效:
a) select * from table_emp where name='jia' and age>='12' //where条件中含有索引的首列字段和第二个字段
b) select * from table_emp where name='jia' //where条件中只含有首列字段
c) select * from table_emp where name='jia' and score<60//where条件中含有首列字段和第三个字
mysql索引类型 普通索引normal ,唯一索引unique,全文索引full text
主键都是唯一性索引
查询条件一般设置为普通索引