如何对数据库中的sql语句进行优化?
1、查询语句中不要使用select *
2、尽量减少子查询,使用关联查询(left join,right join,inner join)替代
3、减少使用 in 或者not in ,使用exist,not exist,或者关联查询语句替代
4、or 的查询尽量用 union 或者 union all 代替(在确定没有重复数据或者不用剔除重复数据时,union all 会更好)
5、应尽量避免在 where 字句中使用 != 或者 〈〉操作符,否则将引擎放弃使用索引而进行全表扫描
6、应尽量避免在 where 字句中对字段进行 Null 值判断,否则将导致引擎放弃使用索引而进行全表扫描。比如:select id from t where num is null 可以在 num 上设置默认值0,确保表中的 num列没有 null 值,然后这样查询:select id from t where num=0