MongoDB在Linux上运行慢可从以下方面优化:
硬件与系统配置
vm.swappiness=10
减少内存换页。MongoDB配置优化
/etc/mongod.conf
:
storage.wiredTiger.engineConfig.cacheSizeGB
为可用内存的50%-70%。operationProfiling
监控慢查询。索引与查询优化
explain()
分析查询计划,避免全表扫描。性能监控与维护
mongostat
、mongotop
或第三方工具(如PMM)监控性能。compact
命令整理碎片。其他措施
insertMany
)减少网络开销。根据实际场景调整参数,优先通过监控定位瓶颈再针对性优化。