Optimizing performance for complicated query

I have this query:

query = wixData.query("TaskList").hasSome("status", statusFilter).and(
wixData.query("TaskList").hasSome("account", adminAccountIds).or(
wixData.query("TaskList").eq("assignedBy", member.id) ));

The initial hasSome() filter is ANDed with another hasSome() filter ORed with another equality test. The hasSome() filter is itself an OR condition, so there’s a whole bunch of ORing going on. The first condition hasSome(“status”, taskFilter) is the most restrictive so I put it first, but that got me thinking - does the order actually matter? Does Wix do some sort of optimization in the background that ignores my order? Secondly, do the inner conditions effectively run as parallel queries so that, because of my AND and OR conditions, I am in effect running 3 queries?

Any tips for improving the performance of the above query would be appreciated. I would also be interested in hearing about how Wix does query performance optimization in general.

1 Like

Hi!

The ordering of clauses does not really matter. The query is a single scan of the collection, whatever matches the whole clause gets promoted to the result.

The only exception is if hasSome runs on a multi-reference field. In that case there is effectively a join operation - meaning there is a separate scan on the referenced collection. The hasSome queries are indexed, so they typically perform well even if used with larger data sets.

In case there is a Multi-Reference query, Wix Data will figure out the right order of execution, rearranging clauses does not really affect performance or query execution. As a more general rule - the less data needs to be read and return - the faster the query will perform.