Monogdb查询分析

一、输出查询过程:

db.getCollection('multobj').find({}).explain("executionStats")

二、对queryPlanner分析

2.1 queryPlanner: queryPlanner的返回
    queryPlanner.namespace:该值返回的是该query所查询的表

    queryPlanner.indexFilterSet:针对该query是否有indexfilter

    queryPlanner.winningPlan:查询优化器针对该query所返回的最优执行计划的详细内容。

    queryPlanner.winningPlan.stage:最优执行计划的stage,这里返回是FETCH,可以理解为
    通过返回的index位置去检索具体的文档(stage有数个模式,将在后文中进行详解)。

    queryPlanner.winningPlan.inputStage:用来描述子stage,并且为其父stage提供文档和索引关键字。

    queryPlanner.winningPlan.stage的child stage,此处是IXSCAN,表示进行的是index scanning。

    queryPlanner.winningPlan.keyPattern:所扫描的index内容,此处是did:1,status:1,modify_time: -1与scid : 1

    queryPlanner.winningPlan.indexName:winning plan所选用的index。

    queryPlanner.winningPlan.isMultiKey是否是Multikey,此处返回是false,如果索引建立在array上,此处将是true。

    queryPlanner.winningPlan.direction:此query的查询顺序,此处是forward,如果用了.sort({modify_time:-1})
    将显示backward。

    queryPlanner.winningPlan.indexBounds:winningplan所扫描的索引范围,如果没有制定范围就是[MaxKey, MinKey],
    这主要是直接定位到mongodb的chunck中去查找数据,加快数据读取。

    queryPlanner.rejectedPlans:其他执行计划(非最优而被查询优化器reject的)的详细返回,其中具体信息与
    winningPlan的返回中意义相同,故不在此赘述。
    
2.2 对executionStats返回逐层分析
    第一层,executionTimeMillis

    最为直观explain返回值是executionTimeMillis值,指的是我们这条语句的执行时间,这个值当然是希望越少越好。

    其中有3个executionTimeMillis,分别是:

    executionStats.executionTimeMillis

    该query的整体查询时间。

    executionStats.executionStages.executionTimeMillisEstimate

    该查询根据index去检索document获得2001条数据的时间。

    executionStats.executionStages.inputStage.executionTimeMillisEstimate

    该查询扫描2001行index所用时间。

    第二层,index与document扫描数与查询返回条目数

    这个主要讨论3个返回项,nReturned、totalKeysExamined、totalDocsExamined,分别代表该条查询返回的条目、
    索引扫描条目、文档扫描条目。

    这些都是直观地影响到executionTimeMillis,我们需要扫描的越少速度越快。

    对于一个查询,我们最理想的状态是:

    nReturned=totalKeysExamined=totalDocsExamined

    第三层,stage状态分析

    那么又是什么影响到了totalKeysExamined和totalDocsExamined?是stage的类型。类型列举如下:

    COLLSCAN:全表扫描

    IXSCAN:索引扫描

    FETCH:根据索引去检索指定document

    SHARD_MERGE:将各个分片返回数据进行merge

    SORT:表明在内存中进行了排序

    LIMIT:使用limit限制返回数

    SKIP:使用skip进行跳过

    IDHACK:针对_id进行查询

    SHARDING_FILTER:通过mongos对分片数据进行查询

    COUNT:利用db.coll.explain().count()之类进行count运算

    COUNTSCAN:count不使用Index进行count时的stage返回

    COUNT_SCAN:count使用了Index进行count时的stage返回

    SUBPLA:未使用到索引的$or查询的stage返回

    TEXT:使用全文索引进行查询时候的stage返回

    PROJECTION:限定返回字段时候stage的返回

    对于普通查询,我希望看到stage的组合(查询的时候尽可能用上索引):

    Fetch+IDHACK

    Fetch+ixscan

    Limit+(Fetch+ixscan)

    PROJECTION+ixscan

    SHARDING_FITER+ixscan

    COUNT_SCAN

    不希望看到包含如下的stage:

    COLLSCAN(全表扫描),SORT(使用sort但是无index),不合理的SKIP,SUBPLA(未用到index的$or),
    COUNTSCAN(不使用index进行count)
转载地址:https://www.cnblogs.com/c-abc/p/6023824.html

三、我的查询分析文本

