【发布时间】:2016-11-17 17:53:02
【问题描述】:
这个问题快把我逼疯了。我有一个收藏:
var ethTransactionSchema = new mongoose.Schema({
blockNumber: Number,
blockHash: String,
hash: String,
transactionIndex: Number,
from: String,
to: String,
value: String
});
ethTransactionSchema.index({ hash: 1 }, { unique: true });
ethTransactionSchema.index({ from: 1 });
ethTransactionSchema.index({ to: 1 });
ethTransactionSchema.index({ blockNumber: 1, transactionIndex: 1 });
ethTransactionSchema.index({ from: 1, to: 1, blockNumber: 1, transactionIndex: 1 });
ethTransactionSchema.index({ from: 1, blockNumber: 1, transactionIndex: 1});
ethTransactionSchema.index({ to: 1, blockNumber: 1, transactionIndex: 1 });
ethTransactionSchema.index({ to: 1, blockNumber: 1 });
ethTransactionSchema.index({ from: 1, blockNumber: 1 });
ethTransactionSchema.index({ from: 1, to: 1, blockNumber: 1 });
ethTransactionSchema.index({ blockNumber: 1 });
ethTransactionSchema.index({ transactionIndex: 1 });
ethTransactionSchema.index({ blockNumber: -1 });
ethTransactionSchema.index({ to: 1, blockNumber: -1 });
ethTransactionSchema.index({ from: 1, blockNumber: -1 });
ethTransactionSchema.index({ from: 1, to: 1, blockNumber: -1 });
ethTransactionSchema.index({ from: 1, to: 1, blockNumber: -1, transactionIndex: -1 });
ethTransactionSchema.index({ from: 1, blockNumber: -1, transactionIndex: -1 });
ethTransactionSchema.index({ to: 1, blockNumber: -1, transactionIndex: -1 });
当我执行这个查询时:
find({"$and":[ {"$or": [ {"from":"0x120a270bbc009644e35f0bb6ab13f95b8199c4ad"},
{"to":"0x120a270bbc009644e35f0bb6ab13f95b8199c4ad"}
]},
{"blockNumber":{"$gte":1289597}}
]
}).sort({ blockNumber: -1, transactionIndex: -1 })
它比使用 lte 的相同查询多 3 到 6 倍:
find({"$and":[ {"$or": [ {"from":"0x120a270bbc009644e35f0bb6ab13f95b8199c4ad"},
{"to":"0x120a270bbc009644e35f0bb6ab13f95b8199c4ad"}
]},
{"blockNumber":{"$lte":1289597}}
]
}).sort({ blockNumber: -1, transactionIndex: -1 })
您可以看到我已经尝试了很多索引组合,只是为了测试我是否可以使用强力解决问题,但我可能遗漏了一些东西。出于这个原因,我即将放弃 MongoDB。 快速查询平均需要 56 毫秒,慢速查询平均需要 167 毫秒。
谁能找出问题所在或帮我找到它?
【问题讨论】:
标签: mongodb performance mongoose mongodb-query