Creates a materialized view.
1 2 3 4 5 6 7 | CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] materialized_view_name [ ( column_name [, ...] ) ] [ BUILD { DEFERRED | IMMEDIATE } ] [ REFRESH [ COMPLETE ] [ ON DEMAND ] [ [ START WITH (timestamptz) ] | [ EVERY (interval) ] ] ] [ { ENABLE | DISABLE } QUERY REWRITE ] [ WITH ( {storage_parameter = value} [, ... ] ) ] [ DISTRIBUTE BY { REPLICATION | ROUNDROBIN | { HASH ( column_name [,...] ) } } ] AS query; |
1 2 3 4 5 6 7 | CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] materialized_view_name [ ( column_name [, ...] ) ] [ BUILD IMMEDIATE ] REFRESH AUTO ENABLE QUERY REWRITE [ WITH ( {storage_parameter = value} [, ... ] ) ] AS query; |
IMMEDIATE indicates that the latest data is included when the materialized view is created.
DEFERRED indicates that data is included only when the materialized view is refreshed for the first time.
Specifies the refresh mode of a materialized view.
After a materialized view is created, the data in the materialized view reflects only the status of the base table at the creation time. When the data in the base table changes, you need to refresh the materialized view (REFRESH MATERIALIZED VIEW) to update the data in the materialized view.
ON DEMAND: manual refresh on demand.
START WITH (timestamptz) | EVERY (interval): scheduled refresh. START WITH specifies the first refresh time and EVERY specifies the refresh interval. The value can be MONTH, DAY, HOUR, MINUTE, or SECOND.
Specifies the refresh method of a materialized view.
Indicates whether query rewriting is supported. By default, query rewriting is not supported.
When ENABLE QUERY REWRITE is specified, the GUC parameter mv_rewrite_rule must be set to enable query rewriting for materialized views.
Query rewriting means that when a base table is queried, if a materialized view is created for the base table, the database system automatically determines whether the pre-calculated result in the materialized view can be used to process the query.
If a materialized view can be used, the pre-calculated result is directly read from the materialized view to accelerate the query.
Specifies the storage mode (row-store, column-store) for table data. This parameter cannot be modified once it is set.
ROW applies to OLTP service, which has many interactive transactions. An interaction involves many columns in the table. Using ROW can improve the efficiency.
COLUMN applies to the data warehouse service, which has a large amount of aggregation computing, and involves a few column operations.
Specifies whether to allow query rewriting on materialized views that contain foreign tables. This parameter must be used together with ENABLE QUERY REWRITE.
The materialized view cannot detect the data changes in the foreign table. Specify this option if you want to enable query rewriting for materialized views that contain foreign tables.
Value range:
Default value: off
The bitmap index is only applicable to the hstore_opt table. To use it, enable the table-level parameter enable_hstore_opt and set bitmap_columns to the specified column. This is supported only by clusters of version 9.1.0.200 or later.
Specifies the number of level-2 partitions in a column-store table. This parameter applies only to H-Store column-store tables. This is supported only by clusters of version 9.1.0.200 or later.
Value range: 1 to 32
Default value: 8
When the enable_hstore_opt table-level parameter is enabled, the enable_hstore table-level parameter is also automatically enabled by default. This is supported only by clusters of version 9.1.0.200 or later.
Default value: false
Determines whether to create a turbo table (column-store tables). The parameter is only valid for column-store tables. This is supported only by clusters of version 9.1.0.200 or later.
Default value: off
Determines the automatic analysis method for materialized views. This is supported only by clusters of version 9.1.0.200 or later.
Value range:
none: indicates that the materialized view does not automatically execute ANALYZE after being refreshed.
light: indicates that the materialized view performs light analysis after being refreshed.
Default value: light
Specifies a partial clustering storage for a materialized view. During the data import process to a column-store table, the data is partially sorted according to the specified column(s). This is supported only by clusters of version 9.1.0.200 or later.
Enable mv_pck_column and set it to a specified column.
Enables the use of function attributes in the query statements when creating materialized views. This is supported only by clusters of version 9.1.0.200 or later.
Value range:
stable: indicates that functions of the STABLE and IMMUTABLE types can be used in query statements.
volatile: indicates that functions of the VOLATILE, STABLE, and IMMUTABLE types can be used in query statements.
Default value: empty
Specifies that the materialized view will not be invalidated when there are data changes in the underlying base table. This is supported only by clusters of version 9.1.0.200 or later.
Set excluded_inactive_tables to schemaName1.tableName1,schemaName2.tableName2.
Default value: empty
Allows query rewrite within a specified time interval after refresh, regardless of the freshness of the data in the materialized view. This is supported only by clusters of version 9.1.0.200 or later.
The unit is second. The default value is 0.
Specifies a tablespace for V3 storage. If default_tablespace is empty, the default tablespace of the database is used. This is supported only by clusters of version 9.1.0.200 or later.
Specifies how the table is distributed or replicated between DNs.
Value range:
Default value: determined by the default_distribution_mode parameter.
When the materialized view is distributed in hash mode, data skew may occur. To check for data skews in materialized views, follow the same procedures used for detecting data skews in regular tables. For details, see "Checking for Data Skew" in the Data Warehouse Service (DWS) Developer Guide. If data skew is identified in a materialized view, perform data skew optimization at the storage layer by referring to "Optimizing Data Skew" in the Data Warehouse Service (DWS) Developer Guide.
Creates a materialized view based on the query result.
Create a base table and insert data into the base table.
1 2 | CREATE TABLE t1 (a int, b int) DISTRIBUTE BY HASH(a); INSERT INTO t1 SELECT x,x FROM generate_series(1,10) x; |
Create a materialized view with the default option BUILD IMMEDIATE.
1 | CREATE MATERIALIZED VIEW mv1 AS SELECT * FROM t1; |
Create a materialized view in column-store.
1 | CREATE MATERIALIZED VIEW mv2 WITH(orientation = column) AS SELECT * FROM t1; |
Create a materialized view that is manually refreshed as required.
1 | CREATE MATERIALIZED VIEW mv3 BUILD DEFERRED REFRESH ON DEMAND AS SELECT * FROM t1; |
Create a materialized view with a scheduled refresh time.
1 | CREATE MATERIALIZED VIEW mv4 BUILD DEFERRED REFRESH START WITH(trunc(sysdate)) EVERY (interval '1 day') AS SELECT * FROM t1; |
Create a materialized view with a bitmap index.
1 2 | CREATE MATERIALIZED VIEW mv1 with (ORIENTATION = COLUMN, enable_hstore=true, enable_hstore_opt=on, bitmap_columns='col1') AS SELECT * FROM base_table; |
Create a materialized view and specify the number of level-2 partitions in a column-store table.
1 2 | CREATE MATERIALIZED VIEW mv WITH (ORIENTATION=COLUMN, ENABLE_HSTORE=ON, enable_hstore_opt=on, mv_pck_column='c3', secondary_part_column = 'c2', secondary_part_num = 8) AS SELECT * FROM base_table; |
Create a materialized view and specify the PCK column for sorting.
1 2 | CREATE MATERIALIZED VIEW mv WITH (ORIENTATION=COLUMN, ENABLE_HSTORE=ON, enable_hstore_opt=on, mv_pck_column='col3') AS SELECT * FROM base_table; |
Create a materialized view and specify the analysis method.
1 | CREATE MATERIALIZED VIEW mv1 enable query rewrite with(excluded_inactive_tables='matview_basic."T1",matview_basic."a=b"',mv_analyze_mode='none') as SELECT * FROM base_table; |
Create a V3 materialized view.
1 2 3 | CREATE MATERIALIZED VIEW mv1 with (orientation=column, enable_hstore=true, compression=low, enable_hstore_opt=on, COLVERSION = 3.0) TABLESPACE cu_obs_tbs distribute by hash(scope_name) AS SELECT * FROM dicttbl_low; |
Create a materialized view that contains a foreign table for query rewriting.
1 | CREATE MATERIALIZED VIEW mv1 with (enable_foreign_table_query_rewrite = true) as SELECT * FROM base_table; |
Create a materialized view and specify that volatile functions can be used in the query statement.
1 | CREATE MATERIALIZED VIEW mv_date with(mv_support_function_type = 'volatile') as select to_date(a) from t_date; |