$ exists的最佳复合索引:true(稀疏索引)

d0x*_*d0x 3 mongodb mongodb-query

问题

我需要加速这种查询:

db.col.find({ a: "foobar", b: { $exists: true} });
Run Code Online (Sandbox Code Playgroud)

数据分布

字段的存在:

  • 该字段a存在于所有文档中,
  • 该领域b仅存在于其中的约10%.

当前表统计:

db.col.count() // 1,050,505
db.col.count({ a : "foobar" }) // 517.967
db.col.count({ a : "foobar", b : { $exists: true} }) // 44.922
db.col.count({ b : { $exists: true} }) // 88.981
Run Code Online (Sandbox Code Playgroud)

Futrue数据增长:

到目前为止,两批装载(2x约500,000).每个月将增加另一批约500,000份文件.该a字段是此批次的名称.那些新添加的文档将具有相同的字段分布(大约10%的新加载文档将具有该b字段)

我的尝试和研究

我创建了一个稀疏索引,{a:1, b:1}但是因为a它存在于所有文档中,所以不会加速它.这是因为MongoDB中稀疏索引的行为.来自文档:

只要文档包含至少一个键,只包含升序/降序索引键的稀疏复合索引将索引文档.

这是.explain()上层查询:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "myCol",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [ 
                {
                    "a" : {
                        "$eq" : "foobar"
                    }
                }, 
                {
                    "b" : {
                        "$exists" : true
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "KEEP_MUTATIONS",
            "inputStage" : {
                "stage" : "FETCH",
                "filter" : {
                    "b" : {
                        "$exists" : true
                    }
                },
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "a" : 1,
                        "b" : 1
                    },
                    "indexName" : "a_1_b_1",
                    "isMultiKey" : false,
                    "direction" : "forward",
                    "indexBounds" : {
                        "a" : [ 
                            "[\"foobar\", \"foobar\"]"
                        ],
                        "b" : [ 
                            "[MinKey, MaxKey]"
                        ]
                    }
                }
            }
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 44922,
        "executionTimeMillis" : 208656,
        "totalKeysExamined" : 517967,
        "totalDocsExamined" : 517967,
        "executionStages" : {
            "stage" : "KEEP_MUTATIONS",
            "nReturned" : 44922,
            "executionTimeMillisEstimate" : 180672,
            "works" : 550772,
            "advanced" : 44922,
            "needTime" : 473045,
            "needFetch" : 32804,
            "saveState" : 41051,
            "restoreState" : 41051,
            "isEOF" : 1,
            "invalidates" : 0,
            "inputStage" : {
                "stage" : "FETCH",
                "filter" : {
                    "b" : {
                        "$exists" : true
                    }
                },
                "nReturned" : 44922,
                "executionTimeMillisEstimate" : 180612,
                "works" : 550772,
                "advanced" : 44922,
                "needTime" : 473045,
                "needFetch" : 32804,
                "saveState" : 41051,
                "restoreState" : 41051,
                "isEOF" : 1,
                "invalidates" : 0,
                "docsExamined" : 517967,
                "alreadyHasObj" : 0,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "nReturned" : 517967,
                    "executionTimeMillisEstimate" : 3035,
                    "works" : 517967,
                    "advanced" : 517967,
                    "needTime" : 0,
                    "needFetch" : 0,
                    "saveState" : 41051,
                    "restoreState" : 41051,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "keyPattern" : {
                        "a" : 1,
                        "b" : 1
                    },
                    "indexName" : "a_1_b_1",
                    "isMultiKey" : false,
                    "direction" : "forward",
                    "indexBounds" : {
                        "a" : [ 
                            "[\"foobar\", \"foobar\"]"
                        ],
                        "b" : [ 
                            "[MinKey, MaxKey]"
                        ]
                    },
                    "keysExamined" : 517967, // INFO: I think that this is too much. These are all documents having a:"foobar"
                    "dupsTested" : 0,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0,
                    "matchTested" : 0
                }
            }
        },
        "allPlansExecution" : []
    },
    "serverInfo" : {
        "host" : "productive-mongodb-16",
        "port" : 27000,
        "version" : "3.0.1",
        "gitVersion" : "534b5a3f9d10f00cd27737fbcd951032248b5952"
    }
}
Run Code Online (Sandbox Code Playgroud)

a所有1,000,000份文件中都有,其中有520,000份a:"foobar".在整个收集88,000有该b领域.

如何加速我的查询(以便IXSCAN只返回44k而不是520k)?

Bla*_*ven 6

你在这里似乎没有理解的是,$exists不能以任何方式"抓住"一个索引,即使在稀疏的地方.正如文档本身所说:

"如果稀疏索引会导致查询和排序操作的结果集不完整,那么MongoDB将不会使用该索引"

这些页面中给出的示例是{ "$exists": false }查询.但是反向逻辑条件在这里没有任何区别.

为了获得"稀疏"索引的"全部好处",您需要考虑它所拥有的数据的"类型"并进行适当的查询.

对于数字,类似于:

db.collection.find({ "a": "foobar", "b": { "$gte": -9999, "$lte": 9999 } })
Run Code Online (Sandbox Code Playgroud)

其中使用索引和稀疏索引.或者基于文本:

db.collection.find({ "a": "foobar", "b": /.+/ })
Run Code Online (Sandbox Code Playgroud)

这也将使用稀疏索引,只查看定义"b"的那些.

对于"阵列"然后"小心".正如所看到的值可能是以上之一,除非你这样做:

db.collection.insert({ "a": 1, "b": [[]] })
Run Code Online (Sandbox Code Playgroud)

那么这没关系:

db.ab.find({ "a": 1, "b": { "$type": 4 } })
Run Code Online (Sandbox Code Playgroud)

但实际上并没有真正使用"稀疏"指数,原因与此相同$exists.

因此,您需要了解这里的术语含义,以及"适当查询",以便在您期望最高性能时使用您创建的索引定义.

这些是您可以自己测试并看到结果是真实的明显示例.我希望核心文件在这些方面更清楚,但我也知道许多人已经尝试做出贡献(并且已经产生了很好的解释),但迄今为止都没有包括这些内容.

猜猜这就是你在这里问的原因.