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

前端 未结 4 442
再見小時候
再見小時候 2020-11-30 04:19

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 qu

4条回答
  •  孤街浪徒
    2020-11-30 05:12

    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.

提交回复
热议问题