【发布时间】:2012-08-14 08:41:52
【问题描述】:
假设我在 MongoDB 中存储了具有以下结构的对象:
Transaction
{
_id
userId
accountId
}
并假设我有以下索引:
db.Transaction.ensureIndex({"userId": 1})
以下查询是否利用索引来最小化搜索时间?
db.Transaction.find( {userId: 'user1234', accountId: 'account1234'} );
也就是说,MongoDB 是否使用索引来“缩减”userId 的结果,然后对accountId 进行表扫描?
db.Transaction.find( {userId: 'user1234', accountId: 'account1234'} ).explain()
{
"cursor" : "BtreeCursor userId_1",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 1,
"millis" : 1,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"userId" : [
[
"user1234",
"user1234"
]
]
}
查看查询的explain() 表示BtreeCursor userId_1,所以我认为它得到了user1234 的userId 的所有用户,然后扫描(仅有的两项)以找到@ 的accountId 987654332@ - 这是正确的吗?
提前致谢。
【问题讨论】: