Idiomatic Materialize SQL chart
View as MarkdownMaterialize follows the SQL standard (SQL-92) implementation and strives for compatibility with the PostgreSQL dialect. However, for some use cases, Materialize provides its own idiomatic query patterns that can provide better performance.
General
Query Patterns
| Idiomatic Materialize SQL Pattern | |
|---|---|
ANY() Equi-join condition
|
If no duplicates exist in the unnested field: Use a Common Table
Expression (CTE) to Duplicates may exist in the unnested field: Use a Common Table
Expression (CTE) to |
NOT IN (<subquery>) predicate
|
Rewrite to Filter out |
mz_now() with date/time operators
|
Rewrite the query expression; specifically, move the operation to the other side of the comparison. |
mz_now() with disjunctions (OR) in materialized/indexed view
definitions and SUBSCRIBE statements
|
Rewrite as
|
Examples
| Example | |
|---|---|
ANY() Equi-join condition
|
If no duplicates in the unnested field To omit duplicates that may exist in the unnested field |
NOT IN (<subquery>) predicate
|
Because the subquery uses Rewrite to Filter out |
mz_now() with date/time operators
|
|
mz_now() with disjunctions (OR) in materialized/indexed view
definitions and SUBSCRIBE statements
|
Rewrite as Rewrite as Rewrite as |
Window Functions
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.
Query Patterns
| Idiomatic Materialize SQL Pattern | |
|---|---|
|
Top-K over partition (K >= 1) |
Use a subquery to
SELECT DISTINCT on the grouping key (e.g.,
|
|
Top-K over partition (K = 1) |
|
|
First value over partition order by … |
Use a subquery that uses the MIN() or MAX() aggregate function. |
|
Last value over partition order by … range between unbounded preceding and unbounded following |
Use a subquery that uses the MIN() or MAX() aggregate function. |
| Lag over (order by) whose ordering can be represented by some equality condition. |
To exclude the first row since it has no previous row To include the first row |
| Lead over (order by) whose ordering can be represented by some equality condition. |
To exclude the last row since it has no next row To include the last row |
Examples
| Example | |
|---|---|
|
Top-K over partition (K >= 1) |
|
|
Top-K over partition (K = 1) |
|
|
First value over partition order by … |
|
|
Last value over partition order by … range between unbounded preceding and unbounded following |
|
| Lag over (order by) whose ordering can be represented by some equality condition. |
To exclude the first row since it has no previous row To include the first row |
| Lead over (order by) whose ordering can be represented by some equality condition. |
To exclude the last row since it has no next row To include the last row |