gpt4 book ai didi

mongodb 使用性能较低的索引

转载 作者:可可西里 更新时间:2023-11-01 09:13:27 25 4
gpt4 key购买 nike

我正在使用 mongodb 2.6,我有以下查询:

db.getCollection('Jobs').find(
{ $and: [ { RunID: { $regex: ".*_0" } },
{ $or: [ { JobType: "TypeX" },
{ JobType: "TypeY" },
{ JobType: "TypeZ" },
{ $and: [ { Info: { $regex: "Weekly.*" } }, { JobType: "YetAnotherType" } ] } ] } ] })

我有三个不同的索引:RunIDRunID + JobTypeRunID + JobType + Info。 Mongo 总是使用仅包含 RunID 的索引,尽管其他索引似乎更有可能产生更快的结果,但它有时甚至使用由 RunID + StartTime 组成的索引,而 StartTime 甚至不在列表中使用过的字段,知道为什么选择该索引吗?

最佳答案

注1:

您可以删除前 2 个索引,RunIDRunID + JobType。只使用扩展复合索引RunID + JobType + Info就足够了;这也可用于查询 RunIDRunID + JobType 字段,info here :

In addition to supporting queries that match on all the index fields, compound indexes can support queries that match on the prefix of the index fields.

当你删除这些索引时,mongo 会选择唯一剩下的索引。

注2:

您可以随时使用 hint , 告诉 mongo 使用特定的索引:

db.getCollection('Jobs').find().hint({RunID:1, JobType:1, Info:1})

关于mongodb 使用性能较低的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39327765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com