您的位置:首页 > Web前端

MySQL Innodb redo log,double write,buffer pool一些关系

2014-10-22 15:26 597 查看
  这两天做doublewrite的开启与关闭的性能测试,测试过程中,发现对innodb几个重要的概念redo,double write,buffer pool之间的一些关系不是很明白,写下来以供参考。

一. Redo Log 和Buffer Pool

MySQL采用WAL(Write Ahead Loggin)来实现ACID中的D,当有数据修改时候,会先记录下redo log,这个时候被修改的页在buffer pool中称作dirty page,dirty page不会实时的刷新到对应的数据文件中的页面中,这样会对数据的性能有很大影响,那什么时候这些dirty page会刷新到数据文件中,这就需要介绍一个参数,叫作innodb_max_dirty_pages_pct。

 MySQL Reference Manual这样说明innodb_max_dirty_pages_pct:

  InnoDB tries to flush data from the buffer pool so that the percentageof dirty pages does not exceed this value. Specify an integer in the range from0 to 99. The default value is 75.

The innodb_max_dirty_pages_pct setting establishes atarget for flushing activity. It does not affect the rate of flushing.

它说明,这个参数是表示buffer pool中最多存多少比例的dirty page,超过这个数值,就会刷新的数据文件中。会不会有一种情况是当比例低于这个数值时候,就刷新dirty page到数据文件中呢?这是有可能的。

InnoDB performs certain tasks in the background, includingflushingof dirty pages (those pages that have been changed but are not yet written tothe database files) from the buffer pool. Currently, InnoDB flushes buffer poolpages if the percentage of dirty
pages in the buffer poolexceedsinnodb_max_dirty_pages_pct.

 

InnoDB uses an algorithm to estimate the required rate of flushing,based on the speed of redo log generation and the current rate of flushing. Theintent is to smooth overall performance by ensuring that buffer flush activitykeeps up with the need to keep
the buffer pool “clean”. Automatically adjustingthe rate of flushing can help to avoid sudden dips in throughput, whenexcessive buffer pool flushing limits the I/O capacity available for ordinaryread and write activity.

 

InnoDB内部会估算出一个刷新的速率,这个速率是根据redolog生成速度和当前的刷新速率计算出来的。目的就是通过刷新来使buffer pool相对“干净”(也就是低于max dirty page pct),从而提供一个比较平稳的性能。自动调节刷新速率能够减少性能上的突然下降,避免过度的刷新限制数据库读写性能。

 

InnoDB uses its log files in a circular fashion. Before reusing aportion of a log file, InnoDB flushes to disk all dirty buffer pool pages whoseredo entries are contained in that portion of the log file, a process known asa sharp checkpoint. If a workload
is write-intensive, it generates a lot ofredo information, all written to the log file. If all available space in thelog files is used up, a sharp checkpoint occurs, causing a temporary reductionin throughput. This situation can happen even though innodb_max_dirty_pages_pctis
not reached.

 

InnoDB循环使用redo log。在重新使用某个redolog时候,会将这部分redo log对应的buffer pool中的dirty page刷新到磁盘上。在一个大量写操作的环境中,产生大量redo log,如果超过这个log file的最大大小,就需要一个sharp checkpoint强制刷新数据,造成性能的急剧下降。这种情形即使在没有达到innodb_max_dirty_pages_pct情况下也会产生。

 

InnoDB uses a heuristic-based algorithm to avoid such a scenario, bymeasuring the number of dirty pages in the buffer pool and the rate at whichredo is being generated. Based on these numbers, InnoDB decides how many dirtypages to flush from the buffer pool
each second. This self-adapting algorithmis able to deal with sudden changes in the workload.

 

InnoDB通过dity page数量和redo log生成速度的一个启发式的算法来避免上面情况的产生。

 

二. Double Write Buffer

 Doube Write是innodb 表空间ibdata中一块连续的128page=2M的存储空间,它的作用的是处理产生partial write时候的data recovery。它的主要工作原理:

A. dirty page刷新到数据文件之前,先刷新到double write buffer里。

B. 然后将page内容刷新到数据文件中。

  如果在A中crash,因为此时数据文件中page没有修改,数据完整,没有问题。

  如果在B中crash,数据文件page已经被修改,但这时doublewrite buffer有数据完整备份,直接用它恢复数据文件中对应page。

  之前一直有个疑问,既然有了redolog,发生故障时候,直接用redo 恢复不就行了,为什么需要double write buffer。后来根据peter@percona和mysqllops的blog大致了解了一些,这里就必须说到InnoDB redo log的格式,我们知道有两种log模式,一种是logical log:记录db中对于row修改的sql 语句(statement based binlog),还有一种就是physical log:记录物理页的修改情况(rowbased binlog)。InnoDB
redo log采取两者结合的模式叫作physiological log,它根据每个page作记录,在每个page内再记录逻辑日志。这样就能理解为什么需要double write buffer,由于innodb page是16K,一般系统page是4k,当有个update语句需要对业内记录加1,当第一个4k中记录加1后,系统宕机,重启恢复时候,innodb 不知道从哪里给记录加1,如果给16k里所有记录都加1,就会导致第一个4k里面记录加2,必然导致数据不一致,这个时候double write buffer就解决了这个问题。

 

参考资料

http://dev.mysql.com/doc/refman/5.6/en/innodb-performance-adaptive_flushing.html
http://www.zhdba.com/mysqlops/2012/04/06/innodb-log2/ http://www.percona.com/blog/2006/08/04/innodb-double-write/ http://www.percona.com/blog/2014/05/23/improve-innodb-performance-write-bound-loads/
---------------------------------------------------------------------------------------------------------------------------------------------

版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!

Email:   lujian90@gmail.com

Blog:    http://blog.csdn.net/tntdb

Weibo: http://weibo.com/ericklu
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql buffer_pool innodb