当前位置:   article > 正文

PostgreSQL/Greenplum数据库的物化视图_greenplum 物化视图

greenplum 物化视图

一、物化视图(MATERIALIZED VIEW)

1、PG的版本

PostgreSQL 9.3 版本之后开始支持物化视图。

2、与普通视图的区别

View 视图:虚拟,不存在实际的数据,在查询视图的时候其实是对视图内的表进行查询操作。

物化视图:实际存在,将数据存成一张表,查询的时候对这个表进行操作。物化视图内的数据需要和表的数据进行同步,这就是refresh。

二、实际操作

1、实验环境:

数据库版本:PostgreSQL 9.6.17 及 Greenplum 6.10.1

2、初始化数据

创建表,并插入数据:

  1. tangyibo=# create table t1 (id int ,col1 varchar(10),col2 varchar(10));
  2. CREATE TABLE
  3. tangyibo=# create table t2 (id int ,col3 varchar(10), col4 varchar(10), col5 varchar(10));
  4. CREATE TABLE
  5. tangyibo=# insert into t1 values (1,'a','b'),(2,'a','b'),(3,'a','b'),(4,'a','b'),(5,'a','b');
  6. INSERT 0 5
  7. 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');
  8. INSERT 0 5
  9. tangyibo=# select * from t1;
  10. id | col1 | col2
  11. ----+------+------
  12. 1 | a | b
  13. 2 | a | b
  14. 3 | a | b
  15. 4 | a | b
  16. 5 | a | b
  17. (5 rows)
  18. tangyibo=# select * from t2;
  19. id | col3 | col4 | col5
  20. ----+------+------+------
  21. 1 | c | d | e
  22. 2 | c | d | e
  23. 3 | c | d | e
  24. 4 | c | d | e
  25. 5 | c | d | e
  26. (5 rows)
  27. tangyibo=#

3、创建物化视图

  1. 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;
  2. SELECT 5
  3. tangyibo=# select * from mv_t1_t2;
  4. t1_id | t2_id | col1 | col2 | col3 | col4 | col5
  5. -------+-------+------+------+------+------+------
  6. 1 | 1 | a | b | c | d | e
  7. 2 | 2 | a | b | c | d | e
  8. 3 | 3 | a | b | c | d | e
  9. 4 | 4 | a | b | c | d | e
  10. 5 | 5 | a | b | c | d | e
  11. (5 rows)

注:在Greenplum中不支持IF NOT EXISTS;

4、刷新物化视图

  1. tangyibo=# insert into t1 values (11,'x','y');
  2. INSERT 0 1
  3. tangyibo=# insert into t2 values (11,'x','y','z');
  4. INSERT 0 1
  5. tangyibo=#

对表进行操作,不改变物化视图中的数据。查询物化视图,数据没有改变:

  1. tangyibo=# select * from mv_t1_t2 ;
  2. t1_id | t2_id | col1 | col2 | col3 | col4 | col5
  3. -------+-------+------+------+------+------+------
  4. 1 | 1 | a | b | c | d | e
  5. 2 | 2 | a | b | c | d | e
  6. 3 | 3 | a | b | c | d | e
  7. 4 | 4 | a | b | c | d | e
  8. 5 | 5 | a | b | c | d | e
  9. (5 rows)
  10. tangyibo=#

(1) 全量更新:

刷新物化视图才能使物化视图的数据改变。

  1. tangyibo=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH DATA;
  2. REFRESH MATERIALIZED VIEW
  3. tangyibo=# select * from mv_t1_t2 ;
  4. t1_id | t2_id | col1 | col2 | col3 | col4 | col5
  5. -------+-------+------+------+------+------+------
  6. 1 | 1 | a | b | c | d | e
  7. 2 | 2 | a | b | c | d | e
  8. 3 | 3 | a | b | c | d | e
  9. 4 | 4 | a | b | c | d | e
  10. 5 | 5 | a | b | c | d | e
  11. 11 | 11 | x | y | x | y | z
  12. (6 rows)
  13. tangyibo=#

(2) 增量更新

在PostgreSQL数据库只有当物化视图中存在unique index的时候,refresh物化视图才能使用增量更新,加入concurrently参数。否则报错。

  1. tangyibo=# REFRESH MATERIALIZED VIEW CONCURRENTLY mv_t1_t2 WITH DATA;
  2. ERROR: cannot refresh materialized view "public.mv_t1_t2" concurrently
  3. HINT: Create a unique index with no WHERE clause on one or more columns of the materialized view.
  4. tangyibo=# create unique index uidx_mv_id on mv_t1_t2 (t1_id );
  5. CREATE INDEX
  6. tangyibo=# insert into t1 values (12,'xx','yy');
  7. INSERT 0 1
  8. tangyibo=# insert into t2 values (12,'xx','yy','zz');
  9. INSERT 0 1
  10. tangyibo=# REFRESH MATERIALIZED VIEW CONCURRENTLY mv_t1_t2 WITH DATA;
  11. REFRESH MATERIALIZED VIEW
  12. tangyibo=# select * from mv_t1_t2 ;
  13. t1_id | t2_id | col1 | col2 | col3 | col4 | col5
  14. -------+-------+------+------+------+------+------
  15. 1 | 1 | a | b | c | d | e
  16. 2 | 2 | a | b | c | d | e
  17. 3 | 3 | a | b | c | d | e
  18. 4 | 4 | a | b | c | d | e
  19. 5 | 5 | a | b | c | d | e
  20. 11 | 11 | x | y | x | y | z
  21. 12 | 12 | xx | yy | xx | yy | zz
  22. (7 rows)
  23. tangyibo=#

在Greenplum数据库中,需要唯一索引与分布式键兼容,否在会报:UNIQUE and DISTRIBUTED RANDOMLY are incompatible

虽然在PostgreSQL数据库中可以用WITH NO DATA刷新物化视图,但是在查询时会报错:

  1. tangyibo=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH NO DATA;
  2. REFRESH MATERIALIZED VIEW
  3. tangyibo=# select * from mv_t1_t2 ;
  4. ERROR: materialized view "mv_t1_t2" has not been populated
  5. HINT: Use the REFRESH MATERIALIZED VIEW command.
  6. tangyibo=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH DATA;
  7. REFRESH MATERIALIZED VIEW
  8. tangyibo=# select * from mv_t1_t2 ;
  9. t1_id | t2_id | col1 | col2 | col3 | col4 | col5
  10. -------+-------+------+------+------+------+------
  11. 1 | 1 | a | b | c | d | e
  12. 2 | 2 | a | b | c | d | e
  13. 3 | 3 | a | b | c | d | e
  14. 4 | 4 | a | b | c | d | e
  15. 5 | 5 | a | b | c | d | e
  16. 11 | 11 | x | y | x | y | z
  17. 12 | 12 | xx | yy | xx | yy | zz
  18. (7 rows)
  19. tangyibo=#

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/601862
推荐阅读
相关标签
  

闽ICP备14008679号