10.查询截取分析_查询优化

1.永远小表驱动大表

  exist 和 in:exist返回 true /false,in 返回具体的结果

  1.select * from A where exists (select 1 from B where B.id = A.id)

  使用 exist,会先查询A,再去查询B里面 B.id = A.id的数据

  2.select * from A where A.id in (select id from B);

  使用 in,会先查询B的id,再去查询A中A.id=B.id的数据

 

2.order by 关键字优化

  1.ORDER BY 子句,尽量使用Index方式排序,避免出现FileSort方式排序

    ORDER BY 满足两情况,会使用Index 方式排序  

    1.ORDER BY 语句使用索引最左前列

    2.使用 where 子句 与 order by 子句条件组合满足索引最左前列

  2.尽可能在索引列上完成排序操作,遵循索引建的最佳左前缀

  3.如果不在索引列上,filesort有两种算法:双路排序(两次IO) 和 单路排序(一次IO总体来说更好一点)

  4.优化策略

    1.增大sort_buffer_size 参数的设置

    2.增大max_length_for_sort_data  参数的设置

 

3.GROUP BY 关键字优化

  1.group by 实质是先排序后进行分组,遵循索引建的最佳左前缀

  2.当无法使用索引列时,增大max_length_for_sort_data、sort_buffer_size

  3.where 高于 having,能写 where ,就不写 having

 

    

  

 

posted @ 2017-08-15 20:40  白日梦想家12138  阅读(133)  评论(0编辑  收藏  举报