MongoDB索引分析explain

作者: 缘来是你ylh | 来源:发表于2019-03-10 06:10 被阅读0次

今天小编通过explain()方法来分析集合中索引的使用效果

1.首先循环插入100w条记录

for (var i = 0; i < 1000000; i++ ) { 
    db.person.insert({"name":"test"+i, "cardNo":i}) 
}

2.查看插入效果

> db.person.find()
{ "_id" : ObjectId("5c7c9e15a795b984b42ad96b"), "name" : "test0", "cardNo" : 0 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad96c"), "name" : "test1", "cardNo" : 1 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad96d"), "name" : "test2", "cardNo" : 2 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad96e"), "name" : "test3", "cardNo" : 3 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad96f"), "name" : "test4", "cardNo" : 4 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad970"), "name" : "test5", "cardNo" : 5 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad971"), "name" : "test6", "cardNo" : 6 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad972"), "name" : "test7", "cardNo" : 7 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad973"), "name" : "test8", "cardNo" : 8 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad974"), "name" : "test9", "cardNo" : 9 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad975"), "name" : "test10", "cardNo" : 10 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad976"), "name" : "test11", "cardNo" : 11 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad977"), "name" : "test12", "cardNo" : 12 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad978"), "name" : "test13", "cardNo" : 13 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad979"), "name" : "test14", "cardNo" : 14 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad97a"), "name" : "test15", "cardNo" : 15 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad97b"), "name" : "test16", "cardNo" : 16 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad97c"), "name" : "test17", "cardNo" : 17 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad97d"), "name" : "test18", "cardNo" : 18 }
{ "_id" : ObjectId("5c7c9e15a795b984b42ad97e"), "name" : "test19", "cardNo" : 19 }
Type "it" for more

3.没有索引情况下查询分析

> db.person.find({"name":"test890000"}).explain("executionStats")
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.person",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "name" : {
                "$eq" : "test890000"
            }
        },
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "name" : {
                    "$eq" : "test890000"
                }
            },
            "direction" : "forward"
        },
        "rejectedPlans" : [ ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 389,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 1000000,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "name" : {
                    "$eq" : "test890000"
                }
            },
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 232,
            "works" : 1000002,
            "advanced" : 1,
            "needTime" : 1000000,
            "needYield" : 0,
            "saveState" : 7814,
            "restoreState" : 7814,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 1000000
        }
    },
    "serverInfo" : {
        "host" : "shengyulongdeMacBook-Pro-2.local",
        "port" : 27017,
        "version" : "4.0.3",
        "gitVersion" : "7ea530946fa7880364d88c8d8b6026bbc9ffa48c"
    },
    "ok" : 1
}

4.给name字段创建升序索引

> db.person.createIndex({"name":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

5.explain查看索引分析

> db.person.find({"name":"test890000"}).explain("executionStats")
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.person",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "name" : {
                "$eq" : "test890000"
            }
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "name" : 1
                },
                "indexName" : "name_1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "name" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "name" : [
                        "[\"test890000\", \"test890000\"]"
                    ]
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 1,
        "totalKeysExamined" : 1,
        "totalDocsExamined" : 1,
        "executionStages" : {
            "stage" : "FETCH",
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 2,
            "advanced" : 1,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 1,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 2,
                "advanced" : 1,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "name" : 1
                },
                "indexName" : "name_1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "name" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "name" : [
                        "[\"test890000\", \"test890000\"]"
                    ]
                },
                "keysExamined" : 1,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },
    "serverInfo" : {
        "host" : "shengyulongdeMacBook-Pro-2.local",
        "port" : 27017,
        "version" : "4.0.3",
        "gitVersion" : "7ea530946fa7880364d88c8d8b6026bbc9ffa48c"
    },
    "ok" : 1
}
  • executionStats.nReturned:返回的文档的数量
  • executionStats.executionTimeMillis:执行的时间,单位(ms)
  • executionStats.totalKeysExamined: 索引扫描条目。
  • executionStats.totalDocsExamined: 文档扫描条目

对比分析

使用索引 不使用索引
搜索时间 1ms 389ms
索引扫描条目 1 0
文档扫描条目 1 1000000

使用索引和不使用索引真是差别巨大啊!!!

相关文章

  • MongoDB索引

    准备阶段 插入10W条数据测试准备: MongoDB性能分析函数(explain) 查看索引 命名索引 删除索引 ...

  • MongoDB索引分析explain

    今天小编通过explain()方法来分析集合中索引的使用效果 1.首先循环插入100w条记录 2.查看插入效果 3...

  • MongoDB查询性能分析(explain) --- 2022-

    本章介绍MongoDB查询性能分析,类似SQL的explain,MongoDB也支持explain分析查询语句的性...

  • mongodb学习2

    MongoDB 索引 和 explain 的使用 索引基础唯一索引索引的一些参数使用 explainexplain...

  • Mongodb性能分析方法explain Executionst

    title: "Mongodb性能分析方法explain Executionstats"date: 2021-02...

  • MySQL Explain命令用法

    引用自 MySQL 性能优化神器 Explain 使用分析 一、介绍 explain显示了mysql如何使用索引...

  • MongoDB 索引和explain使用

    mongodb 索引使用 作用 索引通常能够极大的提高查询。 索引是一种数据结构,他搜集一个集合中文档特定字段的值...

  • Mysql 索引优化

    联合索引和单个索引选择 对比,值越大越好 强制使用某个索引 使用explain分析索引 1、id:SQL执行的顺序...

  • MySQL索引背后的数据结构及算法原理

    参考来源 mysql索引分析 MySQL索引背后的数据结构及算法原理 MySQL中EXPLAIN命令详解 索引连接...

  • mysql查询语句优化

    一、通过使用explain来分析sql查询性能 explain显示了mysql如何使用索引来处理select语句以...

网友评论

    本文标题:MongoDB索引分析explain

    本文链接:https://www.haomeiwen.com/subject/uheiuqtx.html