this is my document i want to query:
{
"_id":ObjectId("5062d30522dfae0e11000000"),
"id_resource" : "147",
"moment_created" : ISODate("2012-03-22T16:29:21Z"),
"moment_updated" : ISODate("2012-03-22T16:29:21Z"),
"users_involved" : [
{
"id_user" : "113928869",
"state" : "answered",
"id_folder" : "0",
"is_deleted" : "0"
},
{
"id_user" : "121624627",
"state" : "new",
"id_folder" : "0",
"is_deleted" : "0" }
],
"posts" : [
{
"id_author" : "113928869",
"post" : "hiohhio",
"moment_created" : ISODate("2012-03-22T16:29:21Z")
}
]
}
and this is how i tried to ensure my index:
db.message.ensureIndex({id_resource:1, users_involved : 1});
and this is the query i used to query my collection :
db.message.find({id_resource : "143", "users_involved" : {$elemMatch : {id_user : "101226353", state : "answered"}}});
but one explain later i get this output:
{
"clusteredType" : "ParallelSort",
"cursor" : "BasicCursor",
"n" : 11,
"nChunkSkips" : 0,
"nYields" : 8624,
"nscanned" : 1461277,
"nscannedAllPlans" : 1461277,
"nscannedObjects" : 1461277,
"nscannedObjectsAllPlans" : 1461277,
"millisShardTotal" : 1878,
"millisShardAvg" : 939,
"numQueries" : 2,
"numShards" : 2,
"millis" : 1646
}
getIndexes will return:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "messaging.message",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"id_resource" : 1,
"users_involved" : 1
},
"ns" : "messaging.message",
"name" : "id_resource_1_users_involved_1"
}
]
sadly i do not understand why my query is not using the index id_resource_1_users_involved_1. Can anyone explain to me why my index is not used or how i have to build my index to support the query i want to use?
thx for time and help
UPDATE
shame on me, a typo on my side. So here is the actual explain of the query
{
"clusteredType" : "ParallelSort",
"cursor" : "BtreeCursor id_resource_1_users_involved_1",
"n" : 5,
"nChunkSkips" : 0,
"nYields" : 2,
"nscanned" : 46868,
"nscannedAllPlans" : 93736,
"nscannedObjects" : 46868,
"nscannedObjectsAllPlans" : 93736,
"millisShardTotal" : 281,
"millisShardAvg" : 140,
"numQueries" : 2,
"numShards" : 2,
"millis" : 220
}
so the query IS using my index but it is still slow , also nscanned is pretty big, so not the whole index is used? i will have to check if nscanned matches the amount of messages for resource x
using the compound index from JohnnyHK it got much much faster:
ensureIndex({id_resource:1, 'users_involved.id_user':1, 'users_involved.state':1});
explain
{
"clusteredType" : "ParallelSort",
"cursor" : "BtreeCursor id_resource_1_users_involved.id_user_1_users_involved.state_1",
"n" : 5,
"nChunkSkips" : 0,
"nYields" : 0,
"nscanned" : 7,
"nscannedAllPlans" : 7,
"nscannedObjects" : 7,
"nscannedObjectsAllPlans" : 7,
"millisShardTotal" : 0,
"millisShardAvg" : 0,
"numQueries" : 2,
"numShards" : 2,
"millis" : 1
}
so if i want to query the users_involved array i have to build a seperate index for every query?
also @JohnnyHK using the whole array as mentioned like:
find({id_resource : "197", "users_involved" : {$elemMatch : {id_user : "128825371", state : "answered", id_folder:"0", is_deleted:"0"}}}).hint("id_resource_1_users_involved_1")
did not improve anything, explain:
{
"clusteredType" : "ParallelSort",
"cursor" : "BtreeCursor id_resource_1_users_involved_1",
"n" : 5,
"nChunkSkips" : 0,
"nYields" : 1,
"nscanned" : 46868,
"nscannedAllPlans" : 46868,
"nscannedObjects" : 46868,
"nscannedObjectsAllPlans" : 46868,
"millisShardTotal" : 222,
"millisShardAvg" : 111,
"numQueries" : 2,
"numShards" : 2,
"millis" : 174
}
or maybe i am still doing it wrong?
*also i removed the shard information from the explain response, if this information may be important just say so
Because your compound index includes the whole
users_involvedarray, the index can only be used when matching complete embedded document elements of the array. See here.I think you’d be better served using a compound index that includes just the fields from
users_involvedthat you intend to search on. So either:OR