0
votes

Do parenthesis affect the order in which filter conditions get applied in WHERE clause? Say I have thousands of documents with this schema:

{
    "batchId": "<random_guid>",
    "size": "<small, medium, or large>",
    "details":
    {
        ...
    }
}

Right now in my collection all documents belong in one of 50 batches. Batches are further broken down by the size, so roughly 1000 documents per batch.

Therefore when querying for all documents with size small in batch x, I would like to reduce the number of documents to search, by first filtering out all of those that don't belong to batch x, and then from that subset of documents only select those that have size small.

Will this query achieve the aforementioned behavior: SELECT * FROM r WHERE ((r.batchId = 'x') AND r.size = 'small')

1

1 Answers

1
votes

Parenthesis won't affect how the filter expression is evaluated. For expression evaluation order, what might matter here is the order of expressions in the WHERE clause (i.e. (r.batchId = 'x' AND r.size = 'small') vs. (r.size = 'small' AND r.batchId = 'x')). While the query engine reorders the expressions in order to achieve optimal execution plan, it will preserve the input query expression order if it concluded that there are no perceived gains from changing the order. In this specific case, the order won't matter that much as both expressions will be evaluated against the index and only documents that satisfy both conditions will be loaded.