Tuesday 16 May 2017

Mongo find query takes 2 minutes

I have around 75,000 documents in a collection.

The total size of database is around 45GB.
Of the 75k docs, around 45k are of 900 KB each (around 42 GB) and the remaining docs around 120 KB each.

Each document is mapped to a custId ObjectId in other collection, and has a timestamp, both indexed.

Now I need to fetch the docs for a particular custId for last month. The count is around 5500 docs. This custId has the small docs with size around 120 KB each.

Following is my query:

db.mycollection.find(
{
    custId:ObjectId("CUST_OBJECT_ID_HERE"),
    timestamp:{$gte:one_month_ago_date, $lt:current_date}
}).sort({timestamp:-1})

Still the query takes 2 minutes to fetch all records. Is it because of the number of documents or the size of the larger documents? Is there a way I can fix this?

NOTE: It takes 2 minutes when the query is fired from nodejs. If I fire it on mongo shell, it returns quickly but it might be because its just fetching first 50 records. When I attached .count() to the query on mongo shell, it took 2 minutes to return with the count.



via Dushyant Bangal

No comments:

Post a Comment