以下是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()查看索引使用情況。配置與維護
db.collection.reIndex()。explain()分析查詢計劃,識別低效索引。硬件與系統優化
查詢優化
projection)和限制(limit)減少數據傳輸,如db.users.find({}, { name: 1, age: 1, _id: 0 }).limit(10)。skip()和limit()。