I am encountering performance issues with MongoDB running in a Docker container on a Debian 12 server, using the default ext4 filesystem. The server specifications include dual E5 2696v3 processors (72 CPUs), 320GB DDR4 2133MHz ECC DRAM, and a 16TB HDD configured in RAID5 with read/write speeds up to 600-700MB/s. I am using MongoDB Community Server version 8.0.
Here is the query I am working with (through pymongo):
comment_data = list(stock_database.find({‘ticker’: ticker}, {‘_id’: 0, ‘comment’:0}).sort(‘post_publish_time’, pymongo.ASCENDING))
This querys the comment data of a given stock and sort it by post_publish_time. I have created indexes for both post_publish_time and ticker. There are a total of 5368 different tickers. My purpose is to read the data into memory using Python and export them to csv. The first 5363 tickers were just fine. I was able to read data from MongoDB sucessfully. However, when it was down to the last 5 tickers, there was problem. The query suddenly became very very slow. What is strange is that the distribution of docs across tickers is sort of even. The last 5 tickers doesn’t come with significantly more documents than the previous ones.
I have tried to troubleshoot the problem in the following ways:
- The most important one of all: After processing the first 5363 tickers, I attempted to re-query tickers that had already been successfully exported to csv. It turned out that None of the tickers works fine any more. I thought the problem was with the last 5 tickers, but it is not. It seems that MongoDB is broken at some point (after data of the first 5363 tickers are retrieved and processed).
- I tried to export the query result from Compass
I don’t know much about how Compass exports data. But it seems that compass is handling documents in chunks of 1000. As the spikes in the network suggests, MongoDB doesn’t take much time to process the first several batches of documents. However, it is becoming more and more slow as the batches it has processed grow. Since my specific query matches 789140 documents in the database. It is likely that the query would take a long long time to complete, which appears to be stuck when working with pymongo. - I have examined my hardware and there is no failure. I was worring if the problem is related to disk IO. I tested IO speed via fio and everything is fine. Memory is also working fine according to pcm-memory.
What is the possible cause of this problem and how should I fix it?