共計 2698 個字符,預計需要花費 7 分鐘才能閱讀完成。
本篇內容介紹了“怎么掌握 MySQL 中的 double write”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
介紹 double write 之前我們有必要了解 partial page write 問題 :
InnoDB 的 Page Size 一般是 16KB,其數據校驗也是針對這 16KB 來計算的,將數據寫入到磁盤是以 Page 為單位進行操作的。而計算機硬件和操作系統,在極端情況下(比如斷電)往往并不能保證這一操作的原子性,16K 的數據,寫入 4K 時,發生了系統斷電 /os crash,只有一部分寫是成功的,這種情況下就是 partial page write 問題。
很多 DBA 會想到系統恢復后,My
可以根據 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
“怎么掌握 MySQL 中的 double write”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!