SELECT id, cu_id,name, info, biz_type, gmt_create, gmt_modified,start_time, end_time, market_type, back_leaf_category,item_status,picuture_url FROM relation where biz_type ='0'AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20;
表的數據量大致有36w左右,該sql是一個非常典型的排序+分頁查詢:order by col limit N,OFFSET M , MySQL 執行此類sql時需要先掃描到N行,然后再去取 M行。對于此類大數據量的排序操作,取前面少數幾行數據會很快,但是越靠后,sql的性能就會越差,因為N越大,MySQL 需要掃描不需要的數據然后在丟掉,這樣耗費大量的時間。
SELECT a.* FROM relation a,(select id from relation where biz_type ='0'AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20 ) b where a.id=b.id
root@xxx 12:33:43>explain SELECT a.* FROM relation a,(select id from relation where biz_type ='0'AND end_time >='2014-05-29' ORDER BY id asc LIMIT 149420 ,20 ) b where a.id=b.id;