Idiomatic Materialize SQL chart
Materialize 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 in the unnested field
If duplicates exist in the unnested field
|
Examples
Idiomatic Materialize SQL | |
---|---|
ANY() Equi-join condition |
If no duplicates in the unnested field
If duplicates exist in the unnested field
|
Window Functions
Materialize and window functions
For window functions, when an input record
in a partition (as determined by the PARTITION BY
clause of your window
function) is added/removed/changed, Materialize recomputes the results for the
entire window partition. This means that when a new batch of input data arrives
(that is, every second), the amount of computation performed is proportional
to the total size of the touched partitions.
For example, assume that in a given second, 20 input records change, and these records belong to 10 different partitions, where the average size of each partition is 100. Then, amount of work to perform is proportional to computing the window function results for 10*100=1000 rows.
As a rule of thumb, if the total size of all touched window partitions is at most 1000000 rows per second, then the system should be able to keep up with the input data as it arrives. However, if your use case has higher performance requirements, consider rewriting your query to not use window functions. If your query 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) |
|
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
|
Examples
Idiomatic Materialize SQL | |
---|---|
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. |
If suppressing 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 suppress the last row since it has no next row
To include the last row
|