39.3. �ﻯ��ͼ

PostgreSQL�е��ﻯ��ͼ����ͼһ��ʹ���˹���ϵͳ��������һ���������ʽ�����˽�������ﻯ��ͼ��

CREATE MATERIALIZED VIEW mymatview AS SELECT * FROM mytab;

����ͼ��

CREATE TABLE mymatview AS SELECT * FROM mytab;

֮�����Ҫ�������ﻯ��ͼ����ֱ�ӱ����£��������ڴ����ﻯ��ͼ�IJ�ѯ�Ĵ洢��ʽ����ͼ��ѯ�Ĵ洢��ʽ��ȫ��ͬ�����ҪΪ�ﻯ��ͼ�������ʵ����ݣ�

REFRESH MATERIALIZED VIEW mymatview;

The information about a materialized view in the �й�һ��PostgreSQLϵͳĿ¼�е��ﻯ��ͼ����Ϣ��һ��������ͼ����Ϣ��ȫ��ͬ����˶��ڽ�������һ���ﻯ��ͼ����һ����ϵ������һ������һ����ͼ����һ���ﻯ��ͼ��һ����ѯ����ʱ������ֱ�Ӵ��ﻯ��ͼ�з��أ���ͬ��һ��������ֻ����������ﻯ��ͼ��

��Ȼ���ﻯ��ͼ�д洢�����ݵķ��ʳ���Ҫ����ֱ�ӷ��ʵײ����ͨ��һ����ͼ���ʣ��������ݲ����������µģ�����ijЩʱ�򲢲���Ҫ��ǰ���ݡ�����һ����¼��������ı���

CREATE TABLE invoice (
    invoice_no    integer        PRIMARY KEY,
    seller_no     integer,       -- ����Ա�� ID
    invoice_date  date,          -- ��������
    invoice_amt   numeric(13,2)  -- ������
);

�����������ٻ�����ʷ�������ݣ����ǿ���ϣ�����ܣ��������ǿ��ܲ������ĵ�ǰ���ڵIJ��������ݣ�

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);

����ﻯ��ͼ���ܶ���Ϊ����Ա�����Ŀ����������ʾһ��ͼ���dz����á�������һ���ƻ�������ÿ��ʹ����� 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

�������ַ�ʽ�����ʶ��DZ�ƴ���ģ���������ǿ���ʲô�����ǿ�����Ҫ�ġ��ٴ�ʹ��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=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

������ܹ����ܶ��ڰ�Զ�����ݸ��µ��������ݿ⣬��������������Ǿ޴�ġ