搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
不正经
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
一步步学习电子维修 二极管挡维修法_优利德万用表二极管档位丢失
2
kafka-主题创建(主题操作的命令)
3
浮点数四则运算与格式化输出_浮点数四则运算与格式化输出头歌
4
redis-exporter监控部署(k8s内)tensuns专用_tensuns监控
5
C语言中10的n次方的三种表示方法_c语言10的n次方怎么表示
6
3GPP-LTE Band11标准定义频点和信道(V17.3.0 (2022-09)_band11 信道
7
秒杀系统
8
springboot + nginx 项目无感知发版
9
认识WebSocket并搭建服务端_websocket服务器端搭建
10
C# 生成图形验证码
当前位置:
article
> 正文
Postgresql - MATERIALIZED VIEW_postgres materialized
作者:不正经 | 2024-05-21 09:13:09
赞
踩
postgres materialized
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)
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/w/不正经/article/detail/601824
推荐阅读
article
oracle
vw
sq 1,
Oracle
vs
PostgreSQL
Develop(20) - ...
Oracle
和
PostgreSQL
都提供了物化视图,但
Oracle
的功能显然比
PostgreSQL
要强大不少,特别是查询...
赞
踩
article
PostgreSQL
物化
视图
快速入门_
物化
视图
postgres
...
本文介绍
PostgreSQL
物化
视图
(materialized views) ,
物化
视图
用于存储物理的查询结果并可周...
赞
踩
article
《
PostgreSQL
开发指南》第32篇
物化
视图
_
postgresql
materialized
...
物化
视图
(Materialized View)是
PostgreSQL
提供的一个扩展功能,它是介于
视图
和表之间的一种对...
赞
踩
article
Postgresql -
MATERIALIZED
VIEW
...
MATERIALIZED
VIEW
PG 9.3 版本之后开始支持物化视图。View 视图:虚拟,不存在实际的数据,在查询视...
赞
踩
article
mysql
物化
视图
知乎_数据分析之
SQL
优化系列(三)---
Postgre
SQL
的materi...
参考文章:Materialized View模式blog.csdn.net
Postgre
SQL
物化
视图
(materia...
赞
踩
article
【
PostgreSQL
内核学习(二十四) —— (
ALTER
MATERIALIZED
VIEW
)】...
是一个SQL命令,用于修改一个已经存在的物化视图的定义或行为。物化视图是一种特殊的数据库对象,它存储了查询结果的实际数据...
赞
踩
article
PostgreSQL
物化
视图
相关操作,
materialized
view
不阻塞刷新_
refresh
...
PostgreSQL
物化
视图
相关操作1、创建一个名为`mv_memberdetail`的
物化
视图
2、创建
视图
索引3、刷新...
赞
踩
article
PostgreSQL
物化
视图
;
CREATE
MATERIALIZED
VIEW
;
REFRESH
M...
PostgreSQL
中的物化
视图
像
视图
一样使用了规则系统,但是以一种类表的形式保留了结果。在物化
视图
:
CREATE
MA...
赞
踩
article
postgreSQL
—物化视图_
refresh
meterialized
view
concurre...
物化视图是PostgreSQL中的一种特殊对象,用于存储查询结果的物理副本,并可以周期性地进行更新。与简单视图不同,物化...
赞
踩
article
PostgreSQL
之 异地物化视图(基于外部表)_
cannot
change
materializ...
首先在192.168.100.200上创建extensionpostgres=# create extension po...
赞
踩
article
2、
创建
视图
(
CREATE
VIEW
)...
创建
视图
是指在已经存在的 MySQL 数据库表上建立
视图
。
视图
可以建立在一张表中,也可以建立在多张表中。基本语法可以使用...
赞
踩
article
微信小
程序
中
video
全屏
遮罩
_
video
遮盖
cover
-
view
...
来实现,非
全屏
时支持同级遮罩可以直接同级使用。由于
video
的层级非常高,所以使用。
_
video
遮盖
cover
-v...
赞
踩
article
HDU 3668
View
Code
(
二重积分
)_
hdu3668
...
大体题意:求图中交叉圆柱体的体积!思路:真是充分反映了高数能力! 是真弱啊,,想了6,7个小时了,才想明白! = =T_...
赞
踩
article
u
niAPP
浙政钉
入门手册_
u
-
view
2.0
支持
浙政钉
小
程序
么...
运行钉钉小
程序
专有钉钉
浙政钉
前端 对接流程采坑记录_
u
-
view
2.0
支持
浙政钉
小
程序
么
u
-
view
2.0
支持浙...
赞
踩
article
【论文阅读】
Multi
-
view
Multi
-
instance
Multi
-
label
Active
...
多视图多实例多标签主动学习。_
multi
-
instance
multi
-
label
learning
multi
-inst...
赞
踩
article
【Android界面
实现
】Z
ListView
,一个最强大的刷新、加载、
滑动
删除的
ListView
控件...
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 我们接着上篇的文章说,在前...
赞
踩
相关标签
oracle vw sq 1
postgresql
数据库
物化视图
mysql 物化视图 知乎
sql
微信小程序
小程序
uni-app
前端
c语言
数学
C++
深度学习
计算机视觉
人工智能
控件
swipe
listview
自定义控件