pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
PostgreSQL中的物化视图和视图一样使用规则系统,但会以类似表的形式持久保存结果。下面两者之间的主要区别是:
CREATE MATERIALIZED VIEW mymatview AS SELECT * FROM mytab;
以及:
CREATE TABLE mymatview AS SELECT * FROM mytab;
物化视图之后不能被直接更新,并且用于创建物化视图的查询,其存储方式与视图查询的存储方式完全相同,因此可以通过下面的命令为物化视图生成新数据:
REFRESH MATERIALIZED VIEW mymatview;
在PostgreSQL系统目录中,物化视图的信息与表或视图的信息完全相同。因此,对于解析器来说,物化视图也是一种关系,就像表或视图一样。当查询引用物化视图时,数据会像从表中那样直接从物化视图返回;规则只用于填充物化视图。
访问物化视图中存储的数据,往往比直接或通过视图访问底层表快得多,但这些数据并不总是最新的;不过,有时并不需要最新数据。考虑以下记录销售情况的表:
CREATE TABLE invoice (
invoice_no integer PRIMARY KEY,
seller_no integer, -- ID of salesperson
invoice_date date, -- date of sale
invoice_amt numeric(13,2) -- amount of sale
);
如果希望快速绘制历史销售数据的图表,就可能需要汇总数据,而不必关心当天尚不完整的数据:
CREATE MATERIALIZED VIEW sales_summary AS
SELECT
seller_no,
invoice_date,
sum(invoice_amt)::numeric(13,2) as sales_amt
FROM invoice
WHERE invoice_date < CURRENT_DATE
GROUP BY
seller_no,
invoice_date
ORDER BY
seller_no,
invoice_date;
CREATE UNIQUE INDEX sales_summary_seller
ON sales_summary (seller_no, invoice_date);
这个物化视图可以用于在为销售人员创建的仪表板中显示图表。可以调度一个作业,每晚使用以下 SQL 语句更新统计数据:
REFRESH MATERIALIZED VIEW sales_summary;
物化视图的另一种用途,是让通过外部数据包装器从远程系统获取的数据能够被更快地访问。下面给出一个使用file_fdw的简单示例,并附带计时结果;不过由于这里使用的是本地系统缓存,因此与真正访问远程系统相比,性能差异通常会比这里展示的更大。 准备工作:
CREATE EXTENSION file_fdw; CREATE SERVER local_file FOREIGN DATA WRAPPER file_fdw; CREATE FOREIGN TABLE words (word text NOT NULL) SERVER local_file OPTIONS (filename '/usr/share/dict/words'); CREATE MATERIALIZED VIEW wrd AS SELECT * FROM words; CREATE UNIQUE INDEX wrd_word ON wrd (word); CREATE EXTENSION pg_trgm; CREATE INDEX wrd_trgm ON wrd USING gist (word gist_trgm_ops); VACUUM ANALYZE wrd;
现在来检查一个单词的拼写。直接使用 file_fdw:
SELECT count(*) FROM words WHERE word = 'caterpiler';
count
-------
0
(1 row)
执行计划为:
Aggregate (cost=4125.19..4125.20 rows=1 width=0) (actual time=26.013..26.014 rows=1 loops=1)
-> Foreign Scan on words (cost=0.00..4124.70 rows=196 width=0) (actual time=26.011..26.011 rows=0 loops=1)
Filter: (word = 'caterpiler'::text)
Rows Removed by Filter: 99171
Foreign File: /etc/dictionaries-common/words
Foreign File Size: 938848
Total runtime: 26.081 ms
如果改用物化视图,查询会快得多:
Aggregate (cost=4.44..4.45 rows=1 width=0) (actual time=0.074..0.074 rows=1 loops=1)
-> Index Only Scan using wrd_word on wrd (cost=0.42..4.44 rows=1 width=0) (actual time=0.071..0.071 rows=0 loops=1)
Index Cond: (word = 'caterpiler'::text)
Heap Fetches: 0
Total runtime: 0.119 ms
无论采用哪种方式,这个单词的拼写都是错误的,因此来找找我们可能想要的单词。再次使用 file_fdw:
SELECT word FROM words ORDER BY word <-> 'caterpiler' LIMIT 10;
word
---------------
cater
caterpillar
Caterpillar
caterpillars
caterpillar's
Caterpillar's
caterer
caterer's
caters
catered
(10 rows)
Limit (cost=2195.70..2195.72 rows=10 width=32) (actual time=218.904..218.906 rows=10 loops=1)
-> Sort (cost=2195.70..2237.61 rows=16765 width=32) (actual time=218.902..218.904 rows=10 loops=1)
Sort Key: ((word <-> 'caterpiler'::text))
Sort Method: top-N heapsort Memory: 25kB
-> Foreign Scan on words (cost=0.00..1833.41 rows=16765 width=32) (actual time=0.046..200.965 rows=99171 loops=1)
Foreign File: /etc/dictionaries-common/words
Foreign File Size: 938848
Total runtime: 218.966 ms
使用物化视图:
Limit (cost=0.28..1.02 rows=10 width=9) (actual time=24.916..25.079 rows=10 loops=1)
-> Index Scan using wrd_trgm on wrd (cost=0.28..7383.70 rows=99171 width=9) (actual time=24.914..25.076 rows=10 loops=1)
Order By: (word <-> 'caterpiler'::text)
Total runtime: 25.884 ms
如果能够接受将远程数据定期更新到本地数据库,性能收益可能相当可观。
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。