Lead over
View as MarkdownOverview
The “lead over” query pattern accesses the field value of the next row as determined by some ordering.
For “lead over (order by)” queries whose ordering can be represented by some equality condition (such as when ordering by a field that increases at a regular interval), Materialize provides an idiomatic SQL as an alternative to the window function.
Materialize and window functions
For indexed views and materialized views that contain window
functions (including aggregate functions used
with an OVER clause), when an input record in a partition is
added/removed/changed, Materialize recomputes the results from scratch for
that partition (instead of using incremental computation).
The PARTITION BY clause of your window function determines your partitions. If
PARTITION BY is omitted, all records belong to a single partition (i.e., any
record change results in a recomputation from scratch over the whole input).
To avoid performance issues that may arise as the number of records grows, consider rewriting your indexed views and materialized views to use idiomatic Materialize SQL instead of window functions. If your view definitions cannot be rewritten without the window functions and the performance of window functions is insufficient for your use case, please contact our team.
Idiomatic Materialize SQL
Exclude the last row in results
Idiomatic Materialize SQL: To access the lead (next row’s field value)
ordered by some field that increases in regular intervals, use a self join
that specifies an equality condition on the order by field (e.g., WHERE t1.order_field = t2.order_field - 1, WHERE t1.order_field = t2.order_field * 2, etc.). The query excludes the last row in the results since it does not
have a next row.
|
Use a self join that specifies an equality match on the lead’s order by
field (e.g.,
! Important: The idiomatic Materialize SQL applies only to those “lead over” queries whose
ordering can be represented by some equality condition.
|
|
|
Avoid the use of |
Include the last row in results
Idiomatic Materialize SQL: To access the lead (next row’s field value)
ordered by some field that increases in regular intervals, use a self LEFT JOIN/LEFT OUTER JOIN that specifies an
equality condition on the order by field (e.g., ON t1.order_field = t2.order_field - 1, ON t1.order_field = t2.order_field * 2, etc.). The LEFT JOIN/LEFT OUTER JOIN query includes the last row, returning null as its
lead value.
|
Use a self
! Important: The idiomatic Materialize SQL applies only to those “lead over” queries whose
ordering can be represented by some equality condition.
|
|
|
Avoid the use of |
Examples
Find next row’s value (exclude the last row in results)
Using idiomatic Materialize SQL, the following example finds the next day’s
order total. That is, the example uses a self join on orders_daily_totals. The
row ordering on the order_date field is represented by an equality
condition using an interval of 1 DAY. The
query excludes the last row in the results since the last row does not have a
next row.
! Important: The idiomatic Materialize SQL applies only to those “lead over” queries whose
ordering can be represented by some equality condition.
|
|
|
|
Find next row’s value (include the last row in results)
Using idiomatic Materialize SQL, the following example finds the next day’s
order total. The example uses a self LEFT JOIN/LEFT OUTER JOIN on orders_daily_totals. The row
ordering on the order_date field is represented by an equality condition
using an interval of 1 DAY). The
query includes the last row in the results, using null as the next row’s
value.
! Important: The idiomatic Materialize SQL applies only to those “lead over” queries whose
ordering can be represented by some equality condition.
|
|
|
|