Regex搜索的索引界限

Regex搜索的索引界限

本文介绍了Mongo Regex搜索的索引界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB,并且具有以下结构的文档集合:

I'm using MongoDB, and I have a collection of documents with the following structure:

{
    fName:"Foo",
    lName:"Barius",
    email:"[email protected]",
    search:"foo barius"
}

我正在构建一个函数,该函数将在 search 字段上执行正则表达式搜索.为了优化性能,我已在搜索字段中对该集合进行了索引.但是,事情仍然有点慢.因此,我在一个示例查询上运行了 explain():

I am building a function that will perform a regular expression search on the search field. To optimize performance, I have indexed this collection on the search field. However, things are still a bit slow. So I ran an explain() on a sample query:

db.Collection.find({search:/bar/}).explain();

根据获胜计划,我看到使用了以下指标范围:

Looking under the winning plan, I see the following index bounds used:

"search": [
        "[\"\", {})",
        "[/.*bar.*/, /.*bar.*/]"
]

第二组很有意义-它从包含bar的任何内容到包含bar的任何内容进行查找.但是,第一盘让我感到困惑.似乎是在" (包括 {} )之外的边界中查找.我担心这组额外的范围会减慢我的查询速度.有必要保留吗?如果不是,我如何防止它被包含?

The second set makes sense - it's looking from anything that contains bar to anything that contains bar. However, the first set baffles me. It appears to be looking in the bounds of "" inclusive to {} exclusive. I'm concerned that this extra set of bounds is slowing down my query. Is it necessary to keep? If it's not, how can I prevent it from being included?

推荐答案

我认为这只是mongodb与regex配合使用的方式(请参见 https://scalegrid.io/blog/mongodb-regular-expressions-indexes-performance/).只需注意nscanned/totalKeysExamined值,如果该值太大,则该索引对您的查询无用.

I think it's just the way mongodb works with regex (see https://scalegrid.io/blog/mongodb-regular-expressions-indexes-performance/). Just watch out for nscanned/totalKeysExamined value, if it's too large then the index is useless for your query.

另请参阅: MongoDB,通过对索引字段进行正则表达式查询的性能

这篇关于Mongo Regex搜索的索引界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:44