2014년 12월 14일 일요일

MongoDB Count() Takes more Time

I am inserted 10 million data on my collection, then i am applied the count it takes more than 1 minuet to get the result



What count are you trying? Can you paste in the exact command? Can you run the corresponding find with .explain() and show us the output? It's likely you don't have a proper index.



db.clnRoleMaster.find({roleName:{$ne:"Admin"}, companyId:10, roleName:{$regex:roleVal, $options:"$i"}}, {_id:1, roleName:1}).count();

i done like this, i am had use proper indexing on companyId and roleName



Can you include the explain output? The $ne condition and $regex condition won't use the index very efficiently, so if this count returns a lot of documents it's expected that the query will involve a lot of work as it will require examining a lot of index entries and documents to determine matches.



What is the use of explain? 



Pardon me, should've linked to .explain() before. Just replace the count with explain:

db.clnRoleMaster.find({roleName:{$ne:"Admin"}, companyId:10, roleName:{$regex:roleVal, $options:"$i"}}, {_id:1, roleName:1}).explain()



You are passing invalid JSON to the query so it's not actually querying for what you think.

{roleName:{$ne:"Admin"}, 
 companyId:10, 
 roleName:{$regex:roleVal, $options:"$i"}
}

As roleName key is appearing twice in the JSON document, only one of its values is preserved (the second one) so the "not equal "Admin" is not being used at all in the query.

In addition, as Will said, case insensitive regex is not an efficient way of querying any field, indexed or not.

So if you have an index on companyId or companyId, roleName, then the query will only be fast if companyId is very selective - what percentage of the documents have companyId 10?


댓글 없음:

댓글 쓰기