次の構造で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()
クエリのforを見ると、が表示されてBtreeCursor userId_1
いるので、すべてのユーザーがuserId
ofuser1234
を取得し、スキャンして(2つのアイテムのみ)accountId
ofを見つけたとaccount1234
思います-これは正しいですか?
前もって感謝します。