mongodb.countDocuments is slow when result set is large even if index is used

后端 未结 1 1085
野的像风
野的像风 2021-02-11 09:59

mongodb.countDocuments is slow when result set is large

Test data on users collection:

  • 10M documents with status \'active\'
1条回答
  •  死守一世寂寞
    2021-02-11 10:35

    Counting seems like one of those things that should be cheap, but often isn't. Because mongo doesn't maintain a count of the number of documents that match certain criteria in its b-tree index, it needs to scan through the index counting documents as it goes. That means that counting 100x the documents will take 100x the time, and this is roughly what we see here -- 0.018 * 100 = 1.8s.

    To speed this up, you have a few options:

    1. The active count is roughly estimatedDocumentCount() - db.users.countDocuments({status: 'inactive'}). Would this be accurate enough for your use case?
    2. Alternatively, you can maintain a counts document in a separate collection that you keep in sync with the number of active/inactive documents that you have.

    0 讨论(0)
提交回复
热议问题