How to Create an Automatic Pipeline Using Dynamic Tables in Snowflake

Omkar MozarOmkar Mozar
4 min read

In the realm of modern data pipelines, automation is key to improving efficiency and scalability. Snowflake’s Dynamic Tables offer a cutting-edge solution to streamline ETL processes by combining the functionalities of streams and tasks. In this article, we’ll explore how to create an automatic pipeline using Dynamic Tables, covering both incremental and full refresh operations, chaining Dynamic Tables, and understanding their limitations. Let’s dive in!


What Are Dynamic Tables in Snowflake?

Dynamic Tables in Snowflake enable automated transformations and materialization of data by continuously applying SQL logic to source tables. They abstract the complexity of managing streams and tasks, providing a declarative approach to pipeline creation.

Key Features:

  • Incremental Refresh: Automatically processes only new or changed data.

  • Full Refresh: Rebuilds the entire table when required.

  • Chaining Support: Enables building a sequence of Dynamic Tables for complex pipelines.

  • Simplified Automation: Reduces manual effort by combining stream and task functionality.


Step-by-Step Guide

Step 1: Create Source and Target Tables

Set up a source table to simulate incoming data and a target dynamic table to automate processing.

-- Create the source table
CREATE OR REPLACE TABLE source_table (
    id INT,
    name STRING,
    created_at TIMESTAMP
);

-- Insert sample data
INSERT INTO source_table VALUES 
    (1, 'Alice', CURRENT_TIMESTAMP),
    (2, 'Bob', CURRENT_TIMESTAMP);

Step 2: Define the First Dynamic Table

A Dynamic Table automates the transformation process. Use the CREATE OR REPLACE DYNAMIC TABLE command to define its logic.

-- Create the dynamic table
CREATE OR REPLACE DYNAMIC TABLE dynamic_table_a 
AS
SELECT 
    id, 
    name, 
    TO_CHAR(created_at, 'YYYY-MM-DD HH24:MI:SS') AS formatted_date
FROM source_table;

Step 3: Chain Dynamic Tables to Build a Deployment Pipeline

Dynamic Tables can be chained together to create a robust pipeline where the output of one table feeds into the next. This approach eliminates the need for additional scripting or orchestration tools.

Example of a Dynamic Table Chain

-- Second Dynamic Table for further processing
CREATE OR REPLACE DYNAMIC TABLE dynamic_table_b 
AS
SELECT 
    id, 
    UPPER(name) AS uppercased_name, 
    formatted_date
FROM dynamic_table_a;

-- Third Dynamic Table for final transformations
CREATE OR REPLACE DYNAMIC TABLE dynamic_table_c 
AS
SELECT 
    id, 
    CONCAT('Processed-', uppercased_name) AS final_name, 
    formatted_date
FROM dynamic_table_b;

In this pipeline:

  1. source_table provides raw data.

  2. dynamic_table_a applies initial transformations.

  3. dynamic_table_b applies additional transformations.

  4. dynamic_table_c prepares the final output.

This chaining enables the creation of fully automated pipelines with minimal code.

Step 4: Configure Automatic Refresh

Dynamic Tables can refresh incrementally or fully, depending on your requirements.

Incremental Refresh

By default, Dynamic Tables track changes in the source and apply transformations incrementally.

-- Check the dynamic table configuration
SHOW DYNAMIC TABLES;

-- Trigger a manual refresh (optional)
ALTER DYNAMIC TABLE dynamic_table_a REFRESH;

Full Refresh

A full refresh reprocesses all rows in the source table. Use this when you need to rebuild the data completely.

-- Trigger a full refresh
ALTER DYNAMIC TABLE dynamic_table_a REFRESH MODE FULL;

Step 5: Monitor and Optimize Performance

Dynamic Tables provide insights into refresh history and performance metrics.

-- View the refresh history
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));

-- Analyze performance
SHOW DYNAMIC TABLES;

Limitations of Dynamic Tables

While Dynamic Tables offer many advantages, it’s essential to be aware of their limitations:

  1. Non-Deterministic Functions:

    • Dynamic Tables do not support non-deterministic functions (e.g., RANDOM(), CURRENT_TIMESTAMP). If these are used, the results may not align with expectations.

    • For example, using CURRENT_TIMESTAMP in a transformation could lead to inconsistent or incorrect refresh results.

  2. Performance Challenges:

    • Large datasets may require careful optimisation to ensure that refresh operations perform efficiently.

    • Incremental refresh works best when the source table has clear changes tracked.

  3. Complex Logic Limitations:

    • While chaining Dynamic Tables simplifies pipelines, overly complex chains can become difficult to manage and debug.
  4. Limited Customization:

    • Some advanced use cases may still require streams and tasks for finer control.

To mitigate these challenges, always design pipelines with scalability and maintainability in mind.


Conclusion

Dynamic Tables in Snowflake provide a powerful, automated solution for managing data pipelines. By leveraging incremental and full refresh capabilities, chaining multiple Dynamic Tables, and understanding their limitations, you can maintain efficient, up-to-date datasets with minimal manual intervention.

While Dynamic Tables simplify the process, it’s essential to design pipelines carefully to avoid issues with non-deterministic functions or performance bottlenecks. Experiment with Dynamic Tables to see how they fit into your workflows and unlock new possibilities for your data operations.

0
Subscribe to my newsletter

Read articles from Omkar Mozar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Omkar Mozar
Omkar Mozar

As a versatile and innovative software engineer with extensive experience in developing scalable systems and modernizing legacy applications, I thrive on crafting impactful solutions. At Acquia, I designed real-time data pipelines using AWS, GCP, and Snowflake, reducing processing times by over 50%, driving efficiency, and cutting operational costs. My expertise spans frameworks like Java Spring Boot, Hibernate, and AngularJS, and databases such as PostgreSQL, MongoDB, and Snowflake. Passionate about collaboration and code quality, I actively lead performance optimizations and contribute to knowledge-sharing sessions. Skilled in Agile practices, I ensure seamless project delivery by aligning technical strategies with business goals. I excel in migrating to cloud platforms like AWS and GCP and modernizing infrastructures with cutting-edge technologies. Whether innovating for telecommunication products or revolutionizing data workflows, I’m driven by the desire to deliver high-quality, scalable, and efficient solutions that solve complex problems and create lasting impact.