{
    "queryPlanner" : {
        "plannerVersion" : 1,    #版本
        "namespace" : "ifaas_data.multobj",  #库名称,表名称
        "indexFilterSet" : false,  #针对该query是否有indexfilter
        "parsedQuery" : {     # 解析后的查询条件
            "$and" : [ 
                {
                    "$or" : [ 
                        {
                            "targetType" : {
                                "$eq" : "face"
                            }
                        }, 
                        {
                            "targetType" : {
                                "$eq" : "body"
                            }
                        }
                    ]
                }, 
                {
                    "sourceId" : {
                        "$eq" : "130"
                    }
                }, 
                {
                    "sourceType" : {
                        "$eq" : "zipFile"
                    }
                }, 
                {
                    "time" : {
                        "$lte" : ISODate("2018-12-29T03:56:00.000Z")
                    }
                }, 
                {
                    "time" : {
                        "$gte" : ISODate("2018-12-26T00:00:00.000Z")
                    }
                }
            ]
        },
        "winningPlan" : {   #查询优化器针对该query所返回的最优执行计划的详细内容。
            "stage" : "SKIP",  #最优执行计划的stage,这里返回是FETCH,可以理解为通过返回的index位置去检索具体的文档(stage有数个模式,将在后文中进行详解)。
            "skipAmount" : 0,
            "inputStage" : {   #用来描述子stage,并且为其父stage提供文档和索引关键字。
                "stage" : "SORT",
                "sortPattern" : {
                    "time" : -1.0
                },
                "limitAmount" : 200,
                "inputStage" : {  #用来描述子stage,并且为其父stage提供文档和索引关键字。
                    "stage" : "SORT_KEY_GENERATOR",
                    "inputStage" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "$and" : [ 
                                {
                                    "$or" : [ 
                                        {
                                            "targetType" : {
                                                "$eq" : "face"
                                            }
                                        }, 
                                        {
                                            "targetType" : {
                                                "$eq" : "body"
                                            }
                                        }
                                    ]
                                }, 
                                {
                                    "sourceType" : {
                                        "$eq" : "zipFile"
                                    }
                                }, 
                                {
                                    "time" : {
                                        "$lte" : ISODate("2018-12-29T03:56:00.000Z")
                                    }
                                }, 
                                {
                                    "time" : {
                                        "$gte" : ISODate("2018-12-26T00:00:00.000Z")
                                    }
                                }
                            ]
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "keyPattern" : {  #所扫描的index内容, 
                                "sourceId" : 1.0
                            },
                            "indexName" : "sourceId_1",  #winning plan所选用的index。
                            "isMultiKey" : false,  #是否是Multikey,此处返回是false,如果索引建立在array上,此处将是true。
                            "multiKeyPaths" : {
                                "sourceId" : []
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward", #此query的查询顺序,此处是forward,如果用了.sort({modify_time:-1})将显示backward。
                            "indexBounds" : {    #所扫描的索引范围,如果没有制定范围就是[MaxKey, MinKey],这主要是直接定位到mongodb的chunck中去查找数据,加快数据读取
                                "sourceId" : [ 
                                    "[\"130\", \"130\"]"
                                ]
                            }
                        }
                    }
                }
            }
        },
        "rejectedPlans" : [   #其他执行计划(非最优而被查询优化器reject的)的详细返回,其中具体信息与winningPlan的返回中意义相同,故不在此赘述
            {
                "stage" : "SKIP",
                "skipAmount" : 100,
                "inputStage" : {
                    "stage" : "SORT",
                    "sortPattern" : {
                        "time" : -1.0
                    },
                    "limitAmount" : 200,
                    "inputStage" : {
                        "stage" : "SORT_KEY_GENERATOR",
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$and" : [ 
                                    {
                                        "sourceId" : {
                                            "$eq" : "130"
                                        }
                                    }, 
                                    {
                                        "sourceType" : {
                                            "$eq" : "zipFile"
                                        }
                                    }, 
                                    {
                                        "time" : {
                                            "$lte" : ISODate("2018-12-29T03:56:00.000Z")
                                        }
                                    }, 
                                    {
                                        "time" : {
                                            "$gte" : ISODate("2018-12-26T00:00:00.000Z")
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "targetType" : 1.0
                                },
                                "indexName" : "targetType_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "targetType" : []
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "targetType" : [ 
                                        "[\"body\", \"body\"]", 
                                        "[\"face\", \"face\"]"
                                    ]
                                }
                            }
                        }
                    }
                }
            }, 
            {
                "stage" : "LIMIT",
                "limitAmount" : 100,
                "inputStage" : {
                    "stage" : "SKIP",
                    "skipAmount" : 100,
                    "inputStage" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "$and" : [ 
                                {
                                    "$or" : [ 
                                        {
                                            "targetType" : {
                                                "$eq" : "face"
                                            }
                                        }, 
                                        {
                                            "targetType" : {
                                                "$eq" : "body"
                                            }
                                        }
                                    ]
                                }, 
                                {
                                    "sourceId" : {
                                        "$eq" : "130"
                                    }
                                }, 
                                {
                                    "sourceType" : {
                                        "$eq" : "zipFile"
                                    }
                                }
                            ]
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "keyPattern" : {
                                "time" : -1.0
                            },
                            "indexName" : "time_-1",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "time" : []
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "time" : [ 
                                    "[new Date(1546055760000), new Date(1545782400000)]"
                                ]
                            }
                        }
                    }
                }
            }, 
            {
                "stage" : "SKIP",
                "skipAmount" : 100,
                "inputStage" : {
                    "stage" : "SORT",
                    "sortPattern" : {
                        "time" : -1.0
                    },
                    "limitAmount" : 200,
                    "inputStage" : {
                        "stage" : "SORT_KEY_GENERATOR",
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "$and" : [ 
                                    {
                                        "$or" : [ 
                                            {
                                                "targetType" : {
                                                    "$eq" : "face"
                                                }
                                            }, 
                                            {
                                                "targetType" : {
                                                    "$eq" : "body"
                                                }
                                            }
                                        ]
                                    }, 
                                    {
                                        "sourceId" : {
                                            "$eq" : "130"
                                        }
                                    }, 
                                    {
                                        "time" : {
                                            "$lte" : ISODate("2018-12-29T03:56:00.000Z")
                                        }
                                    }, 
                                    {
                                        "time" : {
                                            "$gte" : ISODate("2018-12-26T00:00:00.000Z")
                                        }
                                    }
                                ]
                            },
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "sourceType" : 1.0
                                },
                                "indexName" : "sourceType_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "sourceType" : []
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "sourceType" : [ 
                                        "[\"zipFile\", \"zipFile\"]"
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 100,
        "executionTimeMillis" : 3355, #指的是我们这条语句的执行时间,这个值当然是希望越少越好。
        "totalKeysExamined" : 134688, #索引扫描条目
        "totalDocsExamined" : 134688, #文档扫描条目 对于一个查询,
		#我们最理想的状态是:nReturned=totalKeysExamined=totalDocsExamined,那么又是什么影响到了totalKeysExamined和totalDocsExamined?是stage的类型。类型列举如下:
        "executionStages" : {
            "stage" : "SKIP",
            "nReturned" : 100, #该条查询返回的条目
            "executionTimeMillisEstimate" : 1750, #该查询根据index去检索document获得100条数据的时间。
            "works" : 134891,
            "advanced" : 100,
            "needTime" : 134790,
            "needYield" : 0,
            "saveState" : 4216,
            "restoreState" : 4216,
            "isEOF" : 1,
            "invalidates" : 0,
            "skipAmount" : 0,
            "inputStage" : {
                "stage" : "SORT",
                "nReturned" : 200, #该条查询返回的条目
                "executionTimeMillisEstimate" : 1740,  #该查询扫描200行index所用时间。
                "works" : 134891,
                "advanced" : 200,
                "needTime" : 134690,
                "needYield" : 0,
                "saveState" : 4216,
                "restoreState" : 4216,
                "isEOF" : 1,
                "invalidates" : 0,
                "sortPattern" : {
                    "time" : -1.0
                },
                "memUsage" : 4215716,
                "memLimit" : 33554432,
                "limitAmount" : 200,
                "inputStage" : {
                    "stage" : "SORT_KEY_GENERATOR",
                    "nReturned" : 134688,
                    "executionTimeMillisEstimate" : 1100,
                    "works" : 134690,
                    "advanced" : 134688,
                    "needTime" : 1,
                    "needYield" : 0,
                    "saveState" : 4216,
                    "restoreState" : 4216,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "inputStage" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "$and" : [ 
                                {
                                    "$or" : [ 
                                        {
                                            "targetType" : {
                                                "$eq" : "face"
                                            }
                                        }, 
                                        {
                                            "targetType" : {
                                                "$eq" : "body"
                                            }
                                        }
                                    ]
                                }, 
                                {
                                    "sourceType" : {
                                        "$eq" : "zipFile"
                                    }
                                }, 
                                {
                                    "time" : {
                                        "$lte" : ISODate("2018-12-29T03:56:00.000Z")
                                    }
                                }, 
                                {
                                    "time" : {
                                        "$gte" : ISODate("2018-12-26T00:00:00.000Z")
                                    }
                                }
                            ]
                        },
                        "nReturned" : 134688,
                        "executionTimeMillisEstimate" : 840,
                        "works" : 134689,
                        "advanced" : 134688,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 4216,
                        "restoreState" : 4216,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 134688,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "nReturned" : 134688,
                            "executionTimeMillisEstimate" : 90,
                            "works" : 134689,
                            "advanced" : 134688,
                            "needTime" : 0,
                            "needYield" : 0,
                            "saveState" : 4216,
                            "restoreState" : 4216,
                            "isEOF" : 1,
                            "invalidates" : 0,
                            "keyPattern" : {
                                "sourceId" : 1.0
                            },
                            "indexName" : "sourceId_1",
                            "isMultiKey" : false,
                            "multiKeyPaths" : {
                                "sourceId" : []
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "forward",
                            "indexBounds" : {
                                "sourceId" : [ 
                                    "[\"130\", \"130\"]"
                                ]
                            },
                            "keysExamined" : 134688,
                            "seeks" : 1,
                            "dupsTested" : 0,
                            "dupsDropped" : 0,
                            "seenInvalidated" : 0
                        }
                    }
                }
            }
        }
    },
    "serverInfo" : {
        "host" : "deed829d4373",
        "port" : 27017,
        "version" : "3.6.5",
        "gitVersion" : "a20ecd3e3a174162052ff99913bc2ca9a839d618"
    },
    "ok" : 1.0
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值