以下是Ubuntu下MongoDB索引优化的关键技巧:
选择合适索引类型
db.users.createIndex({ email: 1 })。db.orders.createIndex({ userId: 1, status: -1 })。db.locations.createIndex({ coordinates: "2dsphere" })。优化索引设计
db.users.find({ name: "Alice" }, { name: 1, _id: 0 }).explain("executionStats")。db.collection.getIndexes()查看索引使用情况。配置与维护
storage.wiredTiger.engineConfig.cacheSizeGB),建议设置为服务器内存的50%-70%。db.collection.reIndex()。explain()分析查询计划,识别低效索引。硬件与系统优化
查询优化
projection)和限制(limit)减少数据传输,如db.users.find({}, { name: 1, age: 1, _id: 0 }).limit(10)。skip()和limit()。