索引優化
db.users.createIndex({username: 1})
。db.orders.createIndex({userId: 1, orderDate: -1})
。reIndex()
重建索引,刪除冗余索引。查詢語句優化
db.users.find({}, {name: 1, email: 1})
。skip()
和 limit()
,避免大數據集全量返回。explain("executionStats")
分析查詢計劃,避免全表掃描。硬件與配置優化
mongod.conf
中的 wiredTiger.cacheSizeGB
(建議設為物理內存的50%~80%)。架構優化
監控與維護
mongostat
/mongotop
或第三方工具(如PMM)監控性能。