mongo索引性能

mongo索引性能

标签(空格分隔): mongo


https://www.cnblogs.com/c-abc/p/6023824.html

测试mongo索引性能

有索引,每个值都是唯一值


 "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 0,  ## 总耗时
        "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" : {
                    "attr" : 1
                },
                "indexName" : "attr",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "attr" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "attr" : [ 
                        "[\"所属国家\", \"所属国家\"]"
                    ]
                },
                "keysExamined" : 1,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },

无索引,每个值都是唯一值

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 6,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 4723,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "attr" : {
                    "$eq" : "所属国家"
                }
            },
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 4725,
            "advanced" : 1,
            "needTime" : 4723,
            "needYield" : 0,
            "saveState" : 36,
            "restoreState" : 36,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 4723
        }
    },

可以看出,有索引后只需要检索一个文档,并且时间由原来的6降低到了0

有索引,只有两个值

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 4723,
        "executionTimeMillis" : 13,  ## 
        "totalKeysExamined" : 4723, ## 这里有查找索引需要的时间
        "totalDocsExamined" : 4723,  ##
        "executionStages" : {
            "stage" : "FETCH",
            "nReturned" : 4723,
            "executionTimeMillisEstimate" : 20,
            "works" : 4724,
            "advanced" : 4723,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 36,
            "restoreState" : 36,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 4723,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 4723,
                "executionTimeMillisEstimate" : 20,
                "works" : 4724,
                "advanced" : 4723,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 36,
                "restoreState" : 36,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "domain_has_entity" : 1
                },
                "indexName" : "ha",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "domain_has_entity" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "domain_has_entity" : [ 
                        "[\"False\", \"False\"]"
                    ]
                },
                "keysExamined" : 4723,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },

you无索引,只有两个值

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 4723,
        "executionTimeMillis" : 6,
        "totalKeysExamined" : 0,  ## 可以看出不需要检索key值,所以没有索引反而省时间了
        "totalDocsExamined" : 4723,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "domain_has_entity" : {
                    "$eq" : "False"
                }
            },
            "nReturned" : 4723,
            "executionTimeMillisEstimate" : 0,
            "works" : 4725,
            "advanced" : 4723,
            "needTime" : 1,
            "needYield" : 0,
            "saveState" : 36,
            "restoreState" : 36,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 4723
        }
    },

对于个数较少的字段,就不要建索引了,省得他还有查索引的时间

无索引,联合查询

db.getCollection(‘rdf_useful’).find({“entity” : “蒋官屯街道”,”attr” : “辖社区数”}).explain(“executionStats”)

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 974,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 1805242,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "$and" : [ 
                    {
                        "attr" : {
                            "$eq" : "辖社区数" }
                    }, 
                    {
                        "entity" : {
                            "$eq" : "蒋官屯街道" }
                    }
                ]
            },
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 880,
            "works" : 1805244,
            "advanced" : 1,
            "needTime" : 1805242,
            "needYield" : 0,
            "saveState" : 14103,
            "restoreState" : 14103,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 1805242
        }
    },

只对一个字段建索引,但联合查询

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 0
        "totalKeysExamined" : 16,
        "totalDocsExamined" : 16,
        "executionStages" : {
            "stage" : "FETCH",
            "filter" : {
                "attr" : {
                    "$eq" : "辖社区数"
                }
            },
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 17,
            "advanced" : 1,
            "needTime" : 15,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 16,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 16,
                "executionTimeMillisEstimate" : 0,
                "works" : 17,
                "advanced" : 16,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "entity" : 1
                },
                "indexName" : "wn",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "entity" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "entity" : [ 
                        "[\"蒋官屯街道\", \"蒋官屯街道\"]"
                    ]
                },
                "keysExamined" : 16,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },
    ```

建立联合索引,联合查询

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 0,
        "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" : {
                    "entity" : 1,
                    "attr" : 1
                },
                "indexName" : "wn",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "entity" : [],
                    "attr" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "entity" : [ 
                        "[\"蒋官屯街道\", \"蒋官屯街道\"]"
                    ],
                    "attr" : [ 
                        "[\"辖社区数\", \"辖社区数\"]"
                    ]
                },
                "keysExamined" : 1,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },

联合查询,建立单一索引就已经有很大的性能提升了,建立联合索引后,更是飞快

比较查询,无索引

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1279,
        "executionTimeMillis" : 7,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 4723,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "num_of_domain" : {
                    "$gt" : 5.0
                }
            },
            "nReturned" : 1279,
            "executionTimeMillisEstimate" : 0,
            "works" : 4725,
            "advanced" : 1279,
            "needTime" : 3445,
            "needYield" : 0,
            "saveState" : 36,
            "restoreState" : 36,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 4723
        }
    },

有索引,比较查询

"executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1279,
        "executionTimeMillis" : 5,
        "totalKeysExamined" : 1279,
        "totalDocsExamined" : 1279,
        "executionStages" : {
            "stage" : "FETCH",
            "nReturned" : 1279,
            "executionTimeMillisEstimate" : 0,
            "works" : 1280,
            "advanced" : 1279,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 10,
            "restoreState" : 10,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 1279,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 1279,
                "executionTimeMillisEstimate" : 0,
                "works" : 1280,
                "advanced" : 1279,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 10,
                "restoreState" : 10,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "num_of_domain" : 1
                },
                "indexName" : "nu",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "num_of_domain" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "num_of_domain" : [ 
                        "(5.0, inf.0]"
                    ]
                },
                "keysExamined" : 1279,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },

对比较查询影响不大,提高一半左右

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值