创建、刷新、删除物化视图:
一、语法
CREATE MATERIALIZED VIEW table_name
[ (column_name [, ...] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ TABLESPACE tablespace_name ]
AS query
[ WITH [ NO ] DATA ]
二、说明
storage_parameter是存储参数,诸如填充因子(fillfactor)等,tablespace可以指定表空间,比较关键的是后面的as query with [no] data,后面示例描述
三、示例
1.创建基础表
[postgres@primary ~]$ psql
psql (9.3.0)
Type "help" for help.
postgres=# create table highgo_T(id int,vname text);
CREATE TABLE
postgres=# insert into highgo_T select generate_series(1,20),'highgo_value_'||generate_series(1,20);
INSERT 0 20
postgres=# select * from highgo_T ;
id | vname
----+-------------------
1 | highgo_value_1
2 | highgo_value_2
3 | highgo_value_3
4 | highgo_value_4
5 | highgo_value_5
6 | highgo_value_6
7 | highgo_value_7
8 | highgo_value_8
9 | highgo_value_9
10 | highgo_value_10
11 | highgo_value_11
12 | highgo_value_12
13 | highgo_value_13
14 | highgo_value_14
15 | highgo_value_15
16 | highgo_value_16
17 | highgo_value_17
18 | highgo_value_18
19 | highgo_value_19
20 | highgo_value_20
(20 rows)
2.创建物化视图
postgres=# create materialized view mv_highgo_T as select * from highgo_T where id > 10;
SELECT 10
postgres=# select * from mv_highgo_T;
id | vname
----+-------------------
11 | highgo_value_11
12 | highgo_value_12
13 | highgo_value_13
14 | highgo_value_14
15 | highgo_value_15
16 | highgo_value_16
17 | highgo_value_17
18 | highgo_value_18
19 | highgo_value_19
20 | highgo_value_20
(10 rows)
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------------+-------------------+----------+-------+-------------
public | mv_highgo_T | materialized view | postgres | 16 kB |
public | highgo_T | table | postgres | 16 kB |
(2 rows)
postgres=# \d mv_highgo_T
Materialized view "public.mv_highgo_T"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
vname | text |
--size有大小(默认空表是8kb,而这里是16kb)说明存储了数据,有相应的物理文件,并且有类似表的结构
--表和物化视图的文件地址
postgres=# select oid,pg_relation_filepath(oid),relpages from pg_class where relname = 'highgo_T';
oid | pg_relation_filepath | relpages
-------+----------------------+----------
16396 | base/12896/16428 | 0
(1 row)
postgres=# select oid,pg_relation_filepath(oid),relpages from pg_class where relname = 'mv_highgo_T';
oid | pg_relation_filepath | relpages
-------+----------------------+----------
16459 | base/12896/16459 | 0
(1 row)
3.物化视图更新
postgres=# insert into highgo_T values(21,'bad boy');
INSERT 0 1
postgres=# insert into highgo_T values(22,'bad boy2');
INSERT 0 1
postgres=# select * from highgo_T where id>20;
id | vname
----+----------
21 | bad boy
22 | bad boy2
(2 rows)
postgres=# select * from mv_highgo_T where id>20;
id | vname
----+-------
(0 rows)
postgres=# refresh materialized view mv_highgo_T;
REFRESH MATERIALIZED VIEW
postgres=# select * from mv_highgo_T where id>20;
id | vname
----+----------
21 | bad boy
22 | bad boy2
(2 rows)
--使用with no data刷新
postgres=# insert into highgo_T values(32,'bad boy3');
INSERT 0 1
postgres=# select * from mv_highgo_T where id>20;
id | vname
----+----------
21 | bad boy
22 | bad boy2
(2 rows)
postgres=# refresh materialized view mv_highgo_T with no data;
REFRESH MATERIALIZED VIEW
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------------+-------------------+----------+------------+-------------
public | mv_highgo_T | materialized view | postgres | 8192 bytes |
public | highgo_T | table | postgres | 16 kB |
(2 rows)
postgres=# select * from mv_highgo_T;
ERROR: materialized view "mv_highgo_T" has not been populated
HINT: Use the REFRESH MATERIALIZED VIEW command.
使用了with no data刷新后会导致物化视图里面的数据清除干净,并使物化视图不可用,如果需要继续使用,需要使用REFRESH MATERIALIZED VIEW view_name来恢复。
4.删除物化视图
postgres=# drop materialized view mv_highgo_T ;
DROP MATERIALIZED VIEW
postgres=#
--如果有其他约束在物化视图上,需要加cascade来级联删除
四、应用场景和优劣势
可以将复杂的SQL写成视图来调用,并可增大数据的安全性
另外物化视图与普通视图比因为直接扫描数据,通常扫描的数据更少,在有索引的支持下,效率更高,网络消耗也更少,特别是跨DB,跨服务器的查询
与普通视图相比的劣势是数据需要不定时地刷新才能获取到最实时的数据。
五 、总结
1.物化视图当前是全量刷新,暂不支持增量刷新
2.刷新参数with data是全量更新(replace)物化视图内容,且是默认参数;with no data会清除物化视图内容,释放物化视图所占的空间,并使物化视图不可用
一、语法
CREATE MATERIALIZED VIEW table_name
[ (column_name [, ...] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ TABLESPACE tablespace_name ]
AS query
[ WITH [ NO ] DATA ]
二、说明
storage_parameter是存储参数,诸如填充因子(fillfactor)等,tablespace可以指定表空间,比较关键的是后面的as query with [no] data,后面示例描述
三、示例
1.创建基础表
[postgres@primary ~]$ psql
psql (9.3.0)
Type "help" for help.
postgres=# create table highgo_T(id int,vname text);
CREATE TABLE
postgres=# insert into highgo_T select generate_series(1,20),'highgo_value_'||generate_series(1,20);
INSERT 0 20
postgres=# select * from highgo_T ;
id | vname
----+-------------------
1 | highgo_value_1
2 | highgo_value_2
3 | highgo_value_3
4 | highgo_value_4
5 | highgo_value_5
6 | highgo_value_6
7 | highgo_value_7
8 | highgo_value_8
9 | highgo_value_9
10 | highgo_value_10
11 | highgo_value_11
12 | highgo_value_12
13 | highgo_value_13
14 | highgo_value_14
15 | highgo_value_15
16 | highgo_value_16
17 | highgo_value_17
18 | highgo_value_18
19 | highgo_value_19
20 | highgo_value_20
(20 rows)
2.创建物化视图
postgres=# create materialized view mv_highgo_T as select * from highgo_T where id > 10;
SELECT 10
postgres=# select * from mv_highgo_T;
id | vname
----+-------------------
11 | highgo_value_11
12 | highgo_value_12
13 | highgo_value_13
14 | highgo_value_14
15 | highgo_value_15
16 | highgo_value_16
17 | highgo_value_17
18 | highgo_value_18
19 | highgo_value_19
20 | highgo_value_20
(10 rows)
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------------+-------------------+----------+-------+-------------
public | mv_highgo_T | materialized view | postgres | 16 kB |
public | highgo_T | table | postgres | 16 kB |
(2 rows)
postgres=# \d mv_highgo_T
Materialized view "public.mv_highgo_T"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
vname | text |
--size有大小(默认空表是8kb,而这里是16kb)说明存储了数据,有相应的物理文件,并且有类似表的结构
--表和物化视图的文件地址
postgres=# select oid,pg_relation_filepath(oid),relpages from pg_class where relname = 'highgo_T';
oid | pg_relation_filepath | relpages
-------+----------------------+----------
16396 | base/12896/16428 | 0
(1 row)
postgres=# select oid,pg_relation_filepath(oid),relpages from pg_class where relname = 'mv_highgo_T';
oid | pg_relation_filepath | relpages
-------+----------------------+----------
16459 | base/12896/16459 | 0
(1 row)
3.物化视图更新
postgres=# insert into highgo_T values(21,'bad boy');
INSERT 0 1
postgres=# insert into highgo_T values(22,'bad boy2');
INSERT 0 1
postgres=# select * from highgo_T where id>20;
id | vname
----+----------
21 | bad boy
22 | bad boy2
(2 rows)
postgres=# select * from mv_highgo_T where id>20;
id | vname
----+-------
(0 rows)
--物化视图的数据没有刷新过来
postgres=# refresh materialized view mv_highgo_T;
REFRESH MATERIALIZED VIEW
postgres=# select * from mv_highgo_T where id>20;
id | vname
----+----------
21 | bad boy
22 | bad boy2
(2 rows)
--使用with no data刷新
postgres=# insert into highgo_T values(32,'bad boy3');
INSERT 0 1
postgres=# select * from mv_highgo_T where id>20;
id | vname
----+----------
21 | bad boy
22 | bad boy2
(2 rows)
postgres=# refresh materialized view mv_highgo_T with no data;
REFRESH MATERIALIZED VIEW
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------------+-------------------+----------+------------+-------------
public | mv_highgo_T | materialized view | postgres | 8192 bytes |
public | highgo_T | table | postgres | 16 kB |
(2 rows)
postgres=# select * from mv_highgo_T;
ERROR: materialized view "mv_highgo_T" has not been populated
HINT: Use the REFRESH MATERIALIZED VIEW command.
使用了with no data刷新后会导致物化视图里面的数据清除干净,并使物化视图不可用,如果需要继续使用,需要使用REFRESH MATERIALIZED VIEW view_name来恢复。
4.删除物化视图
postgres=# drop materialized view mv_highgo_T ;
DROP MATERIALIZED VIEW
postgres=#
--如果有其他约束在物化视图上,需要加cascade来级联删除
四、应用场景和优劣势
可以将复杂的SQL写成视图来调用,并可增大数据的安全性
另外物化视图与普通视图比因为直接扫描数据,通常扫描的数据更少,在有索引的支持下,效率更高,网络消耗也更少,特别是跨DB,跨服务器的查询
与普通视图相比的劣势是数据需要不定时地刷新才能获取到最实时的数据。
五 、总结
1.物化视图当前是全量刷新,暂不支持增量刷新
2.刷新参数with data是全量更新(replace)物化视图内容,且是默认参数;with no data会清除物化视图内容,释放物化视图所占的空间,并使物化视图不可用