| PostgreSQL 9.3.4 文档 | ||||
|---|---|---|---|---|
| Prev | Up | Chapter 38. 规则系统 | Next | |
PostgreSQL中的物化视图像视图一样使用了规则系统,但是以一种类表的形式保留了结果。在物化视图:
CREATE MATERIALIZED VIEW mymatview AS SELECT * FROM mytab;
和视图:
CREATE TABLE mymatview AS SELECT * FROM mytab;
之间的主要区别是物化视图不能直接被更新,并且用于创建物化视图的查询的存储方式和视图查询的存储方式完全相同,因此要为物化视图生成新鲜的数据:
REFRESH MATERIALIZED VIEW mymatview;
The information about a materialized view in the 有关一个PostgreSQL系统目录中的物化视图的信息和一个表或视图的信息完全相同。因此对于解析器,一个物化视图就是一个关系,就像一个表或一个视图。当一个物化视图被一个查询引用时,数据直接从物化视图中返回,如同表一样;规则只被用来填充物化视图。
虽然对物化视图中存储的数据的访问常常要快于直接访问底层表或通过一个视图访问,但是数据并不总是最新的;但是某些时候并不需要当前数据。考虑一个记录销售情况的表:
CREATE TABLE invoice (
invoice_no integer PRIMARY KEY,
seller_no integer, -- 销售员的 ID
invoice_date date, -- 销售日期
invoice_amt numeric(13,2) -- 销售量
);如果人们想快速绘制历史销售数据,他们可能希望汇总,并且他们可能并不关心当前日期的不完整数据:
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 '/etc/dictionaries-common/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如果你能够忍受定期把远程数据更新到本地数据库,其性能收益可能是巨大的。