0
0
PostgreSQLquery~5 mins

CTE vs subquery performance in PostgreSQL - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is a Common Table Expression (CTE) in SQL?
A CTE is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. It helps organize complex queries by breaking them into simpler parts.
Click to reveal answer
beginner
How does a subquery differ from a CTE?
A subquery is a query nested inside another query, often used inline. A CTE is defined before the main query and can be referenced multiple times, improving readability and sometimes performance.
Click to reveal answer
intermediate
Does PostgreSQL always optimize CTEs and subqueries the same way?
No. Before PostgreSQL 12, CTEs acted as optimization fences, meaning they were executed separately and results materialized. Subqueries could be inlined and optimized better. From PostgreSQL 12, CTEs can be inlined like subqueries unless marked with MATERIALIZED or NOT MATERIALIZED.
Click to reveal answer
intermediate
When might a CTE perform worse than a subquery?
If the CTE is materialized (executed and stored) and used only once, it can cause extra work and slower performance compared to a subquery that the planner can inline and optimize.
Click to reveal answer
beginner
What is one advantage of using CTEs despite potential performance costs?
CTEs improve query readability and maintainability by breaking complex queries into named parts. They also allow recursive queries, which subqueries cannot do.
Click to reveal answer
Before PostgreSQL 12, how were CTEs treated in terms of query optimization?
AThey acted as optimization fences and were materialized separately
BThey were always inlined and optimized with the main query
CThey were ignored by the query planner
DThey were automatically converted to subqueries
Which keyword forces a CTE to be materialized in PostgreSQL 12 and later?
AMATERIALIZED
BOPTIMIZE
CINLINE
DCACHE
Which of the following is a benefit of using subqueries over CTEs?
ABetter readability for complex queries
BAbility to write recursive queries
CPotentially better performance due to inlining
DNamed temporary result sets
What is a common reason to choose a CTE despite possible performance costs?
AThey always run faster than subqueries
BThey allow breaking queries into understandable parts
CThey reduce disk space usage
DThey automatically index the data
In PostgreSQL 12+, what happens if you do not specify MATERIALIZED or NOT MATERIALIZED for a CTE?
AThe query will fail
BThe CTE is always inlined if possible
CThe CTE is always materialized
DThe planner decides whether to inline or materialize
Explain the performance differences between CTEs and subqueries in PostgreSQL, especially before and after version 12.
Think about how the query planner treats CTEs and subqueries differently across versions.
You got /6 concepts.
    Describe when it might be better to use a subquery instead of a CTE for performance reasons.
    Consider how materialization affects execution time.
    You got /4 concepts.