`
gaojingsong
  • 浏览: 1153449 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

【Innodb三大特性之double write】

阅读更多

InnoDB存储引擎有三大特性非常令人激动,它们分别是插入缓冲、两次写和自适应哈希

1、doublewrite buffer(mysql官方的介绍)

 

InnoDB uses a novel file flush technique called doublewrite. Before writing pages to the data files, InnoDB first writes them to a contiguous area called the doublewrite buffer. Only after the write and the flush to the doublewrite buffer have completed, does InnoDB write the pages to their proper positions in the data file. If there is an operating system, storage subsystem, or mysqld process crash in the middle of a page write, InnoDB can later find a good copy of the page from the doublewrite buffer during crash recovery.

 

Although data is always written twice, the doublewrite buffer does not require twice as much I/O overhead or twice as many I/O operations. Data is written to the buffer itself as a large sequential chunk, with a single fsync() call to the operating system.

 

To turn off the doublewrite buffer, specify the option innodb_doublewrite=0.

 

2、partial page write

 

InnoDB 的Page Size一般是16KB,其数据校验也是针对这16KB来计算的,将数据写入到磁盘是以Page为单位进行操作的。而计算机硬件和操作系统,在极端情况下 (比如断电)往往并不能保证这一操作的原子性,16K的数据,写入4K 时,发生了系统断电/os crash ,只有一部分写是成功的,这种情况下就是 partial page write 问题。

很多DBA 会想到系统恢复后,MySQL 可以根据redo log 进行恢复,而mysql在恢复的过程中是检查page的checksum,checksum就是pgae的最后事务号,发生partial page write 问题时,page已经损坏,找不到该page中的事务号,就无法恢复

 

所以说,当page 损坏之后,其实应用redo是没有意义的,这时候无法使用redo来恢复,因为原始页已经损坏了,会发生数据丢失。

 

3、doublewrite

在InnoDB将BP中的Dirty Page刷(flush)到磁盘上时,首先会将(memcpy函数)Page刷到InnoDB tablespace的一个区域中,我们称该区域为Double write Buffer(大小为2MB,每次写入1MB)。在向Double write Buffer写入成功后,第二步、再将数据拷贝到数据文件对应的位置。

 

当第二步过程中发生故障,也就是发生partial page write的问题。恢复的时候先检查页内的checksum是否相同,不一致,则直接从doublewrite中恢复。

 

1)如果写dw buffer失败。那么这些数据不会写到磁盘,innodb会载入磁盘原始数据和redo日志比较,并重新刷到dw buffer。

2)如果写dw buffer成功,但是刷新到磁盘失败,那么innodb就不会通过事务日志来恢复了,而是直接刷新dw buffer中的数据。

 

 

4、对性能的影响

 

系统需要将数据写两份,一般认为,Double Write是会降低系统性能的。peter猜测可能会有5-10%的性能损失,但是因为实现了数据的一致,是值得的。Mark Callaghan认为这应该是存储层面应该解决的问题,放在数据库层面无疑是牺牲了很多性能的。

 

事实上,Double Write对性能影响并没有你想象(写两遍性能应该降低了50%吧?)的那么大。在BP中一次性往往会有很多的Dirty Page同时被flush,Double Write则把这些写操作,由随机写转化为了顺序写。而在Double Write的第二个阶段,因为Double Write Buffer中积累了很多Dirty Page,所以向真正的数据文件中写数据的时候,可能有很多写操作可以合并,这样有可能会降低Fsync的调用次数。

 

基于上面的原因,Double Write并没有想象的那么糟。另外,Dimitri在测试后,发现打开和关闭Double Write对效率的影响并不大。

 

 

介绍double write之前我们有必要了解partial page write 问题 : 
    InnoDB 的Page Size一般是16KB,其数据校验也是针对这16KB来计算的,将数据写入到磁盘是以Page为单位进行操作的。而计算机硬件和操作系统,在极端情况下(比如断电)往往并不能保证这一操作的原子性,16K的数据,写入4K 时,发生了系统断电/os crash ,只有一部分写是成功的,这种情况下就是 partial page write 问题。
很多DBA 会想到系统恢复后,MySQL 可以根据redolog 进行恢复,而mysql在恢复的过程中是检查page的checksum,checksum就是pgae的最后事务号,发生partial page write 问题时,page已经损坏,找不到该page中的事务号,就无法恢复。

一 double write是什么?
    Double write 是InnoDB在 tablespace上的128个页(2个区)是2MB;
其原理:
    为了解决 partial page write 问题 ,当mysql将脏数据flush到data file的时候, 先使用memcopy 将脏数据复制到内存中的double write buffer ,之后通过double write buffer再分2次,每次写入1MB到共享表空间,然后马上调用fsync函数,同步到磁盘上,避免缓冲带来的问题,在这个过程中,doublewrite是顺序写,开销并不大,在完成doublewrite写入后,在将double write buffer写入各表空间文件,这时是离散写入。
如果发生了极端情况(断电),InnoDB再次启动后,发现了一个Page数据已经损坏,那么此时就可以从doublewrite buffer中进行数据恢复了。

二double write的缺点是什么?
    位于共享表空间上的double write buffer实际上也是一个文件,写DWB会导致系统有更多的fsync操作, 而硬盘的fsync性能, 所以它会降低mysql的整体性能. 但是并不会降低到原来的50%. 这主要是因为: 
1) double write 是一个连接的存储空间, 所以硬盘在写数据的时候是顺序写, 而不是随机写, 这样性能更高. 
2) 将数据从double write buffer写到真正的segment中的时候, 系统会自动合并连接空间刷新的方式, 每次可以刷新多个pages;

三 double write在恢复的时候是如何工作的?
If there’s a partial page write to the doublewrite buffer itself, the original page will still be on disk in its real location.-
--如果是写doublewrite buffer本身失败,那么这些数据不会被写到磁盘,InnoDB此时会从磁盘载入原始的数据,然后通过InnoDB的事务日志来计算出正确的数据,重新 写入到doublewrite buffer.
When InnoDB recovers, it will use the original page instead of the corrupted copy in the doublewrite buffer. However, if the doublewrite buffer succeeds and the write to the page’s real location fails, InnoDB will use the copy in the doublewrite buffer during recovery. 
--如果 doublewrite buffer写成功的话,但是写磁盘失败,InnoDB就不用通过事务日志来计算了,而是直接用buffer的数据再写一遍.
InnoDB knows when a page is corrupt because each page has a checksum at the end; the checksum is the last thing to be written, so if the page’s contents don’t match the checksum, the page is corrupt. Upon recovery, therefore, InnoDB just reads each page in the doublewrite buffer and verifies the checksums. If a page’s checksum is incorrect, it reads the page from its original location.
--在恢复的时候,InnoDB直接比较页面的checksum,如果不对的话,就从硬盘载入原始数据,再由事务日志 开始推演出正确的数据.所以InnoDB的恢复通常需要较长的时间.

四 我们是否一定需要 double write ?
In some cases, the doublewrite buffer really isn’t necessary—for example, you might want to disable it on slaves. Also, some filesystems (such as ZFS) do the same thing themselves, so it is redundant for InnoDB to do it. You can disable the doublewrite buffer by setting InnoDB_doublewrite to 0.

五  如何使用 double write
InnoDB_doublewrite=1表示启动double write
show status like 'InnoDB_dblwr%'可以查询double write的使用情况;
相关参数与状态
Double write的使用情况:
show status like  "%InnoDB_dblwr%";
InnoDB_dblwr_pages_written 从bp flush 到 DBWB的个数
InnoDB_dblwr_writes            写文件的次数
每次写操作合并page的个数= InnoDB_dblwr_pages_written/InnoDB_dblwr_writes

  • 大小: 55.7 KB
0
0
分享到:
评论

相关推荐

    MySQL三大特性之double write

    InnoDB uses a novel file flush technique called doublewrite. Before writing pages to the data files, InnoDB first writes them to a contiguous area called the doublewrite buffer. Only after the write ...

    MySQLInnoDB特性:两次写(DoubleWrite)

    本文来自于csdn,本文主要介绍了doublewrite技术进行解析,让大家充分理解doublewrite是如何做到保障数据页的可靠性。介绍doublewrite之前我们有必要了解partialpagewrite(部分页失效)问题。InnoDB的PageSize一般...

    InnoDB的关键特性-插入缓存,两次写,自适应hash索引详解

    InnoDB存储引擎的关键特性包括插入缓冲、两次写(double write)、自适应哈希索引(adaptivehashindex)。这些特性为InnoDB存储引擎带来了更好的性能和更高的可靠性。插入缓冲是InnoDB存储引擎关键特性中最令人激动...

    Mysql innodb 存储引擎全揭秘

    同时使用一种 -- next-key locking 的锁策略来避免幻读现象的产生,还提供了插入缓冲(insert buffer) 二次写(double write) 自适应哈希索引,预读(read ahead)等高性能和高可用的功能。对于表中的数据innodb...

    MySQL存储引擎InnoDB的配置与使用的讲解

    MyISAM和InnoDB是MySQL最常有的存储引擎,上一篇我们讲述了InnoDB与MyISAM之间的区别;...除此之外,InnoDB存储引擎还提供了插入缓冲(insert buffer)、二次读写(double write)、自适应哈希索引(adaptive hash

    MySQL20个经典常用面试题,需要找工作的一定要看

    一、MySQL 的复制原理以及流程 基本原理流程,3 个线程以及之间的关联;...(2)、innodb 引擎的 4 大特性 插入缓冲(insert buffer),二次写(double write),自适应哈希索引(ahi),预读(read ahead) (3)、

    大厂Mysql面试题在这里

    1、MySQL的复制原理以及流程 ...(2)、innodb引擎的4大特性 插入缓冲(insert buffer),二次写(double write),自适应哈希索引(ahi),预读(read ahead) (3)、2者selectcount(*)哪个更快,为什么 myisam更快,因为

    mysql储存相关的面试题目精选

    二次写(double write) 自适应哈希索引(ahi) 预读(read ahead) 3、InnoDB 为何推荐使用自增主键? 自增 ID 可以保证每次插入时 B+ 树索引是从右边扩展的,因此相比自定义 ID (如 UUID)可以避免 B+ 树的频繁合并和...

    mysql 断电不能启动

    2017-11-15 19:23:46 1404 [Note] InnoDB: from the doublewrite buffer... InnoDB: Doing recovery: scanned up to log sequence number 9219763629 InnoDB: 1 transaction(s) which must be rolled back or ...

    使用innodb_force_recovery解决MySQL崩溃无法重启问题

    InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer… InnoDB: Doing recovery: scanned up to log sequence number 9120034833 150125 16:12:51 InnoDB: Sta

    MySQL网络培训精品班-Inside君姜承尧

    day026-InnoDB_7 doublewrite ChangeBuffer AHI FNP MySQL 索引与innodb锁机制 day027-Secondary Index day028-join算法锁_1 day029-锁_2 day030-锁_3 day031-锁_4 day032-锁_5 day032-锁5标清 day033-锁_6 事物_1 ...

    Mysql IO 内存方面的优化

    (insert buffer segment/double write segment/rollback segment/index segment/dictionary segment/undo segment) ib_logfile0/ib_logfile1:事务日志/redolog mysql-relay-bin:中继日志 bin

Global site tag (gtag.js) - Google Analytics