commit 3bf3ab8c563699138be02f9dc305b7b77a724307What is a materialized view? In short, this is a mutant of the table and the usual presentation. A view is a projection of data using a given relationship that does not have a repository. The table is ... table!
Date: Sunday, March 4, 18:23:31 2013 -0600
Posted by: Kevin Grittner
Added materialized views
')
A materialized view has a rule, just like a regular view, and a heap, as well as other physical properties, like a table. The rule is used only for filling the table, the links in the queries indicate the materialized data.
Implemented minimal functionality, but it can be useful in many cases. Currently, data is loaded only “on demand” with the CREATE MATERIALIZED VIEW and REFRESH MATERIALIZED VIEW instructions. It is expected that in future releases incremental data updates with different update time settings will be added, and the notion of “fresh” data will be given a clearer definition. At some point, even queries will be able to use the materialized data instead of the data of the tables themselves, but this requires the implementation of the above described functionality in the first place.
Most of the documentation work was done by Robert Haas. Review: Noah Misch, Thom Brown, Robert Haas, Marko Tiikkaja. A security review, including a decision on how to implement sepgsql, is expected from KaiGai Kohei.
postgres = # CREATE TABLE aa AS SELECT generate_series (1,1000000) AS a;
SELECT 1,000,000
postgres = # CREATE VIEW aav AS SELECT * FROM aa WHERE a <= 500000;
CREATE VIEW
postgres = # CREATE MATERIALIZED VIEW aam AS SELECT * FROM aa WHERE a <= 500000;
SELECT 500000
postgres = # SELECT pg_relation_size ('aa') AS tab_size, pg_relation_size ('aav') AS view_size, pg_relation_size ('aam') AS matview_size;
tab_size | view_size | matview_size
---------- + ----------- + --------------
36249600 | 0 | 18137088
( 1 row )
postgres = # DELETE FROM aa WHERE a <= 500000;
DELETE 500,000
postgres = # SELECT count (*) FROM aam;
count
- 500,000
( 1 row )
postgres = # REFRESH MATERIALIZED VIEW aam;
REFRESH MATERIALIZED VIEW
postgres = # SELECT count (*) FROM aam;
count
- 0
( 1 row )
postgres = # REFRESH MATERIALIZED VIEW aam WITH NO DATA;
REFRESH MATERIALIZED VIEW
postgres = # SELECT count (*) FROM aam;
ERROR: materialized view "aam" has not been populated
HINT: Use the REFRESH MATERIALIZED VIEW command.
postgres = # SELECT matviewname, isscannable FROM pg_matviews;
matviewname | isscannable
------------- + -------------
aam | f
( 1 row )
postgres = # INSERT INTO aam VALUES (1);
ERROR: cannot change materialized view "aam"
postgres = # UPDATE aam SET a = 5;
ERROR: cannot change materialized view "aam"
postgres = # DELETE FROM aam;
ERROR: cannot change materialized view "aam"
postgres = # EXPLAIN ANALYZE SELECT * FROM aam WHERE a = 1;
QUERY PLAN
-------------------------------------------------- ------------------------------------------------
Seq Scan on aam ( cost = 0.00..8464.00 rows = 1 width = 4 ) ( actual time = 0.060..155.934 rows = 1 loops = 1 )
Filter: ( a = 1 )
Rows Removed by Filter: 499999
Total runtime: 156.047 ms
( 4 rows )
postgres = # CREATE INDEX aam_ind ON aam (a);
CREATE INDEX
postgres = # EXPLAIN ANALYZE SELECT * FROM aam WHERE a = 1;
QUERY PLAN
-------------------------------------------------- -------------------------------------------------- --------------
Index Only Scan using aam_ind on aam ( cost = 0.42..8.44 rows = 1 width = 4 ) ( actual time = 2.096..2.101 rows = 1 loops = 1 )
Index Cond: ( a = 1 )
Heap Fetches: 1
Total runtime: 2.196 ms
( 4 rows )
postgres = # INSERT INTO bb VALUES (generate_series (1,100000));
INSERT 0 100000
postgres = # EXPLAIN ANALYZE SELECT * FROM bb WHERE a = 1;
QUERY PLAN
-------------------------------------------------- -------------------------------------------------- -------------
Index Only Scan using bb_pkey on bb ( cost = 0.29..8.31 rows = 1 width = 4 ) ( actual time = 0.078..0.080 rows = 1 loops = 1 )
Index Cond: ( a = 1 )
Heap Fetches: 1
Total runtime: 0.159 ms
( 4 rows )
postgres = # CREATE MATERIALIZED VIEW bbm AS SELECT * FROM bb;
SELECT 100000
postgres = # EXPLAIN ANALYZE SELECT * FROM bbm WHERE a = 1;
QUERY PLAN
-------------------------------------------------- -------------------------------------------------
Seq Scan on bbm ( cost = 0.00..1776.00 rows = 533 width = 4 ) ( actual time = 0.144..41.873 rows = 1 loops = 1 )
Filter: ( a = 1 )
Rows Removed by Filter: 99999
Total runtime: 41.935 ms
( 4 rows )
Source: https://habr.com/ru/post/188802/
All Articles