更新时间:2024-05-20 GMT+08:00

使用

语法格式

  • 创建增量物化视图
    CREATE INCREMENTAL MATERIALIZED VIEW [ view_name ] AS { query_block }; 
  • 全量刷新物化视图
    REFRESH MATERIALIZED VIEW [ view_name ];
  • 增量刷新物化视图
    REFRESH INCREMENTAL MATERIALIZED VIEW [ view_name ];
  • 删除物化视图
    DROP MATERIALIZED VIEW [ view_name ];
  • 查询物化视图
    SELECT * FROM [ view_name ];

示例

-- 准备数据
CREATE TABLE t1(c1 int, c2 int);
INSERT INTO t1 VALUES(1, 1);
INSERT INTO t1 VALUES(2, 2);

-- 创建增量物化视图
gaussdb=# CREATE INCREMENTAL MATERIALIZED VIEW mv AS SELECT * FROM t1;
CREATE MATERIALIZED VIEW

-- 插入数据
gaussdb=# INSERT INTO t1 VALUES(3, 3);
INSERT 0 1

-- 增量刷新物化视图
gaussdb=# REFRESH INCREMENTAL MATERIALIZED VIEW mv;
REFRESH MATERIALIZED VIEW

-- 查询物化视图结果
gaussdb=# SELECT * FROM mv;
 c1 | c2 
----+----
  1 |  1
  2 |  2
  3 |  3
(3 rows)

-- 插入数据
gaussdb=# INSERT INTO t1 VALUES(4, 4);
INSERT 0 1

-- 全量刷新物化视图
gaussdb=# REFRESH MATERIALIZED VIEW mv;
REFRESH MATERIALIZED VIEW

-- 查询物化视图结果
gaussdb=# select * from mv;
 c1 | c2 
----+----
  1 |  1
  2 |  2
  3 |  3
  4 |  4
(4 rows)

-- 删除物化视图
gaussdb=# DROP MATERIALIZED VIEW mv;
DROP MATERIALIZED VIEW