Redrock Postgres 搜索 英文
版本: 9.3 / 9.4 / 9.5 / 9.6 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17

39.3. 物化视图 #

中的物化视图 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 loops=1)
   ->  Foreign Scan on words  (cost=0.00..21761.41 rows=1032 width=0) (actual time=188.177..188.177 rows=0 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 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 loops=1)
         Index Cond: (word = 'caterpiler'::text)
         Heap Fetches: 0
 Planning time: 0.164 ms
 Execution time: 0.117 ms

无论哪种方式,该单词的拼写都是错误的,因此我们来寻找我们想要的内容。再次使用 file_fdwpg_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 loops=1)
   ->  Sort  (cost=11583.61..11804.76 rows=88459 width=32) (actual time=1431.589..1431.591 rows=10 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 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 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 loops=1)
         Order By: (word <-> 'caterpiler'::text)
 Planning time: 0.196 ms
 Execution time: 198.640 ms

如果您能容忍远程数据定期更新到本地数据库,则性能优势可能是巨大的。