Insert-triggered materialized views¶
Part of Streaming Transformations. See all pages in this section.
Insert-triggered materialized views (MVs) keep filtered projections or aggregates of streamed data up to date as rows are ingested, so queries can read precomputed results without a downstream transformation job. A target table can have multiple insert-triggered materialized views, so a single PIPE can keep multiple precomputed views of the same stream up to date.
Example¶
Insert-triggered materialized views can apply projections and filters, with or without aggregation. For example, the following views retain high-value orders and maintain order totals by region:
You can use a set of insert-triggered materialized views to partition an incoming data stream to a set of independent materialized views using a set of filters to create disjoint (or overlapping) partitions of the data. If the underlying base table data is no longer needed it can easily be purged regularly with a TRUNCATE statement.
How insert-triggered materialized views work¶
Insert-triggered materialized views are maintained when rows are inserted into their base table, through Snowpipe Streaming, COPY INTO, or INSERT. With Streaming Transformations, the base table is the PIPE’s target table. The view does not apply updates, deletes, or truncates on the base table.
When rows are ingested, Snowflake evaluates each insert-triggered materialized view definition (filters, aggregates, and projections) and inserts the resulting rows into the view as part of the same ingest path. Snowflake does not backfill rows that were loaded before the view was created.
To commit synchronously with the target table, the materialized views become part of the query plan of the related PIPE. Materialized views can be created before or after creating the PIPE.
Supported view definitions¶
The definition of an insert-triggered materialized view follows the rules for a standard materialized view. In particular:
- An insert-triggered materialized view queries a single base table. Joins, other views, and subqueries aren’t supported.
- A view can project columns and filter rows with
WHERE. - A view can aggregate with a limited set of aggregate functions, for example
COUNT,APPROX_COUNT_DISTINCT(HLL),COUNT_IF,SUM,MIN,MAX, andAVG(except when used inPIVOT). EveryGROUP BYkey must be in theSELECTlist. See materialized view for the complete list of supported aggregate functions. - A view can’t include UDFs, window functions,
HAVING,ORDER BY,LIMIT,GROUPING SETS,ROLLUP,CUBE, or nested aggregates. - Functions in the definition must be deterministic.
Sizing¶
Each insert-triggered materialized view adds ingest work, storage, and background maintenance to every streaming insert. Plan the number of views and what each one stores:
- Number of views: start with the views your queries need and add more only when a query requires one. Ingestion latency grows with the number of views on the target table.
- Aggregate views: a view with
GROUP BYreturns one row per group; through background consolidation, its stored size tends toward the number of distinct groups, not ingested rows. - Background costs: consolidation of aggregate views and automatic clustering of the views run as background services, and their compute is billed in addition to ingestion.
- Filtered projection views: a view without aggregation stores every row that passes its filter, so its size grows with ingested volume. A selective filter keeps it small.
- Validate: measure end-to-end latency with your real number of views before you move to production.
Limitations¶
- Insert-triggered materialized views are only supported on standard Snowflake tables and Interactive tables, except Interactive tables with Versioned upserts. Iceberg tables are currently not supported.
- An insert-triggered materialized view cannot be used as the source for dynamic tables or table streams.
- The
CLUSTER BYkeys of an aggregate insert-triggered materialized view must have theGROUP BYcolumns as leading columns. - Dropping a materialized view currently stops the
PIPEfrom ingesting into the target table. Mitigate by recreating the pipe. DELETE,UPDATE, andMERGEon the base table do not trigger maintenance and are not reflected in the materialized view.- Rows ingested before an insert-triggered materialized view exists aren’t backfilled into it.
- Use 30 or fewer insert-triggered materialized views per target table. Each materialized view adds to end-to-end latency. Test latency with actual views before going to production.
- Limitations of standard materialized views apply.
See also¶
The Scenarios show real-world use cases that combine insert-triggered materialized views with other transformations. See working with materialized views for more information on standard materialized views. For the complete syntax with all features combined, see the Reference.
Previous: Lateral Flatten · Next: Versioned upserts