簡體   English   中英

如何在MongoDB中修復非常慢的查詢

[英]How to fix Very Slow Query in mongodb

  • MongoDB Shell版本v4.0.5

:非常慢的查詢 花時間在獲取| 數據大小:一個集合中有2-3百萬個文檔,大約有13-15個這樣的集合。

  • 系統配置

:體系結構:x86_64 CPU操作模式:32位,64位字節順序:Little Endian CPU:6個在線CPU列表:0-5每個內核的線程:1每個插槽的核心數:6插槽:1 NUMA節點:1供應商ID:正版英特爾CPU家族:6型號:79步進:1 CPU MHz:2097.570 BogoMIPS:4195.14系統管理程序供應商:VMware虛擬化類型:完整L1d緩存:32K L1i緩存:32K L2緩存:256K L3緩存:20480K NUMA node0 CPU:0-5

內存總量:16432268 kB

我已經完成了對表的索引編制,並且工作正常,但仍然需要很多時間:

查詢:

db.vnms_vccells_5.find({auid:"<1e000097>",
        hub_ip:"10.252.0.105",sector_ip:"<1e000046>", 
        last_updated_time:{$gt:"2016-12-24 05:49:00"}})
    .explain("executionStats")

下面是查詢計划器:

{
    "queryPlanner": {
        "plannerVersion": 1,
        "namespace": "opennms.vnms_vccells_5",
        "indexFilterSet": false,
        "parsedQuery": {
            "$and": [{
                "auid": {
                    "$eq": "<1e000097>"
                }
            }, {
                "hub_ip": {
                    "$eq": "10.252.0.105"
                }
            }, {
                "sector_ip": {
                    "$eq": "<1e000046>"
                }
            }, {
                "last_updated_time": {
                    "$gt": "2016-12-24 05:49:00"
                }
            }]
        },
        "winningPlan": {
            "stage": "FETCH",
            "inputStage": {
                "stage": "IXSCAN",
                "keyPattern": {
                    "auid": 1,
                    "hub_ip": 1,
                    "sector_ip": 1,
                    "last_updated_time": -1
                },
                "indexName": "auid_1_hub_ip_1_sector_ip_1_last_updated_time_-1",
                "isMultiKey": false,
                "multiKeyPaths": {
                    "auid": [],
                    "hub_ip": [],
                    "sector_ip": [],
                    "last_updated_time": []
                },
                "isUnique": false,
                "isSparse": false,
                "isPartial": false,
                "indexVersion": 2,
                "direction": "forward",
                "indexBounds": {
                    "auid": ["[\"<1e000097>\", \"<1e000097>\"]"],
                    "hub_ip": ["[\"10.252.0.105\", \"10.252.0.105\"]"],
                    "sector_ip": ["[\"<1e000046>\", \"<1e000046>\"]"],
                    "last_updated_time": ["({}, \"2016-12-24 05:49:00\")"]
                }
            }
        },
        "rejectedPlans": []
    },
    "executionStats": {
        "executionSuccess": true,
        "nReturned": 28788,
        "executionTimeMillis": 34989,
        "totalKeysExamined": 28788,
        "totalDocsExamined": 28788,
        "executionStages": {
            "stage": "FETCH",
            "nReturned": 28788,
            "executionTimeMillisEstimate": 34475,
            "works": 28789,
            "advanced": 28788,
            "needTime": 0,
            "needYield": 0,
            "saveState": 1039,
            "restoreState": 1039,
            "isEOF": 1,
            "invalidates": 0,
            "docsExamined": 28788,
            "alreadyHasObj": 0,
            "inputStage": {
                "stage": "IXSCAN",
                "nReturned": 28788,
                "executionTimeMillisEstimate": 7730,
                "works": 28789,
                "advanced": 28788,
                "needTime": 0,
                "needYield": 0,
                "saveState": 1039,
                "restoreState": 1039,
                "isEOF": 1,
                "invalidates": 0,
                "keyPattern": {
                    "auid": 1,
                    "hub_ip": 1,
                    "sector_ip": 1,
                    "last_updated_time": -1
                },
                "indexName": "auid_1_hub_ip_1_sector_ip_1_last_updated_time_-1",
                "isMultiKey": false,
                "multiKeyPaths": {
                    "auid": [],
                    "hub_ip": [],
                    "sector_ip": [],
                    "last_updated_time": []
                },
                "isUnique": false,
                "isSparse": false,
                "isPartial": false,
                "indexVersion": 2,
                "direction": "forward",
                "indexBounds": {
                    "auid": ["[\"<1e000097>\", \"<1e000097>\"]"],
                    "hub_ip": ["[\"10.252.0.105\", \"10.252.0.105\"]"],
                    "sector_ip": ["[\"<1e000046>\", \"<1e000046>\"]"],
                    "last_updated_time": ["({}, \"2016-12-24 05:49:00\")"]
                },
                "keysExamined": 28788,
                "seeks": 1,
                "dupsTested": 0,
                "dupsDropped": 0,
                "seenInvalidated": 0
            }
        }
    },
    "serverInfo": {
        "host": "vnms",
        "port": 27017,
        "version": "4.0.5",
        "gitVersion": "3739429dd92b92d1b0ab120911a23d50bf03c412"
    },
    "ok": 1
}

mongostat數據:

insert query update delete getmore command dirty  used flushes vsize   res qrw arw net_in net_out conn                time
    *0    *0     *0     *0       0     3|0  0.0% 80.0%       0 5.26G 3.52G 0|0 3|0   254b    101k    7 Jan 23 10:20:41.082

查詢性能受到資源的限制。 這里有一些提示

  1. 按照“ 操作清單”檢查您的資源。
  2. 從查詢計划來看,不僅獲取文檔很慢,而且檢查索引鍵也很慢。 您需要確保索引適合RAM
  3. 應用其他隨機索引無濟於事,並且可能導致大量的頁面錯誤。 您可以使用$ indexStats來檢查索引的使用情況。
  4. 嘗試使用MongoDB的Cloud Manager來幫助收集性能指標。 它提供免費試用。
  5. 在查詢方面,僅投影所需的字段,而不是獲取整個文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM