mysql sql force_index 优化案例
来源:互联网 发布:谷歌浏览器 for mac 编辑:程序博客网 时间:2024/04/30 06:47
下面的sql运行了27秒
explain SELECT
-> COUNT(1)
-> FROM
-> (SELECT
-> t3.store_id
-> FROM
-> dmall_tmp.wm_order t1 , dmall_oop.store t2, dmall_dw.dm_store t3
-> WHERE
-> t1.shop_id = t2.sap_id
-> AND t2.id = t3.ori_store_id
-> AND t1.order_create_time >= 20160504
-> AND t1.order_create_time <= 20160505
-> AND t3.store_id IN (718 , 719, 720, 4262, 4263, 4264, 4265, 4266, 4267, 4268, 4269, 4270, 4271, 4272, 4439, 4440, 4637, 4638, 4639, 5709, 5710, 5712, 5713)
-> GROUP BY DATE_FORMAT(t1.order_create_time, ‘%Y%m%d’) , t3.store_id) a;
+——+————-+————+——–+—————————+———+———+————————–+——+———————————————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+——+————-+————+——–+—————————+———+———+————————–+——+———————————————-+
| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 5451 | |
| 2 | DERIVED | t3 | range | PRIMARY,iindex | PRIMARY | 4 | NULL | 23 | Using where; Using temporary; Using filesort |
| 2 | DERIVED | t2 | eq_ref | PRIMARY,sap_id | PRIMARY | 4 | dmall_dw.t3.ori_store_id | 1 | Using where |
| 2 | DERIVED | t1 | ref | shop_id,order_create_time | shop_id | 8 | dmall_oop.t2.sap_id | 237 | Using index condition; Using where |
+——+————-+————+——–+—————————+———+———+————————–+——+———————————————-+
4 rows in set (0.00 sec)
我们看到在最下面的一行中,使用了shop_id去连接了,没有使用到order_create_time,而时间列的过滤性是十分强的,最好的情况就是按时间过滤后,再去关联,这样结果集就小很多,按照这个思路,我们让t1使用时间列上的索引
explain SELECT
-> count(*)
-> FROM
-> dmall_tmp.wm_order t1 FORCE INDEX(order_create_time), dmall_oop.store t2, dmall_dw.dm_store t3
-> WHERE
-> t1.shop_id = t2.sap_id
-> AND t2.id = t3.ori_store_id
-> AND t1.order_create_time >= 20160504
-> AND t1.order_create_time <= 20160505
-> AND t3.store_id IN (718 , 719, 720, 4262, 4263, 4264, 4265, 4266, 4267, 4268, 4269, 4270, 4271, 4272, 4439, 4440, 4637, 4638, 4639, 5709, 5710, 5712, 5713);
+——+————-+——-+——–+——————-+——————-+———+————————–+——-+————————————+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+——+————-+——-+——–+——————-+——————-+———+————————–+——-+————————————+
| 1 | SIMPLE | t3 | range | PRIMARY,iindex | PRIMARY | 4 | NULL | 23 | Using where |
| 1 | SIMPLE | t2 | eq_ref | PRIMARY,sap_id | PRIMARY | 4 | dmall_dw.t3.ori_store_id | 1 | Using where |
| 1 | SIMPLE | t1 | range | order_create_time | order_create_time | 9 | NULL | 87108 | Using index condition; Using where |
+——+————-+——-+——–+——————-+——————-+———+————————–+——-+————————————+
虽然t1的结果集变大了,但是没有了,实际执行时间下将到2s
- mysql sql force_index 优化案例
- force_index优化sql
- MySQL中SQL优化案例
- MySQL SQL 优化案例:JOIN派生表
- mysql无法优化的sql案例
- MySQL SQL优化案例:相关子查询(dependent subquery)优化
- MySQL 对一段时间内每天数据统计案例--sql优化
- MySQL 对一段时间内每天数据统计案例--sql优化
- sql优化-典型案例
- SQL优化案例(1)
- SQL优化案例
- SQL优化案例-1
- SQL优化案例一则
- SQL优化案例1
- SQL优化案例150811
- SQL优化案例150812
- mysql优化案例分析
- mysql 查询优化案例
- Linux的grep命令
- 重新安装python时出现的问题
- Android学习总结
- Leetcode 67. Add Binary
- LeetCode题解-206-Reverse Linked List Ⅰ
- mysql sql force_index 优化案例
- 快速进入陌生领域: HistCite 使用教程
- 初见Android 6.0新特性
- android shardsdk 一键分享点击无界面问题
- maven配置setting.xml详解
- leetcode 28 Implement strStr() C++
- IOS培训实战教程之开发的21个奇葩技巧
- 【编程马拉松】【015-走迷宫】
- LeetCode 70. Climbing Stairs