MongoDB查询优化或提高性能

时间:2019-12-14 13:06:40

标签: mongodb query-optimization performance-testing query-performance

我收集了大约5.5个文档缺失的产品,并且收集大小为3.3 GB,它存储着产品详细信息和属性的数据。所有属性都存储为具有数据类型数组的主键。现在,我希望选择一个按类别列出的唯一属性。

我已经按照下面的pr查询了。已经创建的活动索引。在第一阶段,将所有文档Brand数组合并为一个,然后从所有Brand数组值中获取唯一值。这个查询需要3146毫秒,这对我来说太长了。我想提高查询性能。

在文档中,所有键均以大写字母开头,后者是属性。我想要文档样本中列出的所有属性的唯一值。

我正在使用具有256 GB SSD,8 GB RAM的本地计算机。 我还尝试了mongoAtlas 4核,10 GB空间和16 GB RAM计算机。这也需要相同的时间。因此,我认为这不是硬件问题。

查询

db.products.aggregate([{
    "$match": {
        "active": "1"
    }
}, {
    "$group": {
        "_id": 0,
        "Brand": {
            "$addToSet": "$Brand"
        }
    }
}, {
    "$project": {
        "_id": 0,
        "Brand": {
            "$reduce": {
                "input": "$Brand",
                "initialValue": [
                    []
                ],
                "in": {
                    "$setUnion": ["$$value", "$$this"]
                }
            }
        }
    }
}])

第1阶段的输出

{
    "Brand": [
        ["Design Clinic"],
        ["Laitin Art"],
        ["Matrix International"],
        ["De Majo"],
        ["Verdure Wellness"],
        ["Maxmarko"],
        ["Golden"],
        ["Panseima"],
        ["Akdo"],
        ["Xiangda kitchenware"]
    ]
}

最终输出

{
    "Brand": ["12h", "5th Gallery", "A & X", "A Casa K", "A Lot Of Brasil", "A Lot of Brasil", "ACL", "ADS Lighting", "AL2", "Akdo"]
}

请解释一下以下数据

"executionStats" : {
                    "executionSuccess" : true,
                    "nReturned" : 525536,
                    "executionTimeMillis" : 3146,
                    "totalKeysExamined" : 525536,
                    "totalDocsExamined" : 525536,
                    "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 525536,
                        "executionTimeMillisEstimate" : 199,
                        "works" : 525537,
                        "advanced" : 525536,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 4143,
                        "restoreState" : 4143,
                        "isEOF" : 1,
                        "docsExamined" : 525536,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "nReturned" : 525536,
                            "executionTimeMillisEstimate" : 38,
                            "works" : 525537,
                            "advanced" : 525536,
                            "needTime" : 0,
                            "needYield" : 0,
                            "saveState" : 4143,
                            "restoreState" : 4143,
                            "isEOF" : 1,
                            "keyPattern" : {
                                "active" : 1
                            },
                            "indexName" : "active",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "active" : [ ]
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "active" : [
                                    "[\"1\", \"1\"]"
                                ]
                            },
                            "keysExamined" : 525536,
                            "seeks" : 1,
                            "dupsTested" : 0,
                            "dupsDropped" : 0
                        }
                    }
                }

我的文档设计如下。

