Mongodb聚合内存不足

2024-10-05 10:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用mongodb聚合从一个大型集合中抽取文档样本。在

https://docs.mongodb.com/manual/reference/operator/aggregation/sample/

在连续打了几个电话之后,我看到mongodb的内存在不断攀升,大约在第12次调用之后,它崩溃,并出现OutOfMemory错误。在

在Mongodb处理完一个查询之后,我如何告诉它释放内存?在


Tags: sample文档httpscomdocsmongodb错误manual
3条回答

原来问题出在存储引擎缓存上。我使用的是一个EC2实例,它导致了OOM错误。我已经能够通过分配如下较小的缓存大小来解决此问题:

mongod  dbpath /a/path/to/db  logpath /a/path/to/log  storageEngine wiredTiger  wiredTigerEngineConfigString="cache_size=200M"  fork

你问这个问题的原因是你不知道^{}运算符是如何工作的。如documentation中所述

In order to get N random documents:

  • If N is greater than or equal to 5% of the total documents in the collection, $sample performs a collection scan, performs a sort, and then select the top N documents. As such, the $sample stage is subject to the sort memory restrictions.

  • If N is less than 5% of the total documents in the collection, If using WiredTiger Storage Engine, $sample uses a pseudo-random cursor over the collection to sample N documents. If using MMAPv1 Storage Engine, $sample uses the _id index to randomly select N documents.

所以我认为你想得到的随机文档数大于5%。您需要的是将allowDiskUse设置为True。在

collection.aggregate(pipeline, allowDiskUse=True)

您应该将allowDiskUse值设置为true。例如:

db.books.aggregate( [
           { $group : { _id : "$author", books: { $push: "$title" } } },
                      {allowDiskUse:true}
                  ] )

Pipeline stages have a limit of 100 megabytes of RAM. If a stage exceeds this limit, MongoDB will produce an error. To allow for the handling of large datasets, use the allowDiskUse option to enable aggregation pipeline stages to write data to temporary files.

你可以阅读更多关于这个here。在

相关问题 更多 >

    热门问题