這個問題讓我瘋狂。我有一個集合:MongoDB查詢比gte更快lte
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 })
它從3比與LTE相同的查詢6倍以上:
find({"$and":[ {"$or": [ {"from":"0x120a270bbc009644e35f0bb6ab13f95b8199c4ad"},
{"to":"0x120a270bbc009644e35f0bb6ab13f95b8199c4ad"}
]},
{"blockNumber":{"$lte":1289597}}
]
}).sort({ blockNumber: -1, transactionIndex: -1 })
你可以看到我試過很多索引組合,只是爲了測試我是否可以用frut force來解決這個問題,但我可能會錯過一些東西。出於這個原因,我即將放棄MongoDB。 快速查詢需要56毫秒和慢167毫秒的平均值。
有人能弄清楚有什麼問題或幫我找到它嗎?
它對這些指標的工作更糟糕。我認爲問題在於有些查詢返回大量文檔,排序需要時間。我真的不知道如何解釋可以幫助我,它看起來像彙編語言。如果結果是67000,那麼排序很慢。我曾經使用Cassandra,您可以指定表格的默認順序,當您需要以某種順序檢索結果時,該表格的默認順序非常有用。它看起來像蒙戈沒有這個功能,這將是我所需要的。 –