Can you describe the components of a dynamic table definition, including the SQL statement and refresh schedule?
A dynamic table definition in Snowflake consists of two main components:
-
SQL Statement: This is the heart of a dynamic table, defining the logic for transforming and filtering the source data. It's written in standard SQL and can include joins, aggregations, filtering clauses, and other transformations. This query essentially defines the structure and content of the resulting dynamic table.
-
Refresh Schedule: This determines how often Snowflake refreshes the dynamic table to reflect changes in the underlying data sources. You can specify various options for scheduling, including:
- Continuous:Â The table updates continuously as changes occur in the source data (useful for real-time scenarios).
- Scheduled:Â You define a specific schedule for automatic refreshes (e.g., hourly, daily).
- Manual:Â You trigger the refresh manually whenever needed.
Optional Components:
- Retention Period:Â You can define a time frame for how long Snowflake keeps historical versions of the dynamic table data.
- Clustering Key:Â This helps Snowflake optimize query performance by clustering data based on frequently used columns.
Here's an example of a dynamic table definition with all the components:
CREATE OR REPLACE DYNAMIC TABLE sales_analysis
REFRESH => ON DEMAND -- Manual refresh
AS
SELECT
customer_id,
SUM(order_amount) AS total_sales,
AVG(discount_rate) AS average_discount
FROM orders
GROUP BY customer_id;
In this example:
- The SQL statement calculates total sales and average discount per customer.
- The refresh schedule is set to manual, meaning you'll need to trigger the refresh yourself.
Overall, a dynamic table definition provides a concise and declarative way to define how you want your transformed data to look, along with how often it should be updated.