PostgreSQL 9.3 版本之后开始支持物化视图。
View 视图:虚拟,不存在实际的数据,在查询视图的时候其实是对视图内的表进行查询操作。
物化视图:实际存在,将数据存成一张表,查询的时候对这个表进行操作。物化视图内的数据需要和表的数据进行同步,这就是refresh。
数据库版本:PostgreSQL 9.6.17 及 Greenplum 6.10.1
创建表,并插入数据:
tangyibo=# create table t1 (id int ,col1 varchar(10),col2 varchar(10));
CREATE TABLE
tangyibo=# create table t2 (id int ,col3 varchar(10), col4 varchar(10), col5 varchar(10));
CREATE TABLE
tangyibo=# insert into t1 values (1,'a','b'),(2,'a','b'),(3,'a','b'),(4,'a','b'),(5,'a','b');
INSERT 0 5
tangyibo=# insert into t2 values (1,'c','d','e'),(2,'c','d','e'),(3,'c','d','e'),(4,'c','d','e'),(5,'c','d','e');
INSERT 0 5
tangyibo=# select * from t1;
id | col1 | col2
----+------+------
1 | a | b
2 | a | b
3 | a | b
4 | a | b
5 | a | b
(5 rows)
tangyibo=# select * from t2;
id | col3 | col4 | col5
----+------+------+------
1 | c | d | e
2 | c | d | e
3 | c | d | e
4 | c | d | e
5 | c | d | e
(5 rows)
tangyibo=#
tangyibo=# CREATE MATERIALIZED VIEW IF NOT EXISTS mv_t1_t2 (t1_id,t2_id, col1,col2,col3,col4,col5) AS SELECT t1.id, t2.id, t1.col1,t1.col2,t2.col3,t2.col4,t2.col5 from t1,t2 where t1.id = t2.id WITH DATA;
SELECT 5
tangyibo=# select * from mv_t1_t2;
t1_id | t2_id | col1 | col2 | col3 | col4 | col5
-------+-------+------+------+------+------+------
1 | 1 | a | b | c | d | e
2 | 2 | a | b | c | d | e
3 | 3 | a | b | c | d | e
4 | 4 | a | b | c | d | e
5 | 5 | a | b | c | d | e
(5 rows)
注:在Greenplum中不支持IF NOT EXISTS;
tangyibo=# insert into t1 values (11,'x','y');
INSERT 0 1
tangyibo=# insert into t2 values (11,'x','y','z');
INSERT 0 1
tangyibo=#
对表进行操作,不改变物化视图中的数据。查询物化视图,数据没有改变:
tangyibo=# select * from mv_t1_t2 ;
t1_id | t2_id | col1 | col2 | col3 | col4 | col5
-------+-------+------+------+------+------+------
1 | 1 | a | b | c | d | e
2 | 2 | a | b | c | d | e
3 | 3 | a | b | c | d | e
4 | 4 | a | b | c | d | e
5 | 5 | a | b | c | d | e
(5 rows)
tangyibo=#
(1) 全量更新:
刷新物化视图才能使物化视图的数据改变。
tangyibo=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH DATA;
REFRESH MATERIALIZED VIEW
tangyibo=# select * from mv_t1_t2 ;
t1_id | t2_id | col1 | col2 | col3 | col4 | col5
-------+-------+------+------+------+------+------
1 | 1 | a | b | c | d | e
2 | 2 | a | b | c | d | e
3 | 3 | a | b | c | d | e
4 | 4 | a | b | c | d | e
5 | 5 | a | b | c | d | e
11 | 11 | x | y | x | y | z
(6 rows)
tangyibo=#
(2) 增量更新
在PostgreSQL数据库只有当物化视图中存在unique index的时候,refresh物化视图才能使用增量更新,加入concurrently参数。否则报错。
tangyibo=# REFRESH MATERIALIZED VIEW CONCURRENTLY mv_t1_t2 WITH DATA;
ERROR: cannot refresh materialized view "public.mv_t1_t2" concurrently
HINT: Create a unique index with no WHERE clause on one or more columns of the materialized view.
tangyibo=# create unique index uidx_mv_id on mv_t1_t2 (t1_id );
CREATE INDEX
tangyibo=# insert into t1 values (12,'xx','yy');
INSERT 0 1
tangyibo=# insert into t2 values (12,'xx','yy','zz');
INSERT 0 1
tangyibo=# REFRESH MATERIALIZED VIEW CONCURRENTLY mv_t1_t2 WITH DATA;
REFRESH MATERIALIZED VIEW
tangyibo=# select * from mv_t1_t2 ;
t1_id | t2_id | col1 | col2 | col3 | col4 | col5
-------+-------+------+------+------+------+------
1 | 1 | a | b | c | d | e
2 | 2 | a | b | c | d | e
3 | 3 | a | b | c | d | e
4 | 4 | a | b | c | d | e
5 | 5 | a | b | c | d | e
11 | 11 | x | y | x | y | z
12 | 12 | xx | yy | xx | yy | zz
(7 rows)
tangyibo=#
在Greenplum数据库中,需要唯一索引与分布式键兼容,否在会报:UNIQUE and DISTRIBUTED RANDOMLY are incompatible
虽然在PostgreSQL数据库中可以用WITH NO DATA刷新物化视图,但是在查询时会报错:
tangyibo=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH NO DATA;
REFRESH MATERIALIZED VIEW
tangyibo=# select * from mv_t1_t2 ;
ERROR: materialized view "mv_t1_t2" has not been populated
HINT: Use the REFRESH MATERIALIZED VIEW command.
tangyibo=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH DATA;
REFRESH MATERIALIZED VIEW
tangyibo=# select * from mv_t1_t2 ;
t1_id | t2_id | col1 | col2 | col3 | col4 | col5
-------+-------+------+------+------+------+------
1 | 1 | a | b | c | d | e
2 | 2 | a | b | c | d | e
3 | 3 | a | b | c | d | e
4 | 4 | a | b | c | d | e
5 | 5 | a | b | c | d | e
11 | 11 | x | y | x | y | z
12 | 12 | xx | yy | xx | yy | zz
(7 rows)
tangyibo=#