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生成 uuid
// 生成UUID,并去掉分割符 function guid() { if (function_exists('com_create_guid')){ $uuid = com_create_guid( ...
- Yii2如何添加sql日志记录的配置信息
在使用Yii2框架的时候,常常会出现没有sql日志记录的问题.在代码里一句一句的打印sql语句也不现实.所以就要用文件记录起来. 在 config/web.php 里面的 log配置中增加如下配置 [ ...
- 小白的Python之路 day1
Python之路,Day1 - Python基础1 本节内容 Python介绍 发展史 Python 2 or 3? 一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van ...
- tyvj4865 天天和树tree
#include<bits/stdc++.h> #define MAXN 100000+10 using namespace std; *MAXN]; ,head[MAXN],pre[MA ...
- 物联网设备是如何被破解的?分析一种篡改IoT固件内容的攻击方式
随着智能硬件进入到人们的生活,人们的生活质量开始有逐步的提高,人们与智能硬件之间的联系更加紧密.同时,智能硬件的安全问题也必须引起高度重视,因为其直接影响到人身安全.社会安全和国家安全. 大家是否 ...
- js 继承的简单易懂小例子
js 继承 今天主要说原型链继承.构造继承.组合继承三种常用继承方式,分享一下我的理解. 原型链继承例子1 //原型继承function A(name){ this.name = name;}func ...
- Python爬虫(十八)_多线程糗事百科案例
多线程糗事百科案例 案例要求参考上一个糗事百科单进程案例:http://www.cnblogs.com/miqi1992/p/8081929.html Queue(队列对象) Queue是python ...
- OJ随笔——【1088-N!】——同余定理
题目如下: Description 请求N!(N<=10000),输出结果对10007取余输入每行一个整数n,遇到-1结束.输出每行一个整数,为对应n的运算结果. Sample Input ...
- iOS 读书笔记-国际化
吐槽一下:国际化-我想说学习的这个project好痛苦. 也许是百度的原因,总是不能找到自己想要东西. 找到的内容不是不具体就是时间有点久了.让我这种小白非常头痛. 以下记录一下整个过程. 国际化是什 ...
- Error: Failed to launch instance "win7": Please try again later [Error: No valid host was found. ].
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...