Snowflake
Dynamic Tables: Automate your Data Transformations

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
_10
ALTER DYNAMIC TABLE sales_summary SUSPEND;
_10
_10
ALTER DYNAMIC TABLE sales_summary RESUME;
_10
_10
-- Adjusting the warehouse used by a dynamic table
_10
ALTER 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
_10
SELECT *
_10
FROM TABLE(INFORMATION_SCHEMA.DYNAMIC_TABLE_REFRESH_HISTORY(NAME => 'DAILY_SALES_SUMMARY'))
_10
ORDER 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_idsale_dateamount
1012024-07-10 12:34:56.000150.00
1022024-07-10 13:21:09.000245.00
1012024-07-10 09:15:00.000300.00
1022024-07-10 10:42:34.000130.00

Snowflake SQL Query


_10
CREATE OR REPLACE DYNAMIC TABLE daily_sales_summary
_10
TARGET_LAG = '30 minutes'
_10
WAREHOUSE = 'compute_wh'
_10
AS
_10
SELECT product_id,
_10
date_trunc('day', sale_date) AS sale_day,
_10
sum(amount) AS total_sales
_10
FROM sales_data
_10
GROUP BY product_id, sale_day;

Output

Table: daily_sales_summary

product_idsale_daytotal_sales
1012024-07-10 00:00:00.000450.00
1022024-07-10 00:00:00.000375.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

Your AI copilot for Snowflake SQL

Write production-ready SQL with AI-based suggestions.

Get Started