CREATE MATERIALIZED VIEW
Optimizes read requests by allowing different partitioning and clustering columns than the base table and eliminates the need for individual write requests to multiple tables. When data is written to the base table, it is also automatically written to all associated materialized views.
Restriction:
-
Use all base table primary keys in the materialized view.
-
Multiple non-primary key columns from the base table are supported when the partition key is the same as in the base table, otherwise only a single non-primary key from the base table is allowed in the materialized view’s PRIMARY KEY.
-
Static columns are not supported.
-
Exclude rows with null values in the materialized view primary key column.
-
A materialized view cannot be created in a different keyspace from the base table. You also cannot create a materialized view in the system tables.
Synopsis
CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] [<keyspace_name>.]<view_name> AS SELECT [ (<column_list>) ] FROM [<keyspace_name>.]<table_name> [ WHERE <column_name> IS NOT NULL [ AND <column_name> IS NOT NULL ... ] ] [ AND <relation> [ AND ... ] ] PRIMARY KEY ( <column_list> ) [ WITH [ table_properties ] [ [ AND ] CLUSTERING ORDER BY (<cluster_column_name> DESC | ASC) ] ] ;
Syntax legend
Syntax conventions | Description |
---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
Set, list, map, or tuple.
Angle brackets ( |
|
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
Parameters
Parameter | Description | Default | ||
---|---|---|---|---|
|
Optional. Name of the keyspace that contains the table to index.
|
If no name is specified, the current keyspace is used. |
||
|
Name of the materialized view. Materialized view names can only contain alpha-numeric characters and underscores. The view name must begin with a number or letter and can be up to 49 characters long. |
|||
|
Comma-separated list of columns from the base table to include in the materialized view. Comma-separated list of columns from the base table to include in the materialized view. Static columns, even when specified, are not supported and not included in the materialized view. |
|||
|
Name of the base table. |
|||
|
Name of the column to include in the MV primary key. Test all such column names for null values. |
|||
|
Name of the column to use as a clustering column. |
Options
-
IS NOT NULL
: Test all columns for null values in theWHERE
clause. Separate each condition withAND
. Rows with null values in any column are not inserted into the materialized view table. -
IF NOT EXISTS
: Optional. Suppresses the error message when attempting to create a materialized view that already exists. Use to continue executing commands, such as aSOURCE
command. The option only validates that a materialized view with the same name exists; columns, primary keys, properties, and other settings can differ. -
AND relation
: Other relations that target the specific data needed. -
PRIMARY KEY ( column_list )
: Comma-separated list of columns used to partition and cluster the data. You can add non-primary key columns from the base table. Reorder the primary keys as needed to query the table more efficiently, including changing the partitioning and clustering keys.List the partition key first, followed by the clustering keys. Create a compound partition key by enclosing column names in parenthesis, for example:
PRIMARY KEY ( ( <PK_column1> [, <PK_column2>... ] ), <clustering_column1> [, <clustering_column2> ... ] )
-
<table_properties>
: Optional. Specify table properties if different than default. Separate table property definitions with an AND. See table properties.The base table properties are not copied.
-
Restriction
: Change log, CDC, is not available for materialized views. Not all table properties are available when creating a materialized view; for example,default_time_to_live
is not available.
Examples
Detailed examples can be found for each type of indexing:
MV type | Example links |
---|---|
Non-primary INT column used for MV primary key |
|
Non-primary DATE column used for MV primary key |
|
Non-primary TEXT column used for MV primary key |
|
Non-primary DATE column used for MV primary key, WHERE clause for another non-primary TEXT column |
|
Non-primary DATE column and non-primary key INT column used for MV primary key |
|
Non-primary DATE column and non-primary key TEXT column set with |
Create MV based on DATE column birthday and TEXT column country restricted with |