2016-05-18 100 views
0

我有一個product_catalog集合,它包含大量的文檔。我們應該用mongodb創建哪些索引來提高性能?

的文件像(文檔結構):

{ 
    "_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" 
    ] 
} 

我們正在與下面的查詢和排序選項檢索文件。

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' } 

我們應該創建什麼樣的索引來提高讀操作的性能?

+0

發佈的選項有哪些? 0和1?這些文件中有多少百分比是「已發佈:1」。如果它是99%,那麼一個索引就不會超級有用。 –

+0

發佈選項爲0和1.發佈80%的文檔(值:「1」)。我想創建索引,使所有操作變得快速。 – rajeshpanwar

+0

https://docs.mongodb.com/manual/core/index-intersection/#index-intersection-and-sort – rajeshpanwar

回答

1

在MongoDB中,建議索引遵循應用程序的用例。既然你已經確定的熱門查詢,你應該只添加索引每個:

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

請注意,我已經把出版作爲第二個字段,因爲要先將更加精細和那些更有效率。

+0

先生,排序選項是什麼?應該在sort_options(sort_order)上有索引嗎? – rajeshpanwar

+1

對於您要排序的任何用例,請在末尾添加sort_order字段,例如'{批准:1,發佈:1,排序次序:1}'。你是否想按照日期順序排序,你是否按sort_order和_id排序? –

+0

是的,先生,我按sort_order和_id排序 – rajeshpanwar

相關問題