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;
CREATE UNIQUE INDEX sales_summary_seller
ON sales_summary (seller_no, invoice_date);
这个物化视图可能有助于显示为销售人员创建的仪表板中的图表。可以使用以下 SQL 语句安排一个作业来更新统计数据,每个晚上执行一次
REFRESH MATERIALIZED VIEW sales_summary;
物化视图的另一个用途是允许通过外部数据封装器(foreign data wrapper)更快地访问从远程系统带来的数据。下面是一个使用 file_fdw 的简单示例,附带了计时信息,但由于这使用了本地系统的缓存,因此与访问远程系统相比,性能差异通常会比这里显示的更大。请注意,我们还利用了在物化视图上建立索引的能力,而 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)
使用 EXPLAIN ANALYZE,我们看到
Aggregate (cost=21763.99..21764.00 rows=1 width=0) (actual time=188.180..188.181 rows=1.00 loops=1)
-> Foreign Scan on words (cost=0.00..21761.41 rows=1032 width=0) (actual time=188.177..188.177 rows=0.00 loops=1)
Filter: (word = 'caterpiler'::text)
Rows Removed by Filter: 479829
Foreign File: /usr/share/dict/words
Foreign File Size: 4953699
Planning time: 0.118 ms
Execution time: 188.273 ms
如果改为使用物化视图,查询会快得多
Aggregate (cost=4.44..4.45 rows=1 width=0) (actual time=0.042..0.042 rows=1.00 loops=1)
-> Index Only Scan using wrd_word on wrd (cost=0.42..4.44 rows=1 width=0) (actual time=0.039..0.039 rows=0.00 loops=1)
Index Cond: (word = 'caterpiler'::text)
Heap Fetches: 0
Index Searches: 1
Planning time: 0.164 ms
Execution time: 0.117 ms
无论哪种方式,这个词都拼错了,所以让我们来看看我们可能想要什么。再次使用 file_fdw 和 pg_trgm
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=11583.61..11583.64 rows=10 width=32) (actual time=1431.591..1431.594 rows=10.00 loops=1)
-> Sort (cost=11583.61..11804.76 rows=88459 width=32) (actual time=1431.589..1431.591 rows=10.00 loops=1)
Sort Key: ((word <-> 'caterpiler'::text))
Sort Method: top-N heapsort Memory: 25kB
-> Foreign Scan on words (cost=0.00..9672.05 rows=88459 width=32) (actual time=0.057..1286.455 rows=479829.00 loops=1)
Foreign File: /usr/share/dict/words
Foreign File Size: 4953699
Planning time: 0.128 ms
Execution time: 1431.679 ms
使用物化视图
Limit (cost=0.29..1.06 rows=10 width=10) (actual time=187.222..188.257 rows=10.00 loops=1)
-> Index Scan using wrd_trgm on wrd (cost=0.29..37020.87 rows=479829 width=10) (actual time=187.219..188.252 rows=10.00 loops=1)
Order By: (word <-> 'caterpiler'::text)
Index Searches: 1
Planning time: 0.196 ms
Execution time: 198.640 ms
如果您可以容忍远程数据周期性地更新到本地数据库,性能上的好处可能是显着的。
如果您在文档中看到任何不正确、与您使用该特定功能时的经验不符或需要进一步说明的内容,请使用此表单报告文档问题。