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;
物化视图的另一种用途,是让通过外部数据包装器从远程系统获取的数据能够被更快地访问。下面给出一个使用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
如果你能够容忍定期把远程数据更新到本地数据库,那么性能收益可能非常可观。
如果您发现文档中有不正确的内容、与您使用特定功能的经验不符或需要进一步说明,请使用此表单来报告文档问题。