共計 3661 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章主要講解了“Mongo 中 MongoDB WiredTiger 引擎調(diào)優(yōu)技巧有哪些”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Mongo 中 MongoDB WiredTiger 引擎調(diào)優(yōu)技巧有哪些”吧!
MongoDB 從 3.0 開始引入可插拔存儲引擎的概念。當前,有不少存儲引擎可供選擇:MMAPV1、WiredTiger、MongoRocks、TokuSE 等等。每個存儲引擎都有自己的優(yōu)勢,你需要根據(jù)性能要求及應用特征挑選最適合的一個。
從 3.2.x 開始,WiredTiger 成為默認的存儲引擎。最為 MongoDB 目前最流行的存儲引擎,WiredTiger 與原先的 MMAPV1 相比有以下優(yōu)勢:
性能 并發(fā):在大多數(shù)工作負載下,WiredTiger 的性能要比 MMAPV1 高很多。WiredTiger 引擎為現(xiàn)代多核系統(tǒng)量身定制,更好地發(fā)揮多核系統(tǒng)的處理能力。MMAPV1 引擎使用表級鎖,因此,當某個單表上有并發(fā)的操作,吞吐將受到限制。WiredTiger 使用文檔級鎖,由此帶來并發(fā)及吞吐的提高。對于典型的應用,切到 WiredTiger 引擎,可帶來 5 -10 倍的性能提升。
壓縮 加密:MMAPV1 引擎要求數(shù)據(jù)在內(nèi)存和在磁盤的形式一致 (map 磁盤內(nèi)存映射)。因此,它并不支持壓縮和加密。WiredTiger 并沒有這層限制,可以更好地支持。
索引前綴壓縮:WiredTiger 存儲索引時使用前綴壓縮——相同的前綴只存一次。由此帶來的效果是:索引更小了,對物理內(nèi)存使用也更少了。
接下來,我會展示幾個用來調(diào)優(yōu) WiredTiger 引擎性能的關鍵參數(shù)。
調(diào)優(yōu) Cache Size
WiredTiger 最重要的調(diào)優(yōu)參數(shù)就是 cache 規(guī)模。默認,MongoDB 從 3.x 開始會保留可用物理內(nèi)存的 50%(3.2 是 60%) 作為數(shù)據(jù) cache。雖然,默認的設置可以應對大部分的應用,通過調(diào)節(jié)為特定應用找到最佳配置值還是非常值得的。cache 的規(guī)模必須足夠大,以便保存應用整個工作集 (working set)。
除了這個 cache,MongoDB 在做諸如聚合、排序、連接管理等操作時需要額外的內(nèi)存。因此,必須確保有足夠的內(nèi)存可供使用,否則,MongoDB 進程有被 OOM killer 殺死的風險。
調(diào)節(jié)這個參數(shù),首先要理解在默認配置下,cache 的使用情況。運行以下命令,可以獲得 cache 統(tǒng)計:
db.serverStatus().wiredTiger.cache
命令輸出結果例子如下:
{ tracked dirty bytes in the cache : 409861, tracked bytes belonging to internal pages in the cache : 738956332, bytes currently in the cache : 25769360777, tracked bytes belonging to leaf pages in the cache : 31473298388, maximum bytes configured : 32212254720, tracked bytes belonging to overflow pages in the cache : 0, bytes read into cache : 29628550664, bytes written from cache : 34634778285, pages evicted by application threads : 0, checkpoint blocked page eviction : 102, unmodified pages evicted : 333277, page split during eviction deepened the tree : 0, modified pages evicted : 437117, pages selected for eviction unable to be evicted : 44825, pages evicted because they exceeded the in-memory maximum : 74, pages evicted because they had chains of deleted items : 33725, failed eviction of pages that exceeded the in-memory maximum : 1518, hazard pointer blocked page eviction : 34814, internal pages evicted : 21623, maximum page size at eviction : 10486876, eviction server candidate queue empty when topping up : 8235, eviction server candidate queue not empty when topping up : 3020, eviction server evicting pages : 191708, eviction server populating queue, but not evicting pages : 2996, eviction server unable to reach eviction goal : 0, pages split during eviction : 8821, pages walked for eviction : 157970002, eviction worker thread evicting pages : 563015, in-memory page splits : 52, percentage overhead : 8, tracked dirty pages in the cache : 9, pages currently held in the cache : 1499798, pages read into cache : 2260232, pages written from cache : 3018846}
第一個要關注的數(shù)值試,cache 中臟數(shù)據(jù)的百分比。如果這個百分比比較高,那么調(diào)大 cache 規(guī)模很有可能可以提升性能。如果應用是重讀的,可再關注 bytes read into cache 這個指標。如果這個指標比較高,那么調(diào)大 cache 規(guī)模很有可能可以提升讀性能。
調(diào)節(jié) cache 規(guī)模不一定非得重啟服務,我們可以動態(tài)調(diào)整:
db.adminCommand( { setParameter : 1, wiredTigerEngineRuntimeConfig : cache_size=xxG})
如果你想讓調(diào)整在重啟后也有效,那么你需要將配置文件也相應調(diào)整一下。
控制 Read/Write Tickets
WiredTiger 使用 tickets 來控制可以同時被存儲引擎處理的讀 / 寫操作數(shù)。默認值是 128,在大部分情況下表現(xiàn)良好。如果這個值經(jīng)常掉到 0,所有后續(xù)操作將會被排隊等待。例如,觀察到讀 tickets 下降,系統(tǒng)可能有大量長耗時的操作 (未索引操作)。如果你想找出有哪些慢操作,可以用一些第三方工具。你可以根據(jù)系統(tǒng)需要和性能影響上下調(diào)節(jié) tickets。
運行以下命令可以確認 tickets 的使用情況:
db.serverStatus().wiredTiger.concurrentTransactions
下面是一個輸出例子:
{ write : { out : 0, available : 128, totalTickets : 128
}, read : { out : 3, available : 128, totalTickets : 128
}
}
同樣,可以動態(tài)調(diào)節(jié) tickets:
db.adminCommand( { setParameter: 1, wiredTigerConcurrentReadTransactions: xx } )db.adminCommand( { setParameter: 1, wiredTigerConcurrentWriteTransactions: xx } )
一旦做出調(diào)整,注意要觀察系統(tǒng)的性能監(jiān)控確保影響是符合預期的。
感謝各位的閱讀,以上就是“Mongo 中 MongoDB WiredTiger 引擎調(diào)優(yōu)技巧有哪些”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對 Mongo 中 MongoDB WiredTiger 引擎調(diào)優(yōu)技巧有哪些這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!