innodb 參數 之 doublewrite

什麼是double write:
doulbe write是開闢在innodb tablespace文件上的一塊有100個連續page的空間. 注意它是在innodb tablespace文件上. 它的作用是當mysql將數據刷新到data file的時候, 先將數據write + fsync()到double write空間上. 然後在某一個時刻會將數據從double write space寫到對應的真正需要寫到的page上.
爲什麼我們會需要double write:
這是因爲會存在partial write的問題. partial write是指mysql在將數據寫到數據文件的時候, 會出現只寫了一半但由於某種原因剩下的數據沒有寫到innodb file上. 出現這種問題可能是由於系統斷電, mysql crash造成的, 而造成這個問題更根本的原因是由於mysql 的page size跟系統文件的page size不一致, 導致在寫數據的時候, 系統並不是把整個buffer pool page一次性寫到disk上,所以當寫到一半時, 系統斷電,partial write也就產生了; 如果partial write產生, 會發生什麼問題呢? 因爲我們知道在flush buffer cache的時候,其實redo log已經寫好了. 爲什麼還需要擔心partial write呢? 這是因爲mysql在恢復的時候是通過檢查page的checksum來決定這個page是否需要恢復的, checksum就是當前這個page最後一個事務的事務號; 如果系統找不到checksum, mysql也就無法對這行數據執行寫入操作;
double write的優點是什麼?
double write解決了partial write的問題, 它能保證即使double write部分發生了partial write但也能恢復. 另外一個好處就是double write能減少redo log的量, 有了double write, redo log只記錄了二進制的變化量, 也就等同於binary log, 而通過前段時間的測試確實發現,在double write關閉的情況下, redo log比binary logs要大;
double write的缺點是什麼?
雖然mysql稱double write是一個buffer, 但其實它是開在物理文件上的一個buffer, 其實也就是file, 所以它會導致系統有更多的fsync操作, 而我們知道硬盤的fsync性能是很慢的, 所以它會降低mysql的整體性能. 但是並不會降低到原來的50%. 這主要是因爲: 1) double write是一個連接的存儲空間, 所以硬盤在寫數據的時候是順序寫, 而不是隨機寫, 這樣性能更高. 另外將數據從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以及臨控double write的使用
innodb_doublewrite=1表示啓動double write
show status like 'innodb_dblwr%'可以查詢double write的使用情況;
相關參數與狀態

是否打開了double write:

root@(none) 07:16:16>show variables like "%double%"; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | innodb_doublewrite | ON | +--------------------+-------+

Double write的使用情況:

root@(none) 07:15:50>SHOW STATUS LIKE "%innodb_dblwr%"; +----------------------------+-----------+ | Variable_name | Value | +----------------------------+-----------+ | Innodb_dblwr_pages_written | 145373349 | | Innodb_dblwr_writes | 2249336 | +----------------------------+-----------+

上面可以看到,從BP共Flush了145373349個Pages到double write buffer中;一共調用了2249336次write寫到真正的數據文件。可見,相當於每次write合併了 145373349 / 2249336 = 64.6次Flush。(這就是爲什麼double write buffer爲什麼並不會對效率有很大影響的原因)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章