MongoDB,在索引字段上正则表达式查询的性能

时间:2021-05-30 04:15:01

I want to find an account by name (in a MongoDB collection of 50K accounts)

我想按名称查找一个帐户(在MongoDB的50K帐户集合中)

In the usual way: we find with string

用通常的方法:我们用字符串查找

db.accounts.find({ name: 'Jon Skeet' })  // indexes help improve performance!

How about with regular expression? Is it an expensive operation?

正则表达式呢?这是一个昂贵的手术吗?

db.accounts.find( { name: /Jon Skeet/ }) // worry! how indexes work with regex?

Edit:

编辑:

According to WiredPrairie:
MongoDB use prefix of RegEx to lookup indexes (ex: /^prefix.*/):

根据WiredPrairie:MongoDB使用前缀的正则表达式查找索引(例:/ ^前缀。* /):

db.accounts.find( { name: /^Jon Skeet/ })  // indexes will help!'

MongoDB $regex

MongoDB美元正则表达式

1 个解决方案

#1


22  

Actually according to the documentation,

实际上根据文件,

If an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. Further optimization can occur if the regular expression is a “prefix expression”, which means that all potential matches start with the same string. This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range.

如果该字段存在索引,则MongoDB将正则表达式与索引中的值匹配,这比收集扫描要快。如果正则表达式是“前缀表达式”,则可以进行进一步的优化,这意味着所有潜在匹配都以相同的字符串开始。这允许MongoDB从这个前缀构造一个“范围”,并且只与该范围内的索引中的值匹配。

http://docs.mongodb.org/manual/reference/operator/query/regex/#index-use

http://docs.mongodb.org/manual/reference/operator/query/regex/ index-use

In other words:

换句话说:

For /Jon Skeet/ regex ,mongo will full scan the keys in the index then will fetch the matched documents, which can be faster than collection scan.

对于/Jon Skeet/ regex,mongo将完全扫描索引中的键,然后获取匹配的文档,这比收集扫描要快。

For /^Jon Skeet/ regex ,mongo will scan only the range that start with the regex in the index, which will be faster.

/ ^ Jon水瓢/正则表达式,蒙戈只扫描的范围从该指数的正则表达式,将更快。

#1


22  

Actually according to the documentation,

实际上根据文件,

If an index exists for the field, then MongoDB matches the regular expression against the values in the index, which can be faster than a collection scan. Further optimization can occur if the regular expression is a “prefix expression”, which means that all potential matches start with the same string. This allows MongoDB to construct a “range” from that prefix and only match against those values from the index that fall within that range.

如果该字段存在索引,则MongoDB将正则表达式与索引中的值匹配,这比收集扫描要快。如果正则表达式是“前缀表达式”,则可以进行进一步的优化,这意味着所有潜在匹配都以相同的字符串开始。这允许MongoDB从这个前缀构造一个“范围”,并且只与该范围内的索引中的值匹配。

http://docs.mongodb.org/manual/reference/operator/query/regex/#index-use

http://docs.mongodb.org/manual/reference/operator/query/regex/ index-use

In other words:

换句话说:

For /Jon Skeet/ regex ,mongo will full scan the keys in the index then will fetch the matched documents, which can be faster than collection scan.

对于/Jon Skeet/ regex,mongo将完全扫描索引中的键,然后获取匹配的文档,这比收集扫描要快。

For /^Jon Skeet/ regex ,mongo will scan only the range that start with the regex in the index, which will be faster.

/ ^ Jon水瓢/正则表达式,蒙戈只扫描的范围从该指数的正则表达式,将更快。