mysql 的 fiter push down 优化
出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读。本文版权归作者所有,欢迎转载,但请保留该声明。
原SQL执行了 4秒:
mysql> select * from employees e inner join (select emp_no,count(*) from salaries group by emp_no) s on s.emp_no=e.emp
o where e.emp_no BETWEEN 10001 and 10010; | 10008 | 1958-02-19 | Saniya | Kalloufi | M | 1994-09-15 | 10008 | 3 |
| 10009 | 1952-04-19 | Sumant | Peac | F | 1985-02-18 | 10009 | 18 |
| 10010 | 1963-06-01 | Duangkaew | Piveteau | F | 1989-08-24 | 10010 | 6 |
+--------+------------+------------+-----------+--------+------------+--------+----------+
10 rows in set (4.11 sec)
执行计划, 中间有个auto_key 是mysql 临时在内存创建索引 , salaries表id 是 2 优先执行 , 如果这个表统计数据量很多,那么先等待它执行完之后才能再跟employee表关联增加执行时间 , 子查询有group by 就不能合并到视图里面去 , 这就是 “filter push-down” , “filter push-down”的目的是推动视图内的限制或不能合并的内联视图。 , 并没有重复使用mysql 优化器优势 :
mysql> explain select * from employees e inner join (select emp_no,count(*) from salaries group by emp_no) s on s.emp_no=e.emp_no where e.emp_no BETWEEN 10001 and 10010;
+----+-------------+------------+------------+-------+----------------------+-------------+---------+--------------------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+----------------------+-------------+---------+--------------------+---------+----------+-------------+
| 1 | PRIMARY | e | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 10 | 100 | Using where |
| 1 | PRIMARY | <derived2> | NULL | ref | <auto_key0> | <auto_key0> | 4 | employees.e.emp_no | 10 | 100 | NULL |
| 2 | DERIVED | salaries | NULL | index | PRIMARY,emp_no,idx_s | emp_no | 4 | NULL | 2694129 | 100 | Using index |
+----+-------------+------------+------------+-------+----------------------+-------------+---------+--------------------+---------+----------+-------------+
那么现在就是要想把emp 表 和 sal 表 同时统计同进进行关联提高效率 , 做法就是把两个表放到一个子查询里面 , 并且摆脱了mysql 临时在内存中创建索引 , 虽然还有using filesort , 但并不是重点 , 效率提升到 0.01 秒 :
select * from employees e inner join
(
select s.emp_no,count(*) from employees e inner join salaries s on s.emp_no = e.emp_no where e.emp_no BETWEEN 10001 and 10010 group by s.emp_no
) s where e.emp_no BETWEEN 10001 and 10010;
| 10008 | 1958-02-19 | Saniya | Kalloufi | M | 1994-09-15 | 10010 | 6 |
| 10009 | 1952-04-19 | Sumant | Peac | F | 1985-02-18 | 10010 | 6 |
| 10010 | 1963-06-01 | Duangkaew | Piveteau | F | 1989-08-24 | 10010 | 6 |
+--------+------------+------------+-----------+--------+------------+--------+----------+
100 rows in set (0.01 sec)
mysql> explain select * from employees e inner join
(
select s.emp_no,count(*) from employees e inner join salaries s on s.emp_no = e.emp_no where e.emp_no BETWEEN 10001 and 10010 group by s.emp_no
) s where e.emp_no BETWEEN 10001 and 10010;
+----+-------------+------------+------------+-------+----------------------+---------+---------+--------------------+------+----------+-----------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+----------------------+---------+---------+--------------------+------+----------+-----------------------------------------------------------+
| 1 | PRIMARY | e | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 10 | 100 | Using where |
| 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 93 | 100 | Using join buffer (Block Nested Loop) |
| 2 | DERIVED | e | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 10 | 100 | Using where; Using index; Using temporary; Using filesort |
| 2 | DERIVED | s | NULL | ref | PRIMARY,emp_no,idx_s | emp_no | 4 | employees.e.emp_no | 9 | 100 | Using index |
+----+-------------+------------+------------+-------+----------------------+---------+---------+--------------------+------+----------+-----------------------------------------------------------+
mysql 的 fiter push down 优化的更多相关文章
- MySql in子句 效率低下优化
MySql in子句 效率低下优化 背景: 更新一张表中的某些记录值,更新条件来自另一张含有200多万记录的表,效率极其低下,耗时高达几分钟. where resid in ( ); 耗时 365s ...
- MySQL有关Group By的优化
昨天我写了有关MySQL的loose index scan的相关博文(http://www.cnblogs.com/wingsless/p/5037625.html),后来我发现上次提到的那个优化方法 ...
- mysql笔记03 查询性能优化
查询性能优化 1. 为什么查询速度会慢? 1). 如果把查询看作是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.如果要优化查询,实际上要优化其子任务,要么消除其中一些子任务,要么减 ...
- MySql学习(七) —— 查询性能优化 深入理解MySql如何执行查询
本篇深入了解查询优化和服务器的内部机制,了解MySql如何执行特定查询,从中也可以知道如何更改查询执行计划,当我们深入理解MySql如何真正地执行查询,明白高效和低效的真正含义,在实际应用中就能扬长避 ...
- python操作三大主流数据库(5)python操作mysql⑤使用Jinja2模板提取优化页面展示
python操作mysql⑤使用Jinja2模板提取优化页面展示 在templates目录下的index.html.cat.html等页面有一些共同的元素,代码比较冗余可以使用模板提取公共代码,在各网 ...
- MySQL千万级大表优化解决方案
MySQL千万级大表优化解决方案 非原创,纯属记录一下. 背景 无意间看到了这篇文章,作者写的很棒,于是乎,本人自私一把,把干货保存下来.:-) 问题概述 使用阿里云rds for MySQL数据库( ...
- mysql 之 group by 性能优化 查询与统计分离
背景介绍 记录共128W条! SELECT cpe_id, COUNT(*) restarts FROM business_log WHERE operate_time>='2012-12- ...
- Mysql数据库写入数据速度优化
Mysql数据库写入数据速度优化 1)innodb_flush_log_at_trx_commit 默认值为1:设置为0,可以提高写入速度. 值为0:提升写入速度,但是安全方面较差,mysql服务器 ...
- MySQL的Innodb缓存相关优化
MySQL的Innodb缓存相关优化 INNODB 状态的部分解释 通过 命令 SHOW STATUS LIKE 'Innodb_buffer_pool_%' 查看 Innodb缓存使用率 (I ...
随机推荐
- 写一段PHP代码,确保多个进程同时写入同一个文件成功(腾讯)
- bitcms内容管理系统 3.1版源码发布
开源bitcms内容管理系统采用ASP.NET MVC5+MySql的组合开发,更适应中小型系统低成本运行. bitcms的主要功能 1.重写了APS.NET MVC的路由机制.bitcms使用路由参 ...
- NOIP2017 小凯的疑惑
题目描述 小凯手中有两种面值的金币,两种面值均为正整数且彼此互素.每种金币小凯都有 无数个.在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的.现在小 凯想知道在无法准确支付的物品中,最贵的 ...
- js 匹配2个字符串相似度
strSimilarity2Number: function (s, t) { var n = s.length, m = t.length, d = []; var i, j, s_i, t_j, ...
- 菜鸟版JAVA设计模式—外观模式
外观模式是一种比較easy理解的模式,作用非常easy.就是解耦合. 结构也是非常easy,一个外观类.这个外观类持有非常多的业务类. 再由客户类去调用这个外观类去实现一些列的业务操作... 这个模式 ...
- Scheme -- Hierarchical Structures
Question: produce a deep-reverse procedure that takes a list as argument and returns as its value t ...
- stanford-parser for C#
在项目里用到C#对英文句子进行词性标注.比較成熟的英文词性标注软件是stanford-parser.它个C#版本号,也是借助于IKVM完毕JAVA-C#的转换.详细配置过程例如以下: 1.下载stan ...
- Hibernate中使用@Lob 注解保存String[] 问题
Hibernate中使用@Lob 注解保存String[] 问题 在Hibernate注解中怎样你想保存一个字段为String数组类型.假设你想尝试保存为clob类型的话,普通情况下为定义为: @En ...
- Highcharts使用CSV格式数据绘制图表
Highcharts使用CSV格式数据绘制图表 CSV(Comma-Separated Values,逗号分隔值文本格式)是採用逗号切割的纯文本数据.通常情况下.每一个数据之间使用逗号切割,几个相关数 ...
- VMware 虚拟机 Ubuntu 登录后蓝屏问题
问题起因 在一次下班收工时关闭虚拟机 Ubuntu,出现异常:关机好久没有完成,进而导致 VMware 软件卡死.后来强行杀死 VMware.第二天上班,启动 VMware 后开启 Ubuntu,输入 ...