rajeshpanwar
rajeshpanwar

Reputation: 1233

which indexes should we create in mongodb to improve performance?

I have a product_catalog collection and it contain the huge number of documents.

The documents are like (document structure):

{
  "_id": "573c1540c4cebf791315aa0a",
  "images": [],
  "categories": [
    "shirts",
    "wedding",
    "suits"
  ],
  "publish": "1",
  "inventory_management": "none",
  "options": [],
  "SEO": {
    "title": "dummy",
    "description": "dfasdfasd",
    "keywords": "dummy"
  },
  "attributes": [],
  "features": [],
  "files": [],
  "seller": "seller_one",
  "approve": "approved",
  "description": "<span style=\"color: rgb(57, 57, 57); font-family: 'Open Sans'; font-size: 14px; line-height: 21px; text-align: right;\">Description here.</span>",
  "name": "dummy",
  "alias": "dummy",
  "price": 500,
  "compare_price": 550,
  "collections": [
    "collection-2",
    "collection-3"
  ],
  "brand": "test_brand",
  "sku": "rtrtrt",
  "barcode": "12121",
  "weight": "12",
  "sort_order": 0,
  "available": 1,
  "uniquesku": [
    "rtrtrt"
  ]
}

And we are retrieving the documents with following query and sort option.

db.collection('product_catalog').find(query).sort(sort_options).toArray(function(err, records){

// records

}) 
sort_options : { sort_order: 1, _id: -1 }   
Query is like : 
    1. { publish: '1'}
     2. { publish: '1', alias: 'alias_value' }
     3. { publish: '1', approve: 'approve_value' }
     4. { categories: 'category_value', publish: '1' }
     5. { collections: 'collection_value', publish: '1' }

What indexes should we create to improve the performance of read operations ?

Upvotes: 0

Views: 45

Answers (1)

Nic Cottrell
Nic Cottrell

Reputation: 9695

In MongoDB it's recommended that indexes follow your application's use cases. Since you've already identified the top queries, you should just add an index for each:

 { alias:1, publish: 1}
 { approve:1, publish:1}
 { categories: 1, publish: 1 }
 { collections: 1, publish: 1 }
 { publish: 1 }

Note that I've put publish as the second field since the first will be more granular and those be more efficient.

Upvotes: 1

Related Questions