關(guān)鍵字
innodb
version 5.7
replication
delay
參考&引用
- MySQL Docs: slave_rows_search_algorithms
- slave-performance-too-slow-with-row-based-events-causes-and-workarounds
問題描述
業(yè)務(wù)表沒有主鍵(唯一鍵), 主庫執(zhí)行了一條大量刪除的DELETE語句導(dǎo)致垛孔,導(dǎo)致Slave延時坝冕。
表結(jié)構(gòu)設(shè)計
由于binlog 是基于Row模式的,主庫的一條大量刪除DELETE語句會產(chǎn)生大量binlog日志馁蒂。這種情況下,從庫 SQL Thread 在重放Relay Log時骤素,由于表結(jié)構(gòu)不存在唯一鍵(沒有好的索引)烦却,導(dǎo)致所有重放都是一次全表掃描蹦误。
從設(shè)計上講咱筛,所有表都應(yīng)該有唯一主鍵,至少有自增主鍵(最好強制要求所有表均有自增主鍵)赞庶。這次的問題主要是項目引用的開源的項目(XXX), 表結(jié)構(gòu)設(shè)計的非常有問題训挡。
問題解決
表結(jié)構(gòu)補上唯一索引(或加上自增主鍵)是最好的方式。 但是如果因為一些原因歧强,無法改表結(jié)構(gòu)的情況,可以臨時通過調(diào)整slave_rows_search_algorithms
參數(shù)為INDEX_SCAN,HASH_SCAN
(默認(rèn)值INDEX_SCAN,TABLE_SCAN
)为肮,改變從庫復(fù)制表搜索算法摊册,來加速復(fù)制。
--動態(tài)參數(shù)
set global slave_rows_search_algorithms = 'INDEX_SCAN,HASH_SCAN';
官方文檔說明:
* The default value is INDEX_SCAN,TABLE_SCAN, which means that all searches that can use indexes do use them, and searches without any indexes use table scans.
* To use hashing for any searches that do not use a primary or unique key, set INDEX_SCAN,HASH_SCAN. Specifying INDEX_SCAN,HASH_SCAN has the same effect as specifying INDEX_SCAN,TABLE_SCAN,HASH_SCAN, which is allowed.
對照測試實驗
同一從庫上堆積一個大事務(wù)情況下,相同的Exec_Master_Log_Pos,相同時間內(nèi),先后觀察innodb undo 數(shù)相差10倍+(數(shù)值僅參考)颊艳。
INDEX_SCAN,TABLE_SCAN
INDEX_SCAN,HASH_SCAN