First, the with
statement is supported in Postgres.
Second, you need to identify where the performance problem is. Is it in the subquery? Or is it the not in
?
You can put the subquery in a table, add indexes, and make the query more efficient.
You can rewrite the subquery using a left join
, which often allows the query to be better optimized.
You can add appropriate indexes to make the entire query more efficient.
Without knowledge of what the subquery actually does, the right approach is speculation.