1、查询优化应尽量避免全表扫描,考虑在where或order by条件字段上建立索引
~ 避免全表扫描的一些点:
1,where子句避免使用 != 或 <>;
2,where 子句避免中对字段进行 null 值判断,可以考虑字段非空设置,有默认值(0)
例:select id from tb where num is null
优:select id from t where num = 0
3,where 子句中应尽量避免使用 or 来连接条件
例:select id from tb where num=10 or num=20
优:select id from tb where num=10 union all select id from t where num=20
4,模糊查询尽量前边不使用%
例:select id from tb where name like '%abc%'
优:考虑全文搜索
5,慎用 in 和 not in,查询若连续考虑between ... and ...
例:select id from tb where num in(1,2,3)
优:select id from tb where num between 1 and 3
6,where 子句中避免尽量对字段进行表达式操作
例:select id from tb where num/2=100
优:select id from tb where num=100*2
7,where子句中避免尽量对字段进行函数操作
例:select id from tb where substring(name,1,3)='abc'
优:select id from tb where name like 'abc%'
例:select id from tb where datediff(day,createdate,'2005-11-30')=0--'2005-11-30'生成的id
优:select id from tb where createdate>='2005-11-30' and createdate<'2005-12-1'
待续。。。