Mongodb优化配置项

Evict数据

参数名称 含义 百分比
eviction_target 当Cache的使用量达到了对应的百分比时触发Evict线程淘汰page 80%
eviction_trigger 当Cache的使用量达到了对应的百分比时触发Evict线程和用户线程淘汰page 95%
eviction_dirty_target 当"脏数据"所占Cache达到对应的百分比触发Evict线程淘汰page 5%
eviction_dirty_trigger 当"脏数据"所占Cache达到对应的百分比触发Evict线程和用户线程淘汰page 20%

可以使用adminCommand动态设置或者使用环境变量

bash 复制代码
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig:`eviction_target=80, eviction_trigger=90,eviction_dirty_target=1,eviction_dirty_trigger=80`})

环境变量设置

shell 复制代码
WIREDTIGER_CONFIG=eviction_target=80,eviction_trigger=90,eviction_dirty_target=1,eviction_dirty_trigger=80

假如写入的数据量比较大,可以设置eviction_dirty_trigger=eviction_target=80。让脏数据和缓存的触发Evict比例相同。cache几乎为脏数据,之前缓存的干净数据会被Evict到磁盘里面。如果写入的数据是读取的数据,就会直接读取脏数据。在特定的情况下,可以这样配置。

满足eviction_dirty_trigger或者eviction_trigger,用户线程参与evict数据,所有的数据库操作将会被堵塞。应避免脏数据写满,或者调整eviction_dirty_trigger比例,避免用户线程参与evict数据。

Evict线程

默认evict线程为4,调整evict线程数,提高evict速度。

bash 复制代码
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig:`eviction=(threads_min=8,threads_max=16)`})

环境变量设置

shell 复制代码
WIREDTIGER_CONFIG=eviction=(threads_min=8,threads_max=16)

Checkpoint刷盘

bash 复制代码
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig:`checkpoint=(wait=30,log_size=1GB)`})

环境变量设置

shell 复制代码
WIREDTIGER_CONFIG=checkpoint=(wait=30,log_size=1GB)
参数名称 含义 默认值
wait seconds to wait between each checkpoint; setting this value above 0 configures periodic checkpoints. 60
log_size wait for this amount of log record bytes to be written to the log between each checkpoint. If non-zero, this value will use a minimum of the log file size. A database can configure both log_size and wait to set an upper bound for checkpoints; setting this value above 0 configures periodic checkpoints. 2GB

如果写入数据还没到Evict的比例,只能通过checkpoint定时刷盘。通过调整checkpoint的wait时间,即时把脏数据写入磁盘,避免用户线程参与evict数据。减少checkpoint的wait时间可以提交磁盘IO的利用率。

缓存大小

默认wiredTiger使用主机的一半内存。MongoDB除了wiredTiger的缓存占用内存外,线程池、网络连接,tcmalloc的内存分配器、操作系统的文件读写缓存都会占用主机内存。所以wiredTiger的缓存大小设置,并不一定就是MongoDB的内存占用大小。通常MongoDB会尽可能占满整个主机的内存。

bash 复制代码
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig:`cache_size=40GB`})

环境变量设置

shell 复制代码
WIREDTIGER_CONFIG=cache_size=40GB

hash bucket配置

集合数越多,需要打开的集合文件越多。在MongoDB启动的时候,都会去查找并存储集合文件的dhandle。大部分的时候都浪费在handleLock和schemaLock上。默认的buckets个数为512,可以调整buckets个数来减轻handleLock和schemaLock的时间。

bash 复制代码
timeWaitingMicros: { handleLock: 81530, schemaLock: 993787 } } protocol:op_msg 1201ms
bash 复制代码
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig:`hash=(buckets=65536,dhandle_buckets=65536)`})

环境变量设置

shell 复制代码
WIREDTIGER_CONFIG=hash=(buckets=65536,dhandle_buckets=65536)

读写并发设置

wiredTigerConcurrentReadTransactions

允许并发读取的最大值,默认128。

bash 复制代码
db.adminCommand( { setParameter: 1, wiredTigerConcurrentReadTransactions: <num> } )

wiredTigerConcurrentWriteTransactions

允许并发写入的最大值,默认128。

bash 复制代码
db.adminCommand( { setParameter: 1, wiredTigerConcurrentWriteTransactions: <num> } )

tcmalloc

tcmalloc提供了释放速度字段来调节缓存的释放速度tcmallocReleaseRate,从0-9,0表示永不释放,默认是1

bash 复制代码
db.adminCommand({setParameter:1, tcmallocReleaseRate:4})
相关推荐
万联WANFLOW1 小时前
外贸独立站:服务器放海外国内后台卡,放国内海外客户打不开——这道两头难怎么破?
运维·网络·数据库·经验分享
金海境科技1 小时前
【服务器数据恢复】H3C CAS虚拟化平台HP存储qcow2文件损坏快照丢失数据恢复案例
服务器·数据库·经验分享
正儿八经的少年2 小时前
Spring 事务保证数据一致性
java·数据库·spring
foo1st2 小时前
MySQL 8.0(Windows)升级笔记
数据库·笔记·mysql
张人玉2 小时前
基于 Vue 3 + ECharts + Express + SQLite 的可视化大屏与业务管理系统——YOLOv8 高精度车辆行人检测与计数系统
数据库·vue.js·yolo·sqlite·echarts·express
吴声子夜歌2 小时前
MongoDB 4.x——高级特性(Change Stream和事务)
数据库·mongodb
是店小二呀2 小时前
NanoPi R4S怎么搭私人云盘?iStoreOS与WebDAV完整教程
数据库·人工智能
Database_Cool_2 小时前
云数据库备份是怎么实现的、支持时间点恢复吗:阿里云 RDS MySQL 备份与 PITR 详解
数据库·mysql·阿里云
名字还没想好☜2 小时前
Go 的 select 实战:超时、非阻塞收发与优雅退出的三个套路
开发语言·数据库·golang·go·并发
NWU_LK2 小时前
【WebFlux】第六篇 —— 全链路响应式与数据库交互
java·数据库