Background: How window functions process partitions
When executing window functions, Firebolt must process all rows in a partition to compute results for that partition. This often requires accumulating all rows from the same partition in memory before producing any output. For large tables, this can lead to high memory consumption and slower query performance. Consider this example:ROW_NUMBER for any row, all rows with the same event_id must be accumulated in memory and sorted.
When most partitions contain only a single row, this approach is inefficient.
How the hint works
Theoptimize_for_single_row_window_partitions hint automatically rewrites your query to optimize for high-cardinality partitions where most partitions contain only a single row.
Currently, this hint optimizes only the ROW_NUMBER window function.
When you use this hint, Firebolt automatically rewrites your query to separate single-row partitions from multi-row partitions.
Instead of accumulating all rows in memory, Firebolt only processes multi-row partitions while quickly assigning a value of 1 to single-row partitions.
This significantly reduces memory consumption and improves query performance.
Here’s the equivalent manual rewrite that shows what Firebolt does automatically when you use the hint:
event_id for those partitions.
Using the hint
Use theoptimize_for_single_row_window_partitions hint by adding it as a query-level comment at the start of your SQL statement, as shown below.
Understanding the query plan
The plan with the hint shows aMaterialize operator that corresponds to the MATERIALIZED CTE in the manually rewritten version of the query.
This materialized CTE is used in both branches of the resulting UNION ALL query to identify which partitions have more than one row.