想要取出goods表中的不重复的cas的对应的goods_id数据。goods_id是主键  cas和is_check 和 is_sale 都建了常规索引   
select goods_id from hxj_goods where is_check=1 and is_sale=1 group by cas limit 51992,8 ---  执行了29秒。select goods_id from hxj_goods where is_check=1 and is_sale=1 group by cas limit  0,8  ---执行了0.002秒。select goods_id from (SELECT goods_id,cas from hxj_goods WHERE is_check=1 and is_sale=1) a group by cas limit 0,8----执行了16秒 ,如果limit更大  就更慢,比前面那一条还差。select goods_id from hxj_goods where is_check=1 and is_sale=1 limit 235472,8  ---执行了0.7秒   去掉group by条件后,但是结果不是想要的。select distinct(cas),goods_id from hxj_goods where is_check=1 and is_sale=1 limit 312,8 ---执行了0.001秒  使用distinct ,但是他的结果是对没一页的数据进行去重,不是全部数据去重再分页。select goods_id from (select distinct(cas),goods_id from hxj_goods where is_check=1 and is_sale=1) a limit 16,8 ----执行了8秒 很慢。

解决方案 »

  1.   

    这个快慢其实和 group by cas 和 limit有关系。第2个之所以快,相对于第1个,是group by生成了8条数据,就直接返回了,而第一个,的至少生成51992+8 条数据后,才能返回数据。
      

  2.   

    子查询在 5。7 的版本中有改进,2/3 应该不会有那么大的差距
    至于其他情况的性能差异,其实你自己写的已经很清楚了,不涉及 group by , 或者在 group by 前分页,性能差异当然小,如果 group by 之后 再分页,当然是越后的页,需要 group by 的数据越多,性能下载越厉害