mysql limit优化

explain
-> select
-> a.id,b.shop_id,b.erp_store_id,b.shop_name,a.order_id,
-> SUM(a.amount) amount,
-> SUM(b.ware_total_price) wareAmount,
-> SUM(b.promotion_price) promotionAmout,
-> SUM(IF(a.payment_method=12,amount,0)) as unionPay,
-> SUM(IF(a.payment_method=13,amount,0)) as alipay,
-> SUM(IF(a.payment_method=10,amount,0)) as weChat,
-> SUM(IF(a.payment_method=53,amount,0)) as balance,
-> SUM(IF(a.payment_method=40,amount,0)) as mtCard
-> FROM xx a ,xxx b
-> where
-> a.order_id=b.id
-> and b.vender_id=1
-> and b.erp_store_id in (273)
-> and a.created>=’2016-05-09’
-> and a.created<=’2016-05-10’
-> GROUP by a.order_id
-> limit 0,10;
+——+————-+——-+——–+—————————————-+————————-+———+————————-+——+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+——+————-+——-+——–+—————————————-+————————-+———+————————-+——+————-+
| 1 | SIMPLE | a | index | off_ord_id_act_paid_idx,idx_created_bj | off_ord_id_act_paid_idx | 8 | NULL | 2328 | Using where |
| 1 | SIMPLE | b | eq_ref | PRIMARY | PRIMARY | 8 | xxx.a.order_id | 1 | Using where |
+——+————-+——-+——–+—————————————-+————————-+———+————————-+——+————-+
2 rows in set (0.00 sec)

explain
-> select * from (
-> select
-> a.id,b.shop_id,b.erp_store_id,b.shop_name,a.order_id,
-> SUM(a.amount) amount,
-> SUM(b.ware_total_price) wareAmount,
-> SUM(b.promotion_price) promotionAmout,
-> SUM(IF(a.payment_method=12,amount,0)) as unionPay,
-> SUM(IF(a.payment_method=13,amount,0)) as alipay,
-> SUM(IF(a.payment_method=10,amount,0)) as weChat,
-> SUM(IF(a.payment_method=53,amount,0)) as balance,
-> SUM(IF(a.payment_method=40,amount,0)) as mtCard
-> FROM xxx a ,xxx b
-> where
-> a.order_id=b.id
-> and b.vender_id=1
-> and b.erp_store_id in (273)
-> and a.created>=’2016-05-09’
-> and a.created<=’2016-05-10’
-> GROUP by a.order_id
-> ) t
-> limit 0,10;
+——+————-+————+——–+—————————————-+—————-+———+————————-+——-+——————————————————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+——+————-+————+——–+—————————————-+—————-+———+————————-+——-+——————————————————–+
| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 66778 | |
| 2 | DERIVED | a | range | off_ord_id_act_paid_idx,idx_created_bj | idx_created_bj | 8 | NULL | 66778 | Using index condition; Using temporary; Using filesort |
| 2 | DERIVED | b | eq_ref | PRIMARY | PRIMARY | 8 | xxxa.order_id | 1 | Using where |
+——+————-+————+——–+—————————————-+—————-+———+————————-+——-+——————————————————–+
3 rows in set (0.00 sec)
第一个sql没有用上索引过滤,第二个用上索引先生成个临时表。

你可能感兴趣的:(mysql)