MongoDB: does document size affect query performance?

后端 未结 4 1838
不思量自难忘°
不思量自难忘° 2021-01-31 16:31

Assume a mobile game that is backed by a MongoDB database containing a User collection with several million documents.

Now assume several dozen properties t

4条回答
  •  庸人自扰
    2021-01-31 17:16

    Just wanted to share my experience when dealing with large documents in MongoDB... don't do it!

    We made the mistake of allowing users to include files encoded in base64 (normally images and screenshots) in documents. We ended up with a collection of ~500k documents ranging from 2 Mb to 10 Mb each.

    Doing a simple aggregate in this collection would bring down the cluster!

    Aggregate queries can be very heavy in MongoDB, especially with large documents like these. Indexes in aggregates can only be used in some conditions and since we needed to $group, indexes were not being used and MongoDB would have to scan all the documents.

    The exact same query in a collection with smaller sized documents was very fast to execute and the resource consumption was not very high.

    Hence, querying in MongoDB with large documents can have a big impact in performance, especially aggregates.

    Also, if you know that the document will continue to grow after it is created (e.g. like including log events in a given entity (document)) consider creating a collection for these child items because the size can also become a problem in the future.

    Bruno.

提交回复
热议问题