0
0
PostgreSQLquery~10 mins

CTE vs subquery performance in PostgreSQL - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - CTE vs subquery performance
Start Query
Check for CTE or Subquery
Materialize?
Use Result
Return Final Result
The query starts, checks if it uses a CTE or subquery, executes accordingly, possibly materializing CTE results, then returns the final output.
Execution Sample
PostgreSQL
WITH cte AS (
  SELECT * FROM orders WHERE amount > 100
)
SELECT * FROM cte WHERE customer_id = 1;
This query uses a CTE to filter orders with amount > 100, then selects those for customer_id 1.
Execution Table
StepActionEvaluationResult
1Start query executionWITH clause foundPrepare CTE execution
2Execute CTE querySELECT * FROM orders WHERE amount > 100Filtered rows with amount > 100
3Materialize CTE resultStore filtered rowsCTE result stored in memory
4Execute outer querySELECT * FROM cte WHERE customer_id = 1Filter CTE rows by customer_id
5Return final resultRows matching both conditionsFinal filtered rows returned
6EndNo more stepsQuery complete
💡 Query ends after returning filtered rows from CTE
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
CTE Resultemptyfiltered orders > 100stored in memoryfiltered by customer_id=1final filtered rows
Key Moments - 2 Insights
Why does the CTE sometimes cause slower performance than a subquery?
Because in PostgreSQL, CTEs are often materialized (stored) before the outer query runs, as shown in step 3 of the execution_table, which can add overhead compared to subqueries that are inlined.
Does a subquery always run faster than a CTE?
Not always. Subqueries can be optimized and inlined by the planner, avoiding materialization, but complex queries or repeated use of the same CTE can make CTEs more efficient.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the CTE result materialized?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Action' and 'Result' columns in step 3 for materialization details.
According to variable_tracker, what is the state of 'CTE Result' after step 4?
Aempty
Bstored in memory
Cfiltered by customer_id=1
Dfiltered orders > 100
💡 Hint
Look at the 'After Step 4' column for 'CTE Result' in variable_tracker.
If the CTE was replaced by a subquery, which step might be skipped?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Materialization (step 3) is typical for CTEs but often skipped for subqueries.
Concept Snapshot
CTE vs Subquery Performance in PostgreSQL:
- CTEs often materialize results before outer query runs.
- Subqueries are usually inlined and optimized.
- Materialization can slow queries if large data sets.
- Use CTEs for readability or repeated use.
- Use subqueries for potential performance gains.
Full Transcript
This visual execution shows how PostgreSQL handles queries with CTEs versus subqueries. The query starts by detecting a CTE, executes the inner query filtering orders with amount greater than 100, then materializes the result in memory. After that, the outer query filters these results by customer_id. This materialization step can add overhead compared to subqueries, which are often inlined and optimized without storing intermediate results. The variable tracker shows how the CTE result changes state through execution. Key moments clarify why CTEs can be slower and when subqueries might be better. The quiz tests understanding of materialization and variable states during execution.