MongoDB find query with compound index taking more than 300ms

64 views Asked by At

I have a MongoDB collection with 300,000 documents, and I'm experiencing slow query performance for a specific query. The document structure is as follows:

{
  "_id": 268621,
  "tenant_id": 1,
  "sub_tenant": "MAIN_SITE",
  "promotion_id": 42020,
  "property": "listing",
  "slot": 1001,
  "is_active": true,
  "priority": 0,
  "imageurl": "https://media12.ppl-media12.com/1687463343_schwarzkopf_professional_spa_essence-2596x836.jpeg",
  "m_imageurl": "https://media12.ppl-media12.com/1687463342_schwarzkopf_professional_spa_essence-1440x544.jpeg",
  "aspect_ratio": 3.10526,
  "m_aspect_ratio": 2.64706,
  "lp_url": "",
  "text": "",
  "tagged_pages": [
    1384569
  ],
  "segment": [],
  "mode_device": [
    "desktop",
    "mobile",
    "android",
    "iosapp"
  ],
  "group_id": 24,
  "start_date": 1687167000,
  "end_date": 1688149500,
  "last_updated_timestamp": 1687465021,
  "x_id": "listing",
  "createdAt": {
    "$date": "2023-06-22T20:17:01.831Z"
  },
  "updatedAt": {
    "$date": "2023-06-22T20:17:01.831Z"
  },
  "__v": 0
}

The compound index I'm using for the query is:

property_1_slot_1_group_id_1_is_active_-1_tagged_pages_1_tenant_id_1_sub_tenant_1_start_date_1_end_date_1

Here's an example of the query I'm running:

db.collection.find({
  "is_active": true,
  "property": "landing_pages_widget",
  "slot": 6010,
  "tagged_pages": 24005,
  "mode_device": "android",
  "group_id": 2,
  "start_date": { "$lte": 1688487854 },
  "end_date": { "$gte": 1688487854 },
  "tenant_id": 1,
  "sub_tenant": "MAIN_SITE"
})

The problem is that this query is sometimes taking more than 300ms to execute in case of high throughput, but when i execute single query in compass, response time was fine. I have used the explain() method in MongoDB compass to analyze the query, and it seems to be utilizing the compound index. Here are the results of the explain():

{
 "stage": "IXSCAN",
 "nReturned": 0,
 "executionTimeMillisEstimate": 0,
 "works": 8,
 "advanced": 0,
 "needTime": 7,
 "needYield": 0,
 "saveState": 0,
 "restoreState": 0,
 "isEOF": 1,
 "keyPattern": {
  "property": 1,
  "slot": 1,
  "group_id": 1,
  "is_active": -1,
  "tagged_pages": 1,
  "tenant_id": 1,
  "sub_tenant": 1,
  "start_date": 1,
  "end_date": 1
 },
 "indexName": "property_1_slot_1_group_id_1_is_active_-1_tagged_pages_1_tenant_id_1_sub_tenant_1_start_date_1_end_date_1",
 "isMultiKey": true,
 "multiKeyPaths": {
  "property": [],
  "slot": [],
  "group_id": [],
  "is_active": [],
  "tagged_pages": [
   "tagged_pages"
  ],
  "tenant_id": [],
  "sub_tenant": [],
  "start_date": [],
  "end_date": []
 },
 "isUnique": false,
 "isSparse": false,
 "isPartial": false,
 "indexVersion": 2,
 "direction": "forward",
 "indexBounds": {
  "property": [
   "[\"landing_pages_widget\", \"landing_pages_widget\"]"
  ],
  "slot": [
   "[6010, 6010]"
  ],
  "group_id": [
   "[2, 2]"
  ],
  "is_active": [
   "[true, true]"
  ],
  "tagged_pages": [
   "[24005, 24005]"
  ],
  "tenant_id": [
   "[1, 1]"
  ],
  "sub_tenant": [
   "[\"MAIN_SITE\", \"MAIN_SITE\"]"
  ],
  "start_date": [
   "[-inf.0, 1688487854]"
  ],
  "end_date": [
   "[1688487854, inf.0]"
  ]
 },
 "keysExamined": 8,
 "seeks": 8,
 "dupsTested": 0,
 "dupsDropped": 0
}

Can anyone help by identifying what went wrong here?

0

There are 0 answers