存储引擎选择
storage.engine: wiredTiger。数据压缩配置
storage:
wiredTiger:
engineConfig:
compression: snappy # 可选snappy/zlib/LZ4
```。
索引优化
db.collection.createIndex({field: 1}) // 单字段升序
db.collection.createIndex({field1: 1, field2: -1}) // 复合索引
```。
db.collection.reIndex()重建索引,删除冗余索引。分片与复制集
sh.enableSharding("dbName")
sh.shardCollection("dbName.collectionName", {shardKey: 1})
```。
硬件与配置优化
cacheSizeGB,建议设置为服务器内存的50%-70%)。存储路径管理
/etc/mongod.conf中storage.dbPath),确保权限正确:sudo chown -R mongodb:mongodb /custom/data/path
```。
监控与维护
mongostat/mongotop监控性能,识别慢查询。注意:优化前需在测试环境验证,优先保证数据安全。
参考来源: