Last active
July 11, 2022 12:08
-
-
Save robert8138/23b6d78e78a90423ab0a45df20d4a714 to your computer and use it in GitHub Desktop.
Demonstrate how to do incremental load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Not Recommended Approach: Scan the entire table and rebuild everyday | |
INSERT OVERWRITE TABLE dim_total_bookings PARTITION (ds = '{{ ds }}') | |
SELECT | |
dim_market | |
, SUM(m_bookings) AS m_bookings | |
FROM | |
fct_bookings | |
WHERE | |
ds <= '{{ ds }}' -- this is expensive, and can quickly run into scale issue | |
GROUP BY | |
dim_market | |
; | |
-- Recommended Approach: Incremental Load | |
INSERT OVERWRITE TABLE dim_total_bookings PARTITION (ds = '{{ ds }}') | |
SELECT | |
dim_market | |
, SUM(m_bookings) AS m_bookings | |
FROM ( | |
SELECT | |
dim_market | |
, m_bookings | |
FROM | |
dim_total_bookings -- a dim table | |
WHERE | |
ds = DATE_SUB('{{ ds }}', 1) -- from the previous ds | |
UNION | |
SELECT | |
dim_market | |
, SUM(m_bookings) AS m_bookings | |
FROM | |
fct_bookings -- a fct table | |
WHERE | |
ds = '{{ ds }}' -- from the current ds | |
GROUP BY | |
dim_market | |
) a | |
GROUP BY | |
dim_market | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Can you explain what is ds here and also can you share your datasets