principlesqlMajor
PostgreSQL Common Table Expressions vs a temporary table?
Viewed 0 times
postgresqltemporaryexpressionscommontable
Problem
The PostgreSQL documentation on WITH shows the following example:
It also notes:
A useful property of WITH queries is that they are evaluated only once
per execution of the parent query, even if they are referred to more
than once by the parent query or sibling WITH queries.
I see that
WITH regional_sales AS (
SELECT region, SUM(amount) AS total_sales
FROM orders
GROUP BY region
), top_regions AS (
SELECT region
FROM regional_sales
WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
)
SELECT region,
product,
SUM(quantity) AS product_units,
SUM(amount) AS product_sales
FROM orders
WHERE region IN (SELECT region FROM top_regions)
GROUP BY region, product;It also notes:
A useful property of WITH queries is that they are evaluated only once
per execution of the parent query, even if they are referred to more
than once by the parent query or sibling WITH queries.
I see that
WITH can be used for other things, like recursive evaluation. But in the example above, is there any important difference between using WITH and creating temporary tables?Solution
There are a few subtle differences, but nothing drastic:
Overall, you should prefer
However, the other option, a subquery in the
- You can add indexes on a temp table;
- Temp tables exist for the life of the session (or, if
ON COMMIT DROP, transaction), wherasWITHis always scoped strictly to the query;
- If a query invokes a function/procedure, it can see the temp table, but it can not see any
WITHtable-expressions;
- A temp table generates
VACUUMwork on the system catalogs thatWITHdoesn't, it needs an extra round trip to create/fill it, and it requires extra work in the server's cache management, so it's slightly less efficient.
Overall, you should prefer
WITH to temp tables unless you know you will benefit from creating an index.However, the other option, a subquery in the
FROM clause, has a very different set of advantages. It can be inlined, in particular, and qualifiers can be pulled up / pushed down. I wrote about this in a recent blog article.Context
StackExchange Database Administrators Q#78253, answer score: 26
Revisions (0)
No revisions yet.