0

I have some heavy queries where performance requires that filters are applied before joins. According to some SO posts

The query optimizer is often smart enough to filter early.

session.query(Table1.post_id)\
    .join(Table2, Table1.post_id == Table2.post_id)\
    .filter(and_(Table1.user_id == thing1.id, Table2.blob_id == thing2.id))

Should I expect this type of query to be performed such that filters are applied before the join?

4
  • 2
    Check the execution plan Commented Feb 7, 2019 at 11:26
  • have you tried to debug an actual query? Commented Feb 7, 2019 at 11:27
  • This is more about "Postgresql performance" than "SQLAlchemy filter performance", unless you're asking how to run EXPLAIN on SQLA queries. Commented Feb 7, 2019 at 11:27
  • The only thing we can analyze is the actual SQL generated by SQLAlchemy that gets passed to Pg to be executed. Commented Feb 7, 2019 at 14:55

1 Answer 1

3

Yes, PostgreSQL will apply the filters first whenever it expects that to be faster, which will probably always be the case.

To be certain, get the actual SQL statement and examine the execution plan with

EXPLAIN SELECT ...;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.