Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB 'count()' is very slow. How do we refine/work around with it?

I am currently using MongoDB with millions of data records. I discovered one thing that's pretty annoying.

When I use 'count()' function with a small number of queried data collection, it's very fast. However, when the queried data collection contains thousand or even millions of data records, the entire system becomes very slow.

I made sure that I have indexed the required fields.

Has anybody encountered an identical thing? How do you do to improve that?

like image 609
Winston Chen Avatar asked Oct 05 '11 07:10

Winston Chen


People also ask

Is MongoDB count slow?

Count queries, indexed or otherwise, are slow due to the fact that MongoDB still has to do a full b-tree walk to find the appropriate number of documents that match your criteria.

Is count faster than find MongoDB?

find({}). count() more fast then collection.

What is slow query in MongoDB?

One can identify slow queries in MongoDB by enabling the profiler and configuring it to its some specifications or executing db. currentOp() on a running mongod instance. By looking at the time parameters on the returned result, we can identify which queries are lagging.


1 Answers

There is now another optimization than create proper index.

db.users.ensureIndex({name:1}); db.users.find({name:"Andrei"}).count(); 

If you need some counters i suggest to precalculate them whenever it possible. By using atomic $inc operation and not use count({}) at all.

But mongodb guys working hard on mongodb, so, count({}) improvements they are planning in mongodb 2.1 according to jira bug.

like image 147
Andrew Orsich Avatar answered Sep 28 '22 09:09

Andrew Orsich