MySQL 性能优化之 延迟关联(deferred join)
来源:互联网 发布:php域名跳转 编辑:程序博客网 时间:2024/04/30 03:49
在高性能MySQL一书中,有这样的例子
select c1 from pro where c2='M' order by c3 limit 100000, 10;如何使它速度更快?
1:对c2+c3做多列二次索引。
2:利用延迟关联(deferred join)技术,通过覆盖索引(上面1建立的索引)来得到所需要的主键,然后再根据这些主键关联原表获得需要的行,
这样就减少了MYSQL扫描那些需要丢失的行数,代码如下:
select c1 from pro inner join (select primarykey, c1 from pro where pro2.c2='M' order by c3 limit 100000, 10)as pro2 using(primarykey, c1);
另外一个例子: 某业务数据库load 报警异常,cpu usr 达到30-40 ,居高不下。使用工具查看数据库正在执行的sql ,排在前面的大部分是:
- SELECT id, cu_id,name, info, biz_type, gmt_create, gmt_modified,start_time, end_time, market_type, back_leaf_category,item_status,picuture_url FROM relation where biz_type='0'AND end_time>='2014-05-29' ORDER BY id asc LIMIT 149420,20;
【分析】
针对limit 优化有很多种方式,
1 前端加缓存,减少落到库的查询操作
2 优化SQL
3 使用书签方式 ,记录上次查询最新/大的id值,向后追溯 M行记录。
4 使用Sphinx 搜索优化。
对于第二种方式 我们推荐使用"延迟关联" (deferred join) 的方法来优化排序操作,何谓"延迟关联" :通过使用覆盖索引查询返回需要的主键,再根据主键关联原表获得需要的数据。
【解决】
根据延迟关联的思路,修改SQL 如下:
优化前
点击(此处)折叠或打开
- root@xxx 12:33:48>explain SELECT id, cu_id,name, info, biz_type, gmt_create, gmt_modified,start_time, end_time, market_type, back_leaf_category,item_status,picuture_url FROM relation where biz_type=\'0\'AND end_time >=\'2014-05-29\' ORDER BY id asc LIMIT 149420,20;
- +----+-------------+-------------+-------+---------------+-------------+---------+------+--------+-----------------------------+
- | id | select_type| table |type | possible_keys| key | key_len| ref | rows | Extra |
- +----+-------------+-------------+-------+---------------+-------------+---------+------+--------+-----------------------------+
- | 1 | SIMPLE | relation | range| ind_endtime | ind_endtime| 9 | NULL| 349622| Using where; Using filesort|
- +----+-------------+-------------+-------+---------------+-------------+---------+------+--------+-----------------------------+
- 1 row in set (0.00 sec)
优化后:
点击(此处)折叠或打开
- SELECT a.* FROM relation a,(select id from relation where biz_type='0'AND end_time>='2014-05-29' ORDER BY id asc LIMIT 149420,20) b where a.id=b.id
- root@xxx 12:33:43>explain SELECT a.* FROM relation a,(select id from relation where biz_type ='0'AND end_time>='2014-05-29' ORDER BY id asc LIMIT 149420,20) b where a.id=b.id;
- +----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+
- | id | select_type| table |type | possible_keys| key | key_len| ref | rows | Extra|
- +----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+
- | 1 | PRIMARY |<derived2> | ALL | NULL | NULL | NULL | NULL| 20 | |
- | 1 | PRIMARY | a | eq_ref| PRIMARY | PRIMARY| 8 | b.id| 1 | |
- | 2 | DERIVED | relation | index | ind_endtime | PRIMARY | 8 | NULL| 733552| |
- +----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+
- 3 rows in set (0.36 sec)
优化后 执行时间 为原来的1/3 。
0 0
- MySQL 性能优化之 延迟关联(deferred join)
- MySQL性能优化之 延迟关联
- MySQL延迟关联性能优化方法改进版!!!
- 【MySQL】MySQL性能优化之Block Nested-Loop Join(BNL)
- 【MySQL】MySQL性能优化之Block Nested-Loop Join(BNL)
- MySQL优化之JOIN
- 浅析Mysql Join语法以及性能优化
- 浅析Mysql Join语法以及性能优化
- 浅析Mysql Join语法以及性能优化
- 浅析Mysql Join语法以及性能优化
- Mysql Join语法以及性能优化
- 【Hibernate】性能优化之延迟加载机制
- Android性能优化之延迟加载视图
- twisted学习笔记之: 延迟对象deferred
- mysql延迟关联
- MySQL 性能优化:性能提升 50%,延迟降低 60%
- 数据库性能优化之JOIN方法
- MYSQL之性能优化
- VTK:基于Qt的VTK右击菜单实现
- error :No module named google.protobuf.internal
- android 二进制流保存为bmp文件
- 回归分析算法
- 使用volley进行post操作
- MySQL 性能优化之 延迟关联(deferred join)
- Problem C
- 大神之路
- Cookie的安全性
- uva 10534 Wavio Sequence
- 计算机图形学(二)输出图元_8_像素编址和对象的几何要素
- [Leetcode]解题文档-ZigZag Conversion
- 学习JAVA之路001
- Git使用.gitignore文件