CREATE MATERIALIZED VIEW defines a query as a stored result set that is not updated each time the view is referenced.
Syntax
CREATE MATERIALIZED VIEW name
[build_clause] [create_mv_refresh]
AS subqueryWhere build_clause is:
BUILD { IMMEDIATE | DEFERRED }Where create_mv_refresh is:
REFRESH [COMPLETE] [ON DEMAND]Parameters
| Parameter | Description |
|---|---|
name | The name of the materialized view. Can be schema-qualified (for example, myschema.dept_30). Must be unique within the schema—no other view, table, sequence, or index can share this name. |
subquery | A SELECT statement that defines the contents of the view. |
BUILD IMMEDIATE | Populates the view immediately upon creation. Default. |
BUILD DEFERRED | Delays population until the first REFRESH operation. |
COMPLETE | On refresh, discards the current content and reloads the view by re-executing the defining query. |
ON DEMAND | Refreshes the view when explicitly triggered by calling DBMS_MVIEW or the REFRESH MATERIALIZED VIEW statement. Default. |
Usage notes
Materialized views are read-only.
INSERT,UPDATE, andDELETEoperations on a view are not allowed.Access to the underlying tables is governed by the permissions of the view owner. The querying user must also have permissions to call all functions used by the view.
For details on the
REFRESH MATERIALIZED VIEWstatement, see the PostgreSQL Core Documentation.
Examples
Filter rows from a single table
Create a view that stores only the rows for department 30, populated immediately:
CREATE MATERIALIZED VIEW dept_30
BUILD IMMEDIATE
AS SELECT * FROM emp WHERE deptno = 30;该文章对您有帮助吗?