Skip to content

Commit 1dedffd

Browse files
committed
Materialized Views and refereshing one
1 parent ff4b0dc commit 1dedffd

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/************* Materialized Views **************/
2+
3+
/*
4+
if we check on Database > specific Database? Schema > Materialized Views >
5+
there may not be anything if don't have exsisting one.
6+
*/
7+
8+
-- Create new materialized view
9+
CREATE MATERIALIZED VIEW mv_staff AS(
10+
SELECT s.last_name, s.department, s.job_title, cr.company_regions
11+
FROM staff s
12+
JOIN company_regions cr ON cr.region_id = s.region_id
13+
);
14+
15+
16+
-- get data from materialized view
17+
SELECT * FROM mv_staff LIMIT 10;
18+
19+
20+
------------- Limitation of Materialized Views -----------
21+
/*
22+
If there is any changes to source table (in this case staff and company_regions),
23+
our materialzed views data won't be refelected automatically. So data will become stale over time.
24+
Thus, we need to refresh data on materialized view.
25+
*/
26+
27+
28+
------------ Refershing Materialized Views -----------------
29+
/*
30+
this will rebuild the entire table and repopulate the data.
31+
so we need to consider carefully how often we want to refresh it and may want to space out the frequency.
32+
*/
33+
34+
REFRESH MATERIALIZED VIEW mv_staff;
35+
36+
37+
38+
39+
40+

0 commit comments

Comments
 (0)