【问题标题】:Why cursor.skip() is sometimes 3 times slower if all-keys are indexed?如果所有键都被索引,为什么 cursor.skip() 有时会慢 3 倍?
【发布时间】:2016-06-30 00:26:24
【问题描述】:

我问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)"
                            ]
                        }
                    }
                }
            }
        }
    ]
},

【问题讨论】:

  • 您正在查询非索引键,它被理解为较慢。很抱歉,但我不明白这一点。
  • @Saleem,非索引键是什么意思。查看db.fast.getIndexes() 的输出。显然所有的键都是字符串并且是可排序的并且可以存储在 B-Tree 数据结构中。
  • 好吧,你知道在所有键上都有索引并不意味着索引实际上被用于查询。如果可能,请向我们展示完整的执行计划
  • @Saleem,附上。

标签: javascript mongodb indexing query-optimization


【解决方案1】:

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

我已经向 MongoDB 提出了这个问题。请看Jira Ticket

【讨论】:

  • 似乎 Jira 没有得到任何关注。也许流程符合预期?
  • 好吧,希望一切顺利:)
  • @0x90,你是对的。我终于得到了 MongoDB 的回复。普通的旧罐头信息。 “按设计”。见jira.mongodb.org/browse/SERVER-23141
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 2016-10-06
  • 1970-01-01
  • 2021-07-23
  • 2016-06-28
  • 2018-01-24
相关资源
最近更新 更多