
Understanding CTEs and Their Impact
Common Table Expressions (CTEs) are widely used in SQL to simplify complex queries and enhance readability. However, there's a prevalent misconception about how they handle memory and execution.
The query executed in step 1 needs to be stored somewhere, which is the RAM. It occupies space (let's say 6 containers) and stays there until the query completes. This space remains occupied, whether it's being actively used or not. Now, the remainder of the query has only 6 containers to process the rest of the data.

Using a Subquery Instead:
The query at any point has the entirety of 12 containers to process the data. More containers mean faster execution—here, potentially 2x faster because the query processes in parallel. Imagine each container processes 10 GB. Even if the subquery portion needs to be executed twice, there's sufficient processing space, and once completed, the rest of the query has all 12 containers available.
This not only saves runtime but also optimizes query performance.
Best Practices
To optimize performance and manage memory effectively:
- Materialize Large CTEs:
For large datasets used multiple times, consider creating temporary tables instead of CTEs.
This can reduce RAM consumption and improve performance. - Use Subqueries:
Run them all as Subqueries. This should give you enough processing space to process them faster.