francs=# select version(); version ---------------------------------------------------------------------------------------------------------- PostgreSQL 9.3devel on i686-pc-linux-gnu, compiled by gcc (GCC) 4.4.620110731 (Red Hat 4.4.6-3), 32-bit (1 row) francs=> create table test_1 (id int4,name character varying(32)); CREATE TABLE francs=> insert into test_1 select generate_series(1,10),generate_series(1,10)||'a'; INSERT 010 francs=> select * from test_1; id | name ----+------ 1 | 1a 2 | 2a 3 | 3a 4 | 4a 5 | 5a 6 | 6a 7 | 7a 8 | 8a 9 | 9a 10 | 10a (10 rows)
1,2 创建普通视图和物化视图
1 2 3 4 5
francs=> createview v_test_1 asselect * from test_1 where pg_sleep(5) isnull; CREATEVIEW francs=> creatematerializedview mv_test_1 asselect * from test_1 where id > 5; SELECT5
1.3 查看PLAN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
francs=> explain analyze select * From v_test_1; QUERY PLAN ---------------------------------------------------------------------------------------------------- Seq Scan on test_1 (cost=0.00..18.75 rows=233 width=86) (actual time=0.032..0.041 rows=5 loops=1) Filter: (id > 5) Rows Removed by Filter: 5 Total runtime: 0.153 ms (4 rows) francs=> explain analyze select * from mv_test_1; QUERY PLAN ------------------------------------------------------------------------------------------------------- Seq Scan on mv_test_1 (cost=0.00..17.00 rows=700 width=86) (actual time=0.222..0.231 rows=5 loops=1) Total runtime: 0.296 ms (2 rows)
francs=> /h refresh materialized view Command: REFRESH MATERIALIZED VIEW Description: replace the contents of a materialized view Syntax: REFRESH MATERIALIZED VIEW name [ WITH [ NO ] DATA ]