MySQL查询优化
MySQL查询过程

MySQL逻辑查询处理的步骤序号

MySQL查询优化
in和exists如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用
in
反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用existsnot in和not exists如果查询语句使用了
not in,那么内外表都进行全表扫描,没有用到索引
而not extsts的子查询依然能用到表上的索引。所以无论那个表大,用not exists都比not in要快-
避免对索引字段进行计算操作、函数、类型转换 -
避免对索引字段上使用not、<>、!=、is null、is not null -
避免建立索引的列中使用空值 - where子句中
避免使用<>、!=in、not in、or、having、计算操作、函数select id from t where num in(1,2,3) -> select id from t where num between 1 and 3
select id from t where num/2=100 -> select id from t where num=100*2 -
将
or改用union all或union,视情况判断是否需要去重 - 分页查询优化
