Nitesh Kumar Anand
Nitesh Kumar Anand

Reputation: 621

In AQL, do joins happen first or the filters?

I have 2 collections, users and employees, such that _id of the employee document is embedded in user document. I was wondering which of the two below is more efficient way to do it.

FOR employee IN employees
    FOR user IN users
            FILTER user.email == '[email protected]'
    FILTER employee._id == user.employee
RETURN merge({user: user, active: employee.active})

OR

FOR user IN users
    FOR employee IN employees
            FILTER employee._id == user.employee
    FILTER user.email == '[email protected]'
RETURN merge({user: user, active: employee.active})

Upvotes: 1

Views: 114

Answers (1)

mpv89
mpv89

Reputation: 1891

In your case it doesn't matter. The query will be automatically optimized from the database execution planner and rewritten to a query that looks similar to the following.

FOR employee IN employees
  FILTER user.email == '[email protected]'
  FOR user IN users
    FILTER employee._id == user.employee
    RETURN merge({user: user, active: employee.active})

You can check how a query is optimized over the webUI in the query tab with the button explain. It will print you the execution plan where you can also see which indexes are used and how much every part of your query costs. It's a very powerful tool.

If you want to know more about query execution and performance you should take a look into the docs.

Upvotes: 1

Related Questions