(5, 'z', 11);
-- we want a view based on the table, too, since views present additional challenges
CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
-SELECT * FROM tv;
+SELECT * FROM tv ORDER BY type;
type | totamt
------+--------
+ x | 5
y | 12
z | 11
- x | 5
(3 rows)
-- create a materialized view with no data, and confirm correct behavior
-- create various views
EXPLAIN (costs off)
- CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
- QUERY PLAN
----------------------
- HashAggregate
- -> Seq Scan on t
-(2 rows)
+ CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
+ QUERY PLAN
+---------------------------
+ Sort
+ Sort Key: t.type
+ -> HashAggregate
+ -> Seq Scan on t
+(4 rows)
-CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
+CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
SELECT * FROM tvm;
type | totamt
------+--------
+ x | 5
y | 12
z | 11
- x | 5
(3 rows)
CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
View definition:
SELECT tv.type,
tv.totamt
- FROM tv;
+ FROM tv
+ ORDER BY tv.type;
\d+ tvm
Materialized view "public.tvm"
View definition:
SELECT tv.type,
tv.totamt
- FROM tv;
+ FROM tv
+ ORDER BY tv.type;
\d+ tvvm
Materialized view "public.tvvm"
View definition:
SELECT tv.type,
tv.totamt
- FROM tv;
+ FROM tv
+ ORDER BY tv.type;
-- modify the underlying table data
INSERT INTO t VALUES (6, 'z', 13);
(3 rows)
-- test join of mv and view
-SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type);
+SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
type | mtot | vtot
------+------+------
+ x | 5 | 5
y | 12 | 12
z | 24 | 24
- x | 5 | 5
(3 rows)
-- test diemv when the mv does exist
-- we want a view based on the table, too, since views present additional challenges
CREATE VIEW tv AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type;
-SELECT * FROM tv;
+SELECT * FROM tv ORDER BY type;
-- create a materialized view with no data, and confirm correct behavior
EXPLAIN (costs off)
-- create various views
EXPLAIN (costs off)
- CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
-CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv;
+ CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
+CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
SELECT * FROM tvm;
CREATE MATERIALIZED VIEW tmm AS SELECT sum(totamt) AS grandtot FROM tm;
CREATE MATERIALIZED VIEW tvmm AS SELECT sum(totamt) AS grandtot FROM tvm;
SELECT * FROM tum;
-- test join of mv and view
-SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type);
+SELECT type, m.totamt AS mtot, v.totamt AS vtot FROM tm m LEFT JOIN tv v USING (type) ORDER BY type;
-- test diemv when the mv does exist
DROP MATERIALIZED VIEW IF EXISTS tum;