CREATE MATERIALIZE VIEW

Translation reference to convert Greenplum Materialized View to Snowflake Dynamic Table

Description

In Snowconvert, Materialized Views are transformed into Snowflake Dynamic Tables. To properly configure Dynamic Tables, two essential parameters must be defined: TARGET_LAG and WAREHOUSE. If these parameters are left unspecified in the configuration options, Snowconvert will default to preassigned values during the conversion, as demonstrated in the example below.

Grammar Syntax

CREATE MATERIALIZED VIEW [ IF  NOT EXISTS ] <table_name>
    [ (<column_name> [, ...] ) ]
    [ USING <method> ]
    [ WITH ( <storage_parameter> [= <value>] [, ... ] ) ]
    [ TABLESPACE <tablespace_name> ]
    AS <query>
    [ WITH [ NO ] DATA ]
    [DISTRIBUTED {| BY <column> [<opclass>], [ ... ] | RANDOMLY | REPLICATED }]

Code Examples

Simple Case

Input Code:

IN -> Greenplum_01.sql
CREATE MATERIALIZED VIEW product_summary AS
SELECT
    category,
    COUNT(*) AS total_products,
    MAX(price) AS max_price
FROM products
GROUP BY category;

Output Code:

OUT -> Greenplum_01.sql
CREATE OR REPLACE DYNAMIC TABLE product_summary
--** SSC-FDM-0031 - DYNAMIC TABLE REQUIRED PARAMETERS SET BY DEFAULT **
TARGET_LAG='1 day'
WAREHOUSE=UPDATE_DUMMY_WAREHOUSE
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "greenplum",  "convertedOn": "04/24/2025",  "domain": "test" }}'
AS
    SELECT
    category,
    COUNT(*) AS total_products,
    MAX(price) AS max_price
FROM
    products
    GROUP BY category;

IF NOT EXISTS

This clause has been removed during the migration from Greenplum to Snowflake.

USING, TABLESPACE, and WITH

This syntax is not needed in Snowflake.

These clauses are removed during the conversion process. In Greenplum, they are used to further customize data storage manually. This is something that Snowflake handles automatically (micro partitions), and it is typically not a concern.

DISTRIBUTED BY

The DISTRIBUTED BY clause in Greenplum controls how data is physically distributed across the system's segments. Meanwhile, CLUSTER BY is a subset of columns in a dynamic table (or expressions on a dynamic table) explicitly designated to co-locate the data in the table in the same micro-partitions. While they operate at different architectural levels, they aim to improve query performance by distributing data efficiently.

Grammar Syntax

DISTRIBUTED BY ( <column> [<opclass>] [, ... ] )

Sample Source

Input Code:

IN -> Greenplum_02.sql
CREATE MATERIALIZED VIEW product_summary AS
SELECT
    category,
    COUNT(*) AS total_products,
    MAX(price) AS max_price
FROM products
GROUP BY category
DISTRIBUTED BY (category);

Output Code:

OUT -> Greenplum_02.sql
CREATE OR REPLACE DYNAMIC TABLE product_summary
--** SSC-FDM-0031 - DYNAMIC TABLE REQUIRED PARAMETERS SET BY DEFAULT **
TARGET_LAG='1 day'
WAREHOUSE=UPDATE_DUMMY_WAREHOUSE
--** SSC-FDM-GP0001 - THE PERFORMANCE OF THE CLUSTER BY MAY VARY COMPARED TO THE PERFORMANCE OF DISTRIBUTED BY **
CLUSTER BY (category)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "greenplum",  "convertedOn": "04/24/2025",  "domain": "test" }}'
AS
    SELECT
    category,
    COUNT(*) AS total_products,
    MAX(price) AS max_price
FROM
    products
    GROUP BY category;

DISTRIBUTED RANDOMLY - REPLICATED

This syntax is not needed in Snowflake.

The DISTRIBUTED REPLICATED or DISTRIBUTED RANDOMLY clause in Greenplum controls how data is physically distributed across the system's segments. As Snowflake automatically handles data storage, these options will be removed in the migration.

  1. SSC-FDM-GP0001: The performance of the CLUSTER BY may vary compared to the performance of Distributed By.

  2. SSC-FDM-0031: Dynamic Table required parameters set by default

Last updated