CREATE MATERIALIZED VIEW

statementsupportedTables, views, streams, sequencesEnterprise+

docs.snowflake.com ↗

Object: MATERIALIZED VIEW

Creates a new materialized view in the current/specified schema, based on a query of an existing table, and populates the view with data. For more details, see Working with Materialized Views. Materialized views require Enterprise Edition. To inquire about upgrading, please contact Snowflake Support.

Syntax

supportedpartialnot supportednot scored

CREATE [ OR REPLACE ] [ SECURE ] [ INTERACTIVE ] MATERIALIZED VIEW [ IF NOT EXISTS ] <name>
  [ COPY GRANTS ]
  [ COPY = ( GRANTS ) ]
  ( <column_list> )
  [ <col1> [ WITH ] MASKING POLICY <policy_name> [ USING ( <col1> , <cond_col1> , ... ) ]
           [ WITH ] PROJECTION POLICY <policy_name>
           [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
  [ , <col2> [ ... ] ]
  [ COMMENT = '<string_literal>' ]
  [ [ WITH ] ROW ACCESS POLICY <policy_name> ON ( <col_name> [ , <col_name> ... ] ) ]
  [ [ WITH ] AGGREGATION POLICY <policy_name> [ ENTITY KEY ( <col_name> [ , <col_name> ... ] ) ] ]
  [ CLUSTER BY ( <expr1> [, <expr2> ... ] ) ]
  [ [ WITH ] TAG ( <tag_name> = '<tag_value>' [ , <tag_name> = '<tag_value>' , ... ] ) ]
  [ WITH CONTACT ( <purpose> = <contact_name> [ , <purpose> = <contact_name> ... ] ) ]
  AS <select_statement>

Parameters

NameRequiredDescription
namerequiredSpecifies the identifier for the view; must be unique for the schema in which the view is created. In addition, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (e.g. "My object"). Identifiers enclosed in double quotes are also case-sensitive. For more details, see Identifier requirements.
select_statementrequiredSpecifies the query used to create the view. This query serves as the text/definition for the view. This query is displayed in the output of SHOW VIEWS and SHOW MATERIALIZED VIEWS. There are limitations on the select_statement. For details, see: Usage notes. Limitations on Creating Materialized Views.
column_listoptionalIf you do not want the column names in the view to be the same as the column names of the underlying table, you may include a column list in which you specify the column names. (You do not need to specify the data types of the columns.) If you include a CLUSTER BY clause for the materialized view, then you must include the column name list.
MASKING POLICY = policy_nameoptionalSpecifies the masking policy to set on a column.
USING ( col_name , cond_col_1 ... )optionalSpecifies the arguments to pass into the conditional masking policy SQL expression. The first column in the list specifies the column for the policy conditions to mask or tokenize the data and must match the column to which the masking policy is set. The additional columns specify the columns to evaluate to determine whether to mask or tokenize the data in each row of the query result when a query is made on the first column. If the USING clause is omitted, Snowflake treats the conditional masking policy as a normal masking policy.
PROJECTION POLICY policy_nameoptionalSpecifies the projection policy to set on a column.
string_literaloptionalSpecifies a comment for the view. The string literal should be in single quotes. (The string literal should not contain single quotes unless they are escaped.) Default: No value.
INTERACTIVEoptionalCreates an interactive materialized view, which is optimized for low-latency queries on interactive tables. An interactive materialized view must be based on a single interactive table. After you create the interactive materialized view, you must add both the materialized view and its underlying base table to the interactive warehouse. For more information, see Materialized view support for interactive tables. Default: No value (creates a standard materialized view)
SECUREoptionalSpecifies that the view is secure. For more information about secure views, see Working with Secure Views. Default: No value (view is not secure)
COPY GRANTSoptionalIf you are replacing an existing view by using the OR REPLACE clause, then the replacement view retains the access permissions from the original view. This parameter copies all privileges, except OWNERSHIP, from the existing view to the new view. The new view does not inherit any future grants defined for the object type in the schema. By default, the role that executes the CREATE MATERIALIZED VIEW statement owns the new view. If the parameter is not included in the CREATE VIEW statement, then the new view does not inherit any explicit access privileges granted on the original view but does inherit any future grants defined for the object type in the schema. Note that the operation to copy grants occurs atomically with the CREATE VIEW statement (i.e. within the same transaction). Default: No value (grants are not copied). You can also copy grants with the COPY = ( GRANTS ) parameter. For materialized views, COPY = ( ... ) accepts only GRANTS.
ROW ACCESS POLICY policy_name ON ( col_name [ , col_name ... ] )optionalSpecifies the row access policy to set on the materialized view.
AGGREGATION POLICY policy_nameoptionalSpecifies the aggregation policy to set on the materialized view.
expr#optionalSpecifies an expression on which to cluster the materialized view. Typically, each expression is the name of a column in the materialized view. For more information about clustering materialized views, see Materialized Views and Clustering. For more information about clustering in general, see What is Data Clustering?.
WITH DATA METRIC FUNCTION ( dmf_name ON ( col_name [ , col_name ... ] ) [ , dmf_binding ... ] )optionalAssociates one or more data metric functions (DMFs) with the materialized view at creation time. The DMF begins running on the schedule configured for the materialized view as soon as it is created. You can attach multiple DMF bindings by separating them with commas. Each binding accepts the same properties as ALTER TABLE … ADD DATA METRIC FUNCTION, including EXECUTE AS ROLE, ANOMALY_DETECTION, SENSITIVITY, DATA_QUALITY_NOTIFICATION, and EXPECTATION. For a description of each property, see Data metric function actions. Snowflake also accepts this clause without the parentheses around the binding list, but that form is deprecated and a future behavior change release removes it. For usage guidance and examples, see Attach DMFs at creation time. Default: No value (no DMF is associated with the materialized view)
TAG ( tag_name = 'tag_value' [ , tag_name = 'tag_value' , ... ] )optionalSpecifies the tag name and the tag string value. The tag value is always a string, and the maximum number of characters for the tag value is 256. For information about specifying tags in a statement, see Tag quotas.
WITH CONTACT ( purpose = contact [ , purpose = contact ...] )optionalAssociate the new object with one or more contacts. Specify the WITH CONTACT clause after all other clauses except the AS clause (if that clause is supported by this command).

Usage notes

See also

Referenced by