MATERIALIZED VIEW
PG 9.3 版本之后开始支持物化视图。
View 视图:
虚拟,不存在实际的数据,在查询视图的时候其实是对视图内的表进行查询操作。

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

实验环境:
CentOS 7
PG 10.4

操作实验:

初始化环境:
创建表,并插入数据
mytest=# create table t1 (id int ,col1 varchar(10),col2 varchar(10));
mytest=# create table t2 (id int ,col3 varchar(10), col4 varchar(10), col5 varchar(10));
mytest=# insert into t1 values (1,'a','b'); ......
mytest=# insert into t2 values (1,'c','d','e'); ......
mytest=# select * from t1;
id | col1 | col2
----+------+------
1 | a | b
2 | a | b
3 | a | b
4 | a | b
5 | a | b
(5 rows)

mytest=# 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)

创建物化视图:
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;

mytest=# 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)

刷新物化视图:
mytest=# insert into t1 values (11,'x','y');
mytest=# insert into t2 values (11,'x','y','z');
对表进行操作,不改变物化视图中的数据。查询物化视图,数据没有改变
mytest=# 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)
全量更新:
刷新物化视图才能使物化视图的数据改变。
mytest=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH DATA;
mytest=# 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)

增量更新
只有当物化视图中存在unique index的时候,refresh物化视图才能使用增量更新,加入concurrently参数。否则报错。
mytest=# 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.
mytest=# create unique index uidx_mv_id on mv_t1_t2 (t1_id );
mytest=# REFRESH MATERIALIZED VIEW CONCURRENTLY mv_t1_t2 WITH DATA;
mytest=# insert into t1 values (12,'xx','yy');
mytest=# insert into t2 values (12,'xx','yy','zz');
mytest=# REFRESH MATERIALIZED VIEW CONCURRENTLY mv_t1_t2 WITH DATA;
mytest=# 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)

物化视图刷新WITH NO DATA ,查询会报错
mytest=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH NO DATA;
mytest=# select * from mv_t1_t2 ;
ERROR: materialized view "mv_t1_t2" has not been populated
HINT: Use the REFRESH MATERIALIZED VIEW command.

mytest=# REFRESH MATERIALIZED VIEW mv_t1_t2 WITH DATA;
REFRESH MATERIALIZED VIEW
mytest=# 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)

---------------------
作者:Chuck_Chen1222
来源:CSDN
原文:https://blog.csdn.net/chuckchen1222/article/details/80847327
版权声明:本文为博主原创文章,转载请附上博文链接!

最新文章

  1. Android学习第三天-打包常用命令
  2. Android项目实战(二十五):Android studio 混淆+打包+验证是否成功
  3. C/C++ 网络库介绍
  4. NGUI之UILabel
  5. mysql删除重复记录语句,删除除了 id 号不同,其他都相同的学生冗余信息
  6. 动画原理——绘画API
  7. java设计模式之四建造者模式(Builder)
  8. hdu149850 years, 50 colors (多个最小顶点覆盖)
  9. 【WC2013】糖果公园 [树上莫队]
  10. 20165223《Java程序设计》第八周Java学习总结
  11. CSS------当内容超出div宽度后自动换行和限制文字不超出div宽度和高度
  12. Java 的对象和类
  13. Runtime-iOS运行时应用
  14. protobuf 协议 windows 下 C++ 环境搭建
  15. fail2ban的使用以及防暴力破解与邮件预警
  16. php filesize() 方法返回的文件大小异常
  17. 如何将程序添加到Windows桌面右键菜单
  18. [CODECHEF]RIN
  19. appium 自动化测试案例
  20. FOJ Problem 2253 Salty Fish

热门文章

  1. ios编程时常见问题总结
  2. SAP应用真的不性感么
  3. [LeetCode] 22. 括号生成 ☆☆☆(回溯)
  4. shell 数组介绍
  5. django中解决跨域问题
  6. *p 和p[i] 区别
  7. select函数的详细使用(C语言)
  8. 用Python做一个飞机大战游戏
  9. devops发展历程
  10. ZooKeeper介绍(转载)