Postgresql - MATERIALIZED VIEW


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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM