1、在where和order by 涉及的列上加索引;
2、引擎会放弃使用索引的情况:
(1)、避免where子句字段进行null值判断;
(2)、避免where子句中使用!=或<>操作符;
(3)、避免where子句中使用or。
3、慎用in 和not in,in和not in也会进行全表扫描;
4、like 子句尽量少用,也会全表扫描;
5、in后面跟的是小表,exists后面跟的是大表。
简记:in小,exists大。
exists 代替 in写法:
select num from a where num in(select num from b)
用下面的语句替换:
select num from a where exists(select 1 from b where num=a.num)
exists可以理解为将主查询的数据放入子查询中做条件验证,根据验证结果(true或false)来决定主查询的数据是否得以保留
6、避免使用select *;
7、from 记录条数多的放在左边;
8、where 可以过滤最大数量记录的条件放在 where 子句的末尾。