Snowflake Dynamic Tables: Automate Your Data Transformations
Dynamic Tables in Snowflake automate data transformation processes, eliminating the manual management of data pipelines. They allow you to define the desired outcomes of your transformations declaratively through SQL.
Benefits of Dynamic Tables
- Declarative Definitions: Simply define what you want your data outcome to be, without managing the specific transformation steps.
- Automatic Orchestration: Snowflake handles the orchestration of your data pipelines automatically, adjusting refresh cycles based on the specified data freshness needs.
- Performance Improvements: Incremental processing capabilities significantly enhance performance over traditional full-table refreshes.
- Flexible Update Mechanisms: Easily switch between batch and stream processing with minor alterations to your dynamic table settings.
- Enhanced Observability: Dynamic tables are fully manageable and observable through Snowsight, with options for extending monitoring through API integrations.
Usage Scenarios
Dynamic tables are particularly useful when:
- Automation of data dependency tracking and refresh scheduling is required.
- Simplicity is preferred over the complexity of manual streams and tasks.
- There is a need for direct query result materialization without the need for intermediate storage.
Comparison with Other Approaches
Dynamic tables offer a simpler and potentially more cost-efficient alternative to traditional data transformation methods involving manual streams and tasks.
Managing Dynamic Tables
- Adjusting Configurations: Change settings such as compute resources or refresh schedules to optimize performance and cost.
- Activity Control: Suspend or resume dynamic tables to align with operational demands or maintenance schedules.
- Deletion of Tables: Remove obsolete dynamic tables to free up resources.
_10-- Example to suspend and resume a dynamic table_10ALTER DYNAMIC TABLE sales_summary SUSPEND;_10_10ALTER DYNAMIC TABLE sales_summary RESUME;_10_10-- Adjusting the warehouse used by a dynamic table_10ALTER DYNAMIC TABLE sales_summary SET WAREHOUSE = 'larger_warehouse';
Monitoring Dynamic Tables
You can monitor dynamic tables in two ways:
- Using Snowsight: Check refresh status and configurations through the graphical interface of Snowsight.
- Information Schema Queries: Obtain detailed operational data and performance metrics by querying Snowflake's
INFORMATION_SCHEMA
.
_10-- Retrieving refresh history for a dynamic table_10SELECT *_10FROM TABLE(INFORMATION_SCHEMA.DYNAMIC_TABLE_REFRESH_HISTORY(NAME => 'DAILY_SALES_SUMMARY'))_10ORDER BY DATA_TIMESTAMP DESC;
Best Practices
- Dedicated Resource Allocation: Utilize specific warehouses for dynamic tables to isolate and analyze cost and performance effectively.
- Consistent Monitoring: Regularly review the performance and refresh patterns to adjust configurations and ensure optimal operations.
- Robust Access Controls: Implement and regularly review access controls to safeguard your data transformation processes.
Example
Using Dynamic Tables for Daily Sales Summary
Input
Table: sales_data
product_id | sale_date | amount |
---|---|---|
101 | 2024-07-10 12:34:56.000 | 150.00 |
102 | 2024-07-10 13:21:09.000 | 245.00 |
101 | 2024-07-10 09:15:00.000 | 300.00 |
102 | 2024-07-10 10:42:34.000 | 130.00 |
Snowflake SQL Query
_10CREATE OR REPLACE DYNAMIC TABLE daily_sales_summary_10 TARGET_LAG = '30 minutes'_10 WAREHOUSE = 'compute_wh'_10AS_10SELECT product_id,_10 date_trunc('day', sale_date) AS sale_day,_10 sum(amount) AS total_sales_10FROM sales_data_10GROUP BY product_id, sale_day;
Output
Table: daily_sales_summary
product_id | sale_day | total_sales |
---|---|---|
101 | 2024-07-10 00:00:00.000 | 450.00 |
102 | 2024-07-10 00:00:00.000 | 375.00 |
Explanation
- CREATE OR REPLACE DYNAMIC TABLE daily_sales_summary: This statement creates a new dynamic table called daily_sales_summary or replaces an existing one if it already exists.
- TARGET_LAG = '30 minutes': Specifies the maximum delay allowed for the data in the dynamic table to lag behind the data in the source table sales_data. This means the dynamic table will be refreshed to reflect any changes from the source table within 30 minutes.
- WAREHOUSE = 'compute_wh': Assigns a specific virtual warehouse, compute_wh, that Snowflake will use to execute the refresh queries. This warehouse provides the necessary compute resources.
- AS SELECT ...: Defines the query that transforms the data. This query aggregates sales by product and day from the sales_data table. It groups the results by product_id and the truncated sale_date to daily granularity, calculating the sum of amount for each group.
![Y42 SQL completion user interface](/learn/_next/image?url=%2Flearn%2Fimages%2Fcta%2Fsql-completion.webp&w=3840&q=100)