Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-57518

16% performance loss switching from Count to CountDocuments

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.5, 4.0.25
    • Component/s: None
    • Query Optimization
    • ALL
    • Hide

      Run in shell (Robo 3T 1.2.1 doesnt know countDocuments, so we wrote it out fully)

      function stopwatch(func) {
          var iters = 10;
          var start = new Date();
          for(var i=0; i<iters; i++) {
              func();
          }
          var end = new Date();
          print((end-start)/iters + " ms");
      }
      stopwatch(() => db.largeCollection.count({Om:0}));
      stopwatch(() => db.largeCollection.aggregate([{$match: {Om:0}}, {$group: {_id: null, count: {$sum:1}}}]))
      
      Show
      Run in shell (Robo 3T 1.2.1 doesnt know countDocuments, so we wrote it out fully) function stopwatch(func) { var iters = 10; var start = new Date(); for ( var i=0; i<iters; i++) { func(); } var end = new Date(); print((end-start)/iters + " ms" ); } stopwatch(() => db.largeCollection.count({Om:0})); stopwatch(() => db.largeCollection.aggregate([{$match: {Om:0}}, {$group: {_id: null , count: {$sum:1}}}]))
    • QO 2022-04-04, QO 2022-04-18, QO 2022-05-02, QO 2022-05-16, QO 2022-05-30, QO 2022-06-13, QO 2022-06-27, QO 2022-07-11, QO 2022-07-25, QO 2022-08-08, QO 2022-08-22, QO 2022-09-05, QO 2022-09-19, QO 2022-10-03, QE 2022-10-17

      Dear Server team,

      I'm a C# dev on a team using MongoDb and I have the task to clean up the compile warnings. One warning was "Count is obsolete. Use CountDocuments instead"

      But at every occurrence in the code,  at all the lines have the comment "Don't use CountDocuments, it is slower", referencing Jira NODE-1638.

      I did my own tests and indeed found CountDocuments to be about 34% slower then Count (~150 ms longer). That is quite significant and thus a problem for us, as we are already having performance issues.

      First thinking this was a C# driver issue, I opened a ticket CSHARP-3680 and had some interaction there. However, we found out that the issue already occurs on the server, thus should get a new SERVER ticket.

      Please read CSHARP-3680 for all the details and let me know what more information you need.

      Thank you.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            jochem@netwinst.nl Jochem Bonarius
            Votes:
            4 Vote for this issue
            Watchers:
            30 Start watching this issue

              Created:
              Updated: