Updated on 2024-05-20 GMT+08:00

Usage

Syntax

  • Create a complete-refresh materialized view.
    CREATE MATERIALIZED VIEW [ view_name ] AS { query_block }; 
  • Refresh a complete-refresh materialized view.
    REFRESH MATERIALIZED VIEW [ view_name ];
  • Drop a materialized view.
    DROP MATERIALIZED VIEW [ view_name ];
  • Query a materialized view.
    SELECT * FROM [ view_name ];

Examples

-- Prepare data.
CREATE TABLE t1(c1 int, c2 int);
INSERT INTO t1 VALUES(1, 1);
INSERT INTO t1 VALUES(2, 2);

-- Create a complete-refresh materialized view.
gaussdb=# CREATE MATERIALIZED VIEW mv AS select count(*) from t1;
CREATE MATERIALIZED VIEW

-- Query the materialized view result.
gaussdb=# SELECT * FROM mv;
 count 
-------
     2
(1 row)

-- Insert data into the base table in the materialized view again.
gaussdb=# INSERT INTO t1 VALUES(3, 3);

-- Completely refresh a complete-refresh materialized view.
gaussdb=# REFRESH MATERIALIZED VIEW mv;
REFRESH MATERIALIZED VIEW

-- Query the materialized view result.
gaussdb=# SELECT * FROM mv;
 count 
-------
     3
(1 row)

-- Drop a materialized view.
gaussdb=# DROP MATERIALIZED VIEW mv;
DROP MATERIALIZED VIEW