-2

I am using "MongoDB v4.2.x". My server memory is just 4GB, and MongoDB is utilizing more than 60%. I am running simple queries, not even aggregations, and the response time is too slow.

The question: How to reduce memory consumption and improve response time when querying a MongoDB database?

Ideas up to now:

  1. Is there a memory limitation option in MongoDB so that the parts of the loaded database that are not used may be outsourced to the disk?

  2. Changing "wiredTiger" cache size up to 1GB, but response time stays very slow. Are there any other MongoDB tweaks?

  3. Is there a workaround in Python instead of tweaking MongoDB itself?

questionto42
  • 7,175
  • 4
  • 57
  • 90
Rakesh Singh
  • 102
  • 1
  • 14
  • 1
    To examine why query response is slow, use [explain](https://docs.mongodb.com/manual/reference/method/db.collection.explain/index.html#db-collection-explain). [serverStatus](https://docs.mongodb.com/manual/reference/command/serverStatus/#serverstatus) should contain some details about what the memory is used for. – Joe Aug 17 '20 at 15:50
  • 1
    Please provide more information about your case. Do you use indexes or not? How many documents in your DB and each collection. What about Mongo Compass? Do you familiar with this software? – AlexZeDim Aug 21 '20 at 06:11
  • 1
    There is no silver bullet, there is no secret `optimizeMemory` flag that will make your queries blazing fast. You need to analyze your usage and how that affects server resources. – CodeCaster Aug 21 '20 at 06:12
  • @AlexZeDim My APP is the combination of instagram + airbnb. Yes, I have indexes all necessary fields. In my db has 80 collection and each collection has 10000 records. No, I am using mongobooster. I don't have idea about Mongo Compass. – Rakesh Singh Aug 21 '20 at 08:56
  • @CodeCaster How can I analyze the usage of queries? – Rakesh Singh Aug 21 '20 at 08:59

1 Answers1

1

If you just want to improve response time and reduce the memory consumed by MongoDB, a workaround is to load the MongoDB data into a pandas DataFrame, two options as follows.

  1. PyMongo's bson module: If it is really just a problem of connecting to MongoDB, you can export the database (or at best the exact part of it that you really need) as a bson file and then read the whole bson file into one pandas DataFrame using pymongo's bson.decode_all(). See Read BSON file in Python? for details.

  2. MongoDB collection: Or if you may have MongoDB open at least at the start, you can load data from a MongoDB collection into pandas DataFrame, see How can I load data from MongoDB collection into pandas' DataFrame?. After loading, close MongoDB to free the memory that the application consumes.

The extra time for loading the database at the start is a one-off cost. Once you have the whole database in one dataframe, you can use Python to query that in-memory DataFrame.

How to reduce memory consumption and response time in Python, among other things:

And when you have a database in a DataFrame, you also want to consider:

And you might want to change between wide and long format:

questionto42
  • 7,175
  • 4
  • 57
  • 90