{
    "_id": "5dedda7a4f015667657654a8",
    "id": "47442",
    "name": "LTZ 19030159 60 61",
    "slug": "ltz-19030159-60-61",
    "model_number": "ltz 19030159 60 61",
    "country_of_mfg": "China",
    "description": "<p>Different elements of colors, shadows, lines and textures, recur the Modern art on the gold foiled hand torn art paper which mounted with the double solid wood frame.</p>\r\n\r\n<ul>\r\n\t<li>Material: imported American poplar frame+ glass+gold foiled hand torn art paper + MDF backboard</li>\r\n\t<li>Customization Available for Frame Material, Finish &amp; Size</li>\r\n\t<li>Sizes\r\n\t<ul>\r\n\t\t<li>L - 600 mm x W - 800 mm</li>\r\n\t\t<li>L - 700 mm&nbsp;x W - 1000 mm</li>\r\n\t\t<li>L - 800 mm x W - 1200 mm</li>\r\n\t</ul>\r\n\t</li>\r\n</ul>",
    "brand_id": "553",
    "price": "0.00",
    "primary_image_url": "https://dt4f7ywfipgvt.cloudfront.net/products_images/47442/optimized/ltz_19030159_60_61-1.jpg",
    "keywords": ["Paintings", "Painting", "picture", "canvas", "sketch", "sketches", "portrait", "drawing", "oil paint", "oil painting", "paint", "paints", "color", "colors", "colour", "depiction", "art", "art work", "decor", "decoration", "decorations", "decorates", "decorative", "indoor", "inside", "in home", "inside home", "for home", "for room", "inside room", "home", "wooden frame painting", "morden art"],
    "new": "0",
    "active": "1",
    "featured": "0",
    "featured_big_flag": "0",
    "uom": "Each",
    "created_datetime": "2019-09-03 09:22:22",
    "category_ids": "14",
    "category_processed": "1",
    "update_date": "2019-09-05 16:35:01",
    "Country": ["China"],
    "Brand": ["Laitin Art"],
    "Frame Material": ["Wood"],
    "Artwork Surface": ["Paper"],
    "Subject": ["Abstract"],
    "Orientation": ["Portrait"],
    "Color": ["Gold", "Multicolor"],
    "brand_data": {
        "id": "553",
        "vendor_id": "520",
        "vendors_brands_id": "0",
        "brand_name": "Laitin Art",
        "active": "1",
        "hide_brand_name": "0",
        "featured": "0",
        "synced": "1",
        "priority": "1"
    },
    "categories_data": [{
        "id": "9337238",
        "top_cat_id": "4",
        "name": "Paintings",
        "breadcrumb": "Rugs & Decor",
        "breadcrumb_full": "Rugs & Decor> Paintings",
        "breadcrumb_html": "<li><a href='/product-list/rugs-decor/1'>Rugs & Decor</a> </li><li> <a href='/product-list/paintings/1'>Paintings</a></li>",
        "partial_breadcrumb_html": "<li><a href='/product-list/rugs-decor/1'>Rugs & Decor</a> </li><li> Paintings</li>",
        "slug": "paintings",
        "type": "category",
        "active": "1",
        "products_count": "1195",
        "category_id": "14",
        "product_id": "47442"
    }, {
        "id": "9337239",
        "name": "Rugs & Decor",
        "breadcrumb": "Rugs & Decor",
        "breadcrumb_full": "Decor> Decor",
        "breadcrumb_html": "<li> Rugs & Decor</li>",
        "partial_breadcrumb_html": "<li> Rugs & Decor</li>",
        "slug": "rugs-decor",
        "type": "category",
        "active": "1",
        "products_count": "8379",
        "sub_cat_ids": "8,10,11,13,14,15,839,842,844,845,985,986,1742,1795,1851,1861,1867,1883,1909,1913,1930,1938,1958,1959",
        "category_id": "4",
        "product_id": "47442"
    }],
    "Brand": ["Paintings", "Rugs & Decor"],
    "images": [{
        "id": "81874",
        "product_id": "47442",
        "image_url": "https://dt4f7ywfipgvt.cloudfront.net/products_images/47442/optimized/ltz_19030159_60_61-2.jpg",
        "type": "image",
        "sort_order": "1"
    }],
    "attributes": {
        "Country": {
            "values": ["China"],
            "value_display": ["China"],
            "active": "1",
            "filterable": "1"
        },
        "Brand": {
            "values": ["Laitin Art"],
            "value_display": ["Laitin Art"],
            "active": "1",
            "filterable": "1"
        },
        "Frame Material": {
            "values": ["Wood"],
            "value_display": ["Poplar Wood"],
            "active": "1",
            "filterable": "1"
        },
        "Artwork Surface": {
            "values": ["Paper"],
            "value_display": ["Art Paper"],
            "active": "1",
            "filterable": "1"
        },
        "Style": {
            "values": ["Modern"],
            "value_display": ["Modern"],
            "active": "1",
            "filterable": "1"
        },
        "Frame Type ": {
            "values": ["Framed"],
            "value_display": ["Framed"],
            "active": "1",
            "filterable": "1"
        },
        "Subject": {
            "values": ["Abstract"],
            "value_display": ["Abstract"],
            "active": "1",
            "filterable": "1"
        },
        "Orientation": {
            "values": ["Portrait"],
            "value_display": ["Portrait"],
            "active": "1",
            "filterable": "1"
        },
        "Color": {
            "values": ["Gold", "Multicolor"],
            "value_display": ["Gold", "Multicolor"],
            "active": "1",
            "filterable": "1"
        }
    }
}

0 个答案:

没有答案