1、对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引(避免查询* select * from table)
2、避免在 where 子句中使用!=或<>或者or或者in或者not in或者like,否则将引擎放弃使用索引而进行全表扫描,in用exist代替
不推荐:select id from table where num=10 or num = 20
select num from a where num in(select num from b)
推荐:select id from table where num = 10 union all
select id from table where num = 20
select num from a where exists(select 1 from b where num=a.num)
3、不要对字段进行函数或者表达式操作,否则将引擎放弃使用索引而进行全表扫描
不推荐:select id from t where num/2=100
select id from t where substring(name,1,3)='abc'
推荐:select id from t where num=100*2
select id from t where name like 'abc%'
4、索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,一个表的索引数最好不要超过6个