search
HomeDatabaseMysql TutorialPostgreSQL 9.3物化视图使用

物化视图在Oracle里面是很早就内置的一个功能,而PostgreSQL也很早就将功能代码做出来,方式是类似create table as....,只是一直

物化视图在Oracle里面是很早就内置的一个功能,而PostgreSQL也很早就将功能代码做出来,方式是类似create table as....,只是一直没有内置,9.3版本终于将此作为一个内置的功能点来使用,下面分享下最新版本的物化视图使用。

目前postgres9.3在官网上有4个安装包,分别是9.3.0(stable version)、9.3.0 beta1、9.3.0betal2和9.3.0rc版本(release candidate version),所以我们下载稳定的9.3.0 stable版本。

下载地址:
安装略。

一、语法

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 test_kenyon(id int,vname text); CREATE TABLE postgres=# insert into test_kenyon select generate_series(1,20),'kenyon good boy'||generate_series(1,20); INSERT 0 20 postgres=# select * from test_kenyon ; id | vname ----+------------------- 1 | kenyon good boy1 2 | kenyon good boy2 3 | kenyon good boy3 4 | kenyon good boy4 5 | kenyon good boy5 6 | kenyon good boy6 7 | kenyon good boy7 8 | kenyon good boy8 9 | kenyon good boy9 10 | kenyon good boy10 11 | kenyon good boy11 12 | kenyon good boy12 13 | kenyon good boy13 14 | kenyon good boy14 15 | kenyon good boy15 16 | kenyon good boy16 17 | kenyon good boy17 18 | kenyon good boy18 19 | kenyon good boy19 20 | kenyon good boy20 (20 rows)

2.创建物化视图

postgres=# create materialized view mv_test_kenyon as select * from test_kenyon where id > 10; SELECT 10 postgres=# select * from mv_test_kenyon; id | vname ----+------------------- 11 | kenyon good boy11 12 | kenyon good boy12 13 | kenyon good boy13 14 | kenyon good boy14 15 | kenyon good boy15 16 | kenyon good boy16 17 | kenyon good boy17 18 | kenyon good boy18 19 | kenyon good boy19 20 | kenyon good boy20 (10 rows) postgres=# \d+ List of relations Schema | Name | Type | Owner | Size | Description --------+----------------+-------------------+----------+-------+------------- public | mv_test_kenyon | materialized view | postgres | 16 kB | public | test_kenyon | table | postgres | 16 kB | (2 rows) postgres=# \d mv_test_kenyon Materialized view "public.mv_test_kenyon" Column | Type | Modifiers --------+---------+----------- id | integer | vname | text | --size有大小(默认空表是8kb,而这里是16kb)说明存储了数据,有相应的物理文件,并且有类似表的结构

3.物化视图更新

postgres=# insert into test_kenyon values(21,'bad boy'); INSERT 0 1 postgres=# insert into test_kenyon values(22,'bad boy2'); INSERT 0 1 postgres=# select * from test_kenyon where id>20; id | vname ----+---------- 21 | bad boy 22 | bad boy2 (2 rows) postgres=# select * from mv_test_kenyon where id>20; id | vname ----+------- (0 rows) --物化视图的数据没有刷新过来 --刷新物化视图数据 postgres=# refresh materialized view mv_test_kenyon; REFRESH MATERIALIZED VIEW postgres=# select * from mv_test_kenyon where id>20; id | vname ----+---------- 21 | bad boy 22 | bad boy2 (2 rows) --使用with no data刷新 postgres=# insert into test_kenyon values(32,'bad boy3'); INSERT 0 1 postgres=# select * from mv_test_kenyon where id>20; id | vname ----+---------- 21 | bad boy 22 | bad boy2 (2 rows) postgres=# refresh materialized view mv_test_kenyon with no data; REFRESH MATERIALIZED VIEW postgres=# \d+ List of relations Schema | Name | Type | Owner | Size | Description --------+----------------+-------------------+----------+------------+------------- public | mv_test_kenyon | materialized view | postgres | 8192 bytes | public | test_kenyon | table | postgres | 16 kB | (2 rows) postgres=# select * from mv_test_kenyon; ERROR: materialized view "mv_test_kenyon" 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_test_kenyon ; DROP MATERIALIZED VIEW postgres=# --如果有其他约束在物化视图上,需要加cascade来级联删除

四、应用场景和优劣势
可以将复杂的SQL写成视图来调用,并可增大数据的安全性
另外物化视图与普通视图比因为直接扫描数据,通常扫描的数据更少,在有索引的支持下,效率更高,,网络消耗也更少,特别是跨DB,跨服务器的查询
与普通视图相比的劣势是数据需要不定时地刷新才能获取到最实时的数据。

、总结
1.物化视图当前是全量刷新,暂不支持增量刷新
2.刷新参数with data是全量更新物化视图内容,且是默认参数;with no data会清除物化视图内容,释放物化视图所占的空间,并使物化视图不可用

六、参考:
http://wiki.postgresql.org/wiki/Materialized_Views

相关阅读:

PostgreSQL删除表中重复数据行

PostgreSQL数据库连接池PgBouncer的搭建

Windows平台编译 PostgreSQL

PostgreSQL备份心得笔记

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.