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
版权声明:本文为博主原创文章,转载请附上博文链接!

Postgresql - MATERIALIZED VIEW的更多相关文章

  1. PostgreSQL物化视图(materialized view)

    1.创建视图 CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] table_name [ (column_name [, ...] ) ] [ WITH ( sto ...

  2. PostgreSQL rule view materialized view examples

    warehouse_db=# create table tab_view(emp_id int not null,emp_name varchar(10),emp_city varchar(10)); ...

  3. [terry笔记]物化视图 materialized view基础学习

    一.物化视图定义摘录:     物化视图是包括一个查询结果的数据库对像(由系统实现定期刷新数据),物化视图不是在使用时才读取,而是预先计算并保存表连接或聚集等耗时较多的操作结果,这样在查询时大大提高了 ...

  4. MATERIALIZED VIEW

    Oracle的实体化视图提供了强大的功能,可以用在不同的环境中,实体化视图和表一样可以直接进行查询.实体化视图可以基于分区表,实体化视图本身也可以分区. 主要用于预先计算并保存表连接或聚集等耗时较多的 ...

  5. Advanced Replication同步复制实验(基于Trigger&基于Materialized View)

    1. 高级复制和流复制介绍 1.1 高级复制(Advanced Replication) 高级复制也称为对称复制,分为多主体站点复制(Multiple Master Rplication).物化视图站 ...

  6. Materialized View in Oracle - Concepts and Architecture

    List all of MV inoracle: select owner, query, query_len from dba_mviews See content of aMV: select * ...

  7. 物化视图(materialized view) 实现数据迁移、数据定时同步

    近日公司有一个9i 的Oracle数据库,运行效率低下.想要将其升级到11G. 但是升级之前 要将数据进行同步,好在表不是很多.只有三张表.业务压力也不大,就想到了使用物 化视图的方式将数据同步过来. ...

  8. Materialized View模式

    Materialized-View模式是在要求数据格式不利于查询操作的情况下,根据多个数据仓库的数据生成预生成的视图的一种模式.这种模式可以帮助支持高效的查询和数据提取,提高应用程序的性能. 问题 在 ...

  9. ora-904 rowid create materialized view

    create materialized view t_v asselect t1.*,1 as marker,rowid from t1 t1union allselect t2.*,2 as mar ...

随机推荐

  1. laravel 的安装与配置

    1.工作环境 php 7.0+ .MySQL5.1+ 这里可以用开发环境包一键安装: 自己用的是wamp(windows)http://www.wampserver.com/en/ linux系统和m ...

  2. sql语句查询结果中添加自增列

    SELECT Row_Number() over ( order by getdate() ) as init , * FROM 表名

  3. 二零一八阿里p7笔试116题

    1. junit用法,before,beforeClass,after, afterClass的执行顺序 2. 分布式锁 3. nginx的请求转发算法,如何配置根据权重转发 4. 用hashmap实 ...

  4. Image Processing and Analysis_8_Edge Detection:Local Scale Control for Edge Detection and Blur Estimation——1998

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  5. sql/pl 安装并连接Oracle数据库

    1,首先,先下载pl/sql devloper 安装包.下载对应版本的安装包 下载地址  https://www.allroundautomations.com/bodyplsqldevreg.htm ...

  6. Iterator 和 ListIterator 有什么区别?(未完成)

    Iterator 和 ListIterator 有什么区别?(未完成)

  7. 【转】Golang汇编命令解读

    原文: https://www.cnblogs.com/yjf512/p/6132868.html ------------------------------------------------- ...

  8. Pycharm----显示tab制表符

    设置前: 设置后: 操作方法:

  9. BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)

    题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...

  10. python自动华 (十一)

    Python自动化 [第十一篇]:Python进阶-RabbitMQ队列/Memcached/Redis  本节内容: RabbitMQ队列 Memcached Redis 1.  RabbitMQ ...