Question

I have query in postgresql 9.1 like

SELECT id
FROM students
INNER JOIN exams ON /some condition
WHERE studentsid NOT IN (SUBQUERY);

and when I run only the subquery it executes in 120ms, when I execute the previous query without condition with subquery it executes for 12 seconds, but when I add subquery it runs half hour Is possible to cache in some variable results of subquery (results are always same array of ids) and execute in console/pgadmin ? I found WITH statement but it looks like is not supported in postgres

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top