MySQL 优化之 MRR (Multi-Range Read:二级索引合并回表)
2015-11-25 16:20
806 查看
MySQL5.6中引入了MRR,专门来优化:二级索引的范围扫描并且需要回表的情况。它的原理是,将多个需要回表的二级索引根据主键进行排序,然后一起回表,将原来的回表时进行的随机IO,转变成顺序IO。文档地址:http://dev.mysql.com/doc/refman/5.6/en/mrr-optimization.html
Reading rows using a range scan on a secondary index can result in many random disk accesses to the base table when the table is large and not stored in the storage engine's cache. With the Disk-Sweep Multi-Range Read (MRR) optimization, MySQL tries to reduce the number of random disk access for range scans by first scanning the index only and collecting the keys for the relevant rows. Then the keys are sorted and finally the rows are retrieved from the base table using the order of the primary key. The motivation for Disk-sweep MRR is to reduce the number of random disk accesses and instead achieve a more sequential scan of the base table data.
首先对二级索引进行范围扫描,对于符合条件的 key, 按照主键进行排序,然后一起根据key来读取基表。
The Multi-Range Read optimization provides these benefits:
MRR enables data rows to be accessed sequentially rather than in random order, based on index tuples. The server obtains a set of index tuples that satisfy the query conditions, sorts them according to data row ID order, and uses the sorted tuples to retrieve data rows in order. This makes data access more efficient and less expensive.
MRR enables batch processing of requests for key access for operations that require access to data rows through index tuples, such as range index scans and equi-joins that use an index for the join attribute. MRR iterates over a sequence of index ranges to obtain qualifying index tuples. As these results accumulate, they are used to access the corresponding data rows. It is not necessary to acquire all index tuples before starting to read data rows.
MRR的主要优势:将随机IO转换成顺序IO;使用在 索引范围扫描 和 使用索引进行join 时;
The following scenarios illustrate when MRR optimization can be advantageous:
Scenario A: MRR can be used for
A portion of the index tuples are accumulated in a buffer.
The tuples in the buffer are sorted by their data row ID.
Data rows are accessed according to the sorted index tuple sequence.
When MRR is used, the
Example query for which MRR can be used, assuming that there is an index on
The index consists of tuples of
Without MRR, an index scan covers all index tuples for the
With MRR, the scan is broken up into multiple ranges, each for a single value of
To express this using interval notation, the non-MRR scan must examine the index range
Two
For MRR, a storage engine uses the value of the
MySQL的MRR一次扫描多少个二级索引,然后进行回表,其使用到的内存是参考 read_rnd_buffer_size 的值来决定的。
总结:
MRR 仅仅针对 二级索引 的范围扫描 和 使用二级索引进行 join 的情况。
MRR 的优势是将多个随机IO转换成较少数量的顺序IO。所以对于 SSD 来说价值还是有的,但是相比机械磁盘来说意义小一些。
Reading rows using a range scan on a secondary index can result in many random disk accesses to the base table when the table is large and not stored in the storage engine's cache. With the Disk-Sweep Multi-Range Read (MRR) optimization, MySQL tries to reduce the number of random disk access for range scans by first scanning the index only and collecting the keys for the relevant rows. Then the keys are sorted and finally the rows are retrieved from the base table using the order of the primary key. The motivation for Disk-sweep MRR is to reduce the number of random disk accesses and instead achieve a more sequential scan of the base table data.
首先对二级索引进行范围扫描,对于符合条件的 key, 按照主键进行排序,然后一起根据key来读取基表。
The Multi-Range Read optimization provides these benefits:
MRR enables data rows to be accessed sequentially rather than in random order, based on index tuples. The server obtains a set of index tuples that satisfy the query conditions, sorts them according to data row ID order, and uses the sorted tuples to retrieve data rows in order. This makes data access more efficient and less expensive.
MRR enables batch processing of requests for key access for operations that require access to data rows through index tuples, such as range index scans and equi-joins that use an index for the join attribute. MRR iterates over a sequence of index ranges to obtain qualifying index tuples. As these results accumulate, they are used to access the corresponding data rows. It is not necessary to acquire all index tuples before starting to read data rows.
MRR的主要优势:将随机IO转换成顺序IO;使用在 索引范围扫描 和 使用索引进行join 时;
The following scenarios illustrate when MRR optimization can be advantageous:
Scenario A: MRR can be used for
InnoDBand
MyISAMtables for index range scans and equi-join operations.
A portion of the index tuples are accumulated in a buffer.
The tuples in the buffer are sorted by their data row ID.
Data rows are accessed according to the sorted index tuple sequence.
When MRR is used, the
Extracolumn in
EXPLAINoutput shows
Using MRR.
Example query for which MRR can be used, assuming that there is an index on
()[/code]:key_part1, [code]key_part2
SELECT * FROM t WHERE key_part1 >= 1000 AND key_part1 < 2000 AND key_part2 = 10000;
The index consists of tuples of
(key_part1,
key_part2) values, ordered first by
key_part1and then by
key_part2.
Without MRR, an index scan covers all index tuples for the
key_part1range from 1000 up to 2000, regardless of the
key_part2value in these tuples. The scan does extra work to the extent that tuples in the range contain
key_part2values other than 10000.
With MRR, the scan is broken up into multiple ranges, each for a single value of
key_part1(1000, 1001, ... , 1999). Each of these scans need look only for tuples with
key_part2= 10000. If the index contains many tuples for which
key_part2is not 10000, MRR results in many fewer index tuples being read.
To express this using interval notation, the non-MRR scan must examine the index range
[{1000, 10000}, {2000, MIN_INT}), which may include many tuples other than those for which
key_part2= 10000. The MRR scan examines multiple single-point intervals
[{1000, 10000}], ...,
[{1999, 10000}], which includes only tuples with
key_part2= 10000.
Two
optimizer_switchsystem variable flags provide an interface to the use of MRR optimization. The
mrrflag controls whether MRR is enabled. If
mrris enabled (
on), the
mrr_cost_basedflag controls whether the optimizer attempts to make a cost-based choice between using and not using MRR (
on) or uses MRR whenever possible (
off). By default,
mrris
onand
mrr_cost_basedis
on. See Section 8.9.2, “Controlling Switchable Optimizations”.
For MRR, a storage engine uses the value of the
read_rnd_buffer_sizesystem variable as a guideline for how much memory it can allocate for its buffer. The engine uses up to
read_rnd_buffer_sizebytes and determines the number of ranges to process in a single pass.
MySQL的MRR一次扫描多少个二级索引,然后进行回表,其使用到的内存是参考 read_rnd_buffer_size 的值来决定的。
总结:
MRR 仅仅针对 二级索引 的范围扫描 和 使用二级索引进行 join 的情况。
MRR 的优势是将多个随机IO转换成较少数量的顺序IO。所以对于 SSD 来说价值还是有的,但是相比机械磁盘来说意义小一些。
相关文章推荐
- mysql更改表引擎INNODB为MyISAM的方法总结
- Mysql批量删除相同前缀或后缀的表
- mysql 二进制安装
- MySQL的线程池原理学习教程
- mysql增加新用户和无法登陆解决方法
- MySQL 5.7新特性
- MySQL主从复制
- mysql 优化
- MySQL的权限
- mysql导出部分(指定)数据库表字段
- MYSQLdump参数详解
- 美团网技术团队分享的MySQL索引及慢查询优化教程
- mysql 中insert into set用法
- 解决在mysql表中删除自增id数据后,再添加数据时,id不会自增1的问题
- DBCP连接mysql出现“8小时”问题解决
- MapReduce操作mysql数据库
- Mysql 学习笔记之索引篇
- MySQL导出导入数据
- 高性能MySQL第15章备份恢复
- MySQL对索引的使用