mysql sql优化
来源:互联网 发布:淘宝如何刷信誉 编辑:程序博客网 时间:2024/06/05 10:45
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没有用上索引过滤,第二个用上索引先生成个临时表。
sql优化案例二:
explain SELECT erp_store_id from dmall_order.wm_order_merge a
-> WHERE
-> EXISTS
-> (
-> select *
-> FROM
-> dmall_ledger.off_ord_act_paid b
-> WHERE
-> a.id=b.order_id
-> and a.vender_id=1
-> and b.payment_time>=’2016-05-10 09:00:00’
-> and b.payment_time<=’2016-05-10 19:00:00’
-> ) ;
+——+————-+——-+——+———————————————+————————-+———+——————+———-+—————————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+——+————-+——-+——+———————————————+————————-+———+——————+———-+—————————-+
| 1 | PRIMARY | a | ALL | PRIMARY | NULL | NULL | NULL | 39799056 | Using where |
| 1 | PRIMARY | b | ref | off_ord_id_act_paid_idx,idx_payment_time_id | off_ord_id_act_paid_idx | 8 | dmall_order.a.id | 1 | Using where; FirstMatch(a) |
+——+————-+——-+——+———————————————+————————-+———+——————+———-+—————————-+
a表是个大表,优化器不智能,应该是先把b过滤完后,在驱动a去筛选条件,这种情况下,直接转换成表连接的方式,效果好很多
explain
select a.erp_store_id from dmall_order.wm_order_merge a ,dmall_ledger.off_ord_act_paid b where a.id=b.order_id
and a.vender_id=1
and b.payment_time>=’2016-05-10 09:00:00’
and b.payment_time<=’2016-05-10 19:00:00’ ;
+——+————-+——-+——–+———————————————+———————+———+————————-+——-+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+——+————-+——-+——–+———————————————+———————+———+————————-+——-+———————–+
| 1 | SIMPLE | b | range | off_ord_id_act_paid_idx,idx_payment_time_id | idx_payment_time_id | 8 | NULL | 48966 | Using index condition |
| 1 | SIMPLE | a | eq_ref | PRIMARY,idx_vender_id | PRIMARY | 8 | dmall_ledger.b.order_id | 1 | Using where |
+——+————-+——-+——–+———————————————+———————+———+————————-+——-+————-
- mysql优化---优化sql
- MySQL数据库优化--SQL
- MySQL 常用SQL优化
- SQL Mysql 表优化
- mysql SQL 优化1
- MySQL常见sql优化
- Mysql sql语句优化
- 【SQL】MySQL性能优化
- mysql sql优化
- mysql sql语句优化
- mysql优化sql语句
- MySQL SQL优化
- mysql---SQL语句优化
- mysql优化sql语句
- MySQL SQL优化
- MYSQL SQL优化
- MySQL SQL优化
- mysql sql优化
- ART是一个安装程序?或者一个操作系统?或者是一个虚拟机?
- UItextField实时监控输入框的文字
- Linux异步机制
- test文章
- Kafka 安装和测试
- mysql sql优化
- LintCode:买卖股票的最佳时机 II
- Spark SQL和DataFrame的学习总结
- alibaba的FastJson(高性能JSON开发包)
- c++第五次上机实验:项目一-奇数偶数分组
- 第10,11周 阅读程序写出运行结果 (3)
- Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重
- codeforces466E 并查集+离线处理+dfs
- C语言将16进制的数转换为字符串的方法