MongoDB如何评估多个$ or语句? [英] How does MongoDB evaluates multiple $or statements?

查看:37
本文介绍了MongoDB如何评估多个$ or语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB将如何评估此查询:

How will MongoDB evaluate this query:

db.testCol.find(
{
    "$or" : [ {a:1, b:12}, {b:9, c:15}, {c:10, d:"foo"} ]
});

当第一个OR语句为TRUE时扫描文档中的值时,是否还会评估其他语句?

When scanning values in a document if first OR statement is TRUE will the other statements be also be evaluated?

从逻辑上讲,如果MongoDB得到优化,则不应评估OR语句中的其他值,但我不知道MongoDB的实现方式.

Logically if the MongoDB is optimized other values in OR statement should not be evaluated, but I don't know how MongoDB is implemented.

更新: 我更新了查询,因为它是错误的,并且没有正确解释我要完成的工作.我需要找到一组具有不同属性的文档,并且如果找到这些属性的精确组合,则必须返回该文档.

UPDATE: I updated my query because it was wrong and it didn't explain correctly what I was trying to accomplish. I need to find a set of documents that have different properties and if an exact combination of these properties is found the document must be returned.

我查询的SQL等效项是:

The SQL equivalent of my query would be:

SELECT * FROM testCol 
WHERE (a = 1 AND b = 12) OR (b = 9 AND c = 15) OR (c = 10 AND d = 'foo'); 

推荐答案

MongoDB将执行$ or操作的每个子句作为单独的查询,并删除重复项作为后期处理.因此,每个子句都可以使用单独的索引,这通常非常有用.

MongoDB will execute each clause of the $or operation as a seperate query and remove duplicates as a post processing pass. As such each clause can use a seperate index which is often very useful.

换句话说,它将不会查看1个文档,而是查看哪个OR子句适用,并且如果第一个子句是匹配项,则进行提前提取.相反,它对每个子句执行完整的数据集查询,并在事实发生后进行重复数据删除.这似乎效率不高,但实际上它总是快得多,因为第一种方法最多只能为所有子句命中最多一个索引,而这几乎是无效的.

In other words, it will NOT look at 1 document, see which of the OR clauses apply and do an early-out if the first clause is a match. Rather it does a full dataset query per clause and de-dupe after the fact. This may seem less than efficient but in practice it's almost always faster since the first approach would only be able to hit at most one index for all clauses which is rarely efficient.

这篇关于MongoDB如何评估多个$ or语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