简体   繁体   English

如何在MongoDB中修复非常慢的查询

[英]How to fix Very Slow Query in mongodb

  • MongoDB shell version v4.0.5 MongoDB Shell版本v4.0.5

: Very Slow Query | :非常慢的查询 Taking time in fetching | 花时间在获取| Data Size : 2-3 Million documents in a collection and there are around 13-15 such collections. 数据大小:一个集合中有2-3百万个文档,大约有13-15个这样的集合。

  • System Configuration 系统配置

: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 6 On-line CPU(s) list: 0-5 Thread(s) per core: 1 Core(s) per socket: 6 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 79 Stepping: 1 CPU MHz: 2097.570 BogoMIPS: 4195.14 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 20480K NUMA node0 CPU(s): 0-5 :体系结构: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

MemTotal: 16432268 kB 内存总量:16432268 kB

I have done indexing on the tables and that is working fine but still, the system is taking a lot of time: 我已经完成了对表的索引编制,并且工作正常,但仍然需要很多时间:

Query: 查询:

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")

Below is the query planner: 下面是查询计划器:

{
    "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 data: 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

The query performance is limited by your resources. 查询性能受到资源的限制。 Here are a few tips 这里有一些提示

  1. check your resources by following the Operations Checklists . 按照“ 操作清单”检查您的资源。
  2. From the query plan, not only fetching docs was slow, examining indexes keys was also slow. 从查询计划来看,不仅获取文档很慢,而且检查索引键也很慢。 You need to Ensure Indexes Fit in RAM . 您需要确保索引适合RAM
  3. Applying additional random indexes does not help and you can cause high numbers of page faults. 应用其他随机索引无济于事,并且可能导致大量的页面错误。 You can use $indexStats to check your indexes usages. 您可以使用$ indexStats来检查索引的使用情况。
  4. Try MongoDB's Cloud Manager to help collect performance metrics. 尝试使用MongoDB的Cloud Manager来帮助收集性能指标。 It offers free trial. 它提供免费试用。
  5. Query-wise, project only the fields you need instead of fetching the entire document. 在查询方面,仅投影所需的字段,而不是获取整个文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM