Are CTEs materialized, and what are the performance implications in an analytical context?

Quick Overview

Asks whether common table expressions are computed once or inlined like subqueries, and how that affects query performance on large analytical workloads. Tests knowledge of engine-specific CTE handling, optimization fences, predicate pushdown, repeated scans and reading execution plans.

Are CTEs materialized, and what are the performance implications in an analytical context?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: hard

Interview Round: Other

Are CTEs (common table expressions) materialized, and what are the performance implications in an analytical context? Picture a reporting query against a large fact table that is split into several CTEs for readability. ```hint Count references Count how many times each CTE is referenced by the rest of the query, and think about how much work happens in each case. ``` ```hint Filters Consider what an outer `WHERE` clause can do to a subquery that it cannot do to a result that was already computed. ``` ### What a Strong Answer Covers - That the behavior depends on the engine and its version, with correct examples for at least two engines - The benefit of computing a CTE once when it is referenced several times - The cost of an optimization fence: lost predicate pushdown, index use and partition pruning - Concerns specific to analytical warehouses, such as repeated scans of large tables and cost per byte scanned - How to verify and control the behavior through execution plans, hints and temporary tables ### Follow-up Questions - A query was fast on PostgreSQL 11 and got slower after an upgrade to PostgreSQL 14 with no code change. How could CTE handling explain it, and what would you do? - A CTE draws a random sample of rows and is referenced twice. What can go wrong? - When would you replace a CTE with a temporary table? - How can you tell from a PostgreSQL execution plan whether a CTE was materialized?

Overview: Asks whether common table expressions are computed once or inlined like subqueries, and how that affects query performance on large analytical workloads. Tests knowledge of engine-specific CTE handling, optimization fences, predicate pushdown, repeated scans and reading execution plans.

PracHub logo
General
Nov 24, 2026
hardData ScientistOtherData Manipulation (SQL/Python)
0
0

Are CTEs (common table expressions) materialized, and what are the performance implications in an analytical context?

Picture a reporting query against a large fact table that is split into several CTEs for readability.

What a Strong Answer Covers Guidance

  • That the behavior depends on the engine and its version, with correct examples for at least two engines
  • The benefit of computing a CTE once when it is referenced several times
  • The cost of an optimization fence: lost predicate pushdown, index use and partition pruning
  • Concerns specific to analytical warehouses, such as repeated scans of large tables and cost per byte scanned
  • How to verify and control the behavior through execution plans, hints and temporary tables

Follow-up Questions Guidance

  • A query was fast on PostgreSQL 11 and got slower after an upgrade to PostgreSQL 14 with no code change. How could CTE handling explain it, and what would you do?
  • A CTE draws a random sample of rows and is referenced twice. What can go wrong?
  • When would you replace a CTE with a temporary table?
  • How can you tell from a PostgreSQL execution plan whether a CTE was materialized?