为什么如果对所有键进行索引,cursor.skip()有时会慢3倍?

时间:2016-03-14 19:33:41

标签: javascript mongodb indexing query-optimization

我问Is cursor.skip() on indexed keys always faster?

现在我正在尝试利用multi-key indexing (第94页)来执行更快的查询。

使用此脚本创建2个数据库:

var a = 0;
while(a++ < 4096*2){
  var randomnumber=Math.ceil(Math.random()*1000000)

  // create string in length of 3
  var name = Math.random().toString(36).substring(2,5);

  db.fast.insert({name:name, email:name + ".email@example.com", age:randomnumber});
  db.slow.insert({name:name, email:name + ".email@example.com", age:randomnumber});

}

数据库索引如下:

> db.fast.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.fast"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test.fast"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1,
            "name" : 1,
            "email" : 1
        },
        "name" : "age_1_name_1_email_1",
        "ns" : "test.fast"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 0,
            "name" : 0,
            "email" : 0
        },
        "name" : "age_0_name_0_email_0",
        "ns" : "test.fast"
    }
]
> 
> db.slow.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test.slow"
    }
]

我执行了这两个查询:

>pageNumber=18;nPerPage=20; db.slow.find({name:{$gt:"1234", $lt:"z"}, age:{$gt:200, $lt:777217}, email:{$gt:"bdnsa28831283d", $lt:"z"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")

    "executionSuccess" : true,
    "nReturned" : 20,
    "executionTimeMillis" : 93,
    "totalKeysExamined" : 0,
    "totalDocsExamined" : 688,


>pageNumber=18;nPerPage=20; db.fast.find({name:{$gt:"1234", $lt:"z"}, age:{$gt:200, $lt:777217}, email:{$gt:"bdnsa28831283d", $lt:"z"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")

    "executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 20,
    "executionTimeMillis" : 70,
    "totalKeysExamined" : 559,
    "totalDocsExamined" : 360,

如您所见,fast db的执行时间比预期的要好。

但在那之后我执行了以下两个并且慢了好多了:

>pageNumber=58;nPerPage=300; db.slow.find({name:{$gt:"1Adsa34", $lt:"zZ123z"}, age:{$gt:4555, $lt:988888}, email:{$gt:"abdnsa28831283d", $lt:"z2"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 0,
        "executionTimeMillis" : 30,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 8192,

>pageNumber=58;nPerPage=300; db.fast.find({name:{$gt:"1Adsa34", $lt:"zZ123z"}, age:{$gt:4555, $lt:988888}, email:{$gt:"abdnsa28831283d", $lt:"z2"} }).skip(pageNumber > 0 ? ((pageNumber-1)*nPerPage) : 0).limit(nPerPage).explain("executionStats")
        "executionSuccess" : true,
        "nReturned" : 0,
        "executionTimeMillis" : 101,
        "totalKeysExamined" : 8058,
        "totalDocsExamined" : 5484,

可能以某种方式服务的缓存问题,虽然我重新启动了mongod。

执行计划缓慢:

"queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "test.slow",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "age" : {
                    "$lt" : 988888
                }
            },
            {
                "email" : {
                    "$lt" : "z2"
                }
            },
            {
                "name" : {
                    "$lt" : "zZ123z"
                }
            },
            {
                "age" : {
                    "$gt" : 4555
                }
            },
            {
                "email" : {
                    "$gt" : "abdnsa28831283d"
                }
            },
            {
                "name" : {
                    "$gt" : "1Adsa34"
                }
            }
        ]
    },
    "winningPlan" : {
        "stage" : "LIMIT",
        "limitAmount" : 300,
        "inputStage" : {
            "stage" : "SKIP",
            "skipAmount" : 11616,
            "inputStage" : {
                "stage" : "COLLSCAN",
                "filter" : {
                    "$and" : [
                        {
                            "age" : {
                                "$lt" : 988888
                            }
                        },
                        {
                            "email" : {
                                "$lt" : "z2"
                            }
                        },
                        {
                            "name" : {
                                "$lt" : "zZ123z"
                            }
                        },
                        {
                            "age" : {
                                "$gt" : 4555
                            }
                        },
                        {
                            "email" : {
                                "$gt" : "abdnsa28831283d"
                            }
                        },
                        {
                            "name" : {
                                "$gt" : "1Adsa34"
                            }
                        }
                    ]
                },
                "direction" : "forward"
            }
        }
    },
    "rejectedPlans" : [ ]
},

快速数据库执行计划:

"queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "test.fast",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "age" : {
                    "$lt" : 988888
                }
            },
            {
                "email" : {
                    "$lt" : "z2"
                }
            },
            {
                "name" : {
                    "$lt" : "zZ123z"
                }
            },
            {
                "age" : {
                    "$gt" : 4555
                }
            },
            {
                "email" : {
                    "$gt" : "abdnsa28831283d"
                }
            },
            {
                "name" : {
                    "$gt" : "1Adsa34"
                }
            }
        ]
    },
    "winningPlan" : {
        "stage" : "LIMIT",
        "limitAmount" : 300,
        "inputStage" : {
            "stage" : "SKIP",
            "skipAmount" : 17100,
            "inputStage" : {
                "stage" : "FETCH",
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "age" : 0,
                        "name" : 0,
                        "email" : 0
                    },
                    "indexName" : "age_0_name_0_email_0",
                    "isMultiKey" : false,
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "age" : [
                            "(4555.0, 988888.0)"
                        ],
                        "name" : [
                            "(\"1Adsa34\", \"zZ123z\")"
                        ],
                        "email" : [
                            "(\"abdnsa28831283d\", \"z2\")"
                        ]
                    }
                }
            }
        }
    },
    "rejectedPlans" : [
        {
            "stage" : "LIMIT",
            "limitAmount" : 300,
            "inputStage" : {
                "stage" : "SKIP",
                "skipAmount" : 17100,
                "inputStage" : {
                    "stage" : "FETCH",
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "age" : 1,
                            "name" : 1,
                            "email" : 1
                        },
                        "indexName" : "age_1_name_1_email_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                            "age" : [
                                "(4555.0, 988888.0)"
                            ],
                            "name" : [
                                "(\"1Adsa34\", \"zZ123z\")"
                            ],
                            "email" : [
                                "(\"abdnsa28831283d\", \"z2\")"
                            ]
                        }
                    }
                }
            }
        },
        {
            "stage" : "LIMIT",
            "limitAmount" : 300,
            "inputStage" : {
                "stage" : "SKIP",
                "skipAmount" : 17100,
                "inputStage" : {
                    "stage" : "FETCH",
                    "filter" : {
                        "$and" : [
                            {
                                "email" : {
                                    "$lt" : "z2"
                                }
                            },
                            {
                                "name" : {
                                    "$lt" : "zZ123z"
                                }
                            },
                            {
                                "email" : {
                                    "$gt" : "abdnsa28831283d"
                                }
                            },
                            {
                                "name" : {
                                    "$gt" : "1Adsa34"
                                }
                            }
                        ]
                    },
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "age" : 1
                        },
                        "indexName" : "age_1",
                        "isMultiKey" : false,
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 1,
                        "direction" : "forward",
                        "indexBounds" : {
                            "age" : [
                                "(4555.0, 988888.0)"
                            ]
                        }
                    }
                }
            }
        }
    ]
},

1 个答案:

答案 0 :(得分:1)

是的,你是对的,我已经验证并且具有讽刺意味的是,假设覆盖索引的速度很快,忽略了最快的执行计划。

我已经用MongoDB提出了这个问题。请参阅Jira Ticket