DWHPro Insights

Migrating SQL Between Teradata and Snowflake? Check Where the Join Gets Its Rows

By Roland Wenzlofsky ·

Migrating SQL Between Teradata and Snowflake? Check Where the Join Gets Its Rows

The SQL passes validation after the migration. It returns the same counts and sales totals. Before you sign off on the batch window, check how the target engine produced those results. Two optimizers can pass very different numbers of rows through a join for the same query.

One query, three execution plans

In our lab, 500,000 sales joined to 10,000 customers. In the skewed version, 409,100 sales belonged to one customer. The query counted sales and summed amounts by customer segment. Both platforms returned ten segments.

Three measured execution plans. The Teradata join produced 10,000 rows, the original Snowflake join produced 500,000, and the Snowflake rewrite produced 10,000.
Measured row counts from Teradata DBQL and Snowflake Query Profile.

Teradata aggregated sales per customer before redistribution and the merge join. DBQL recorded 10,000 rows after that first aggregate and 10,000 rows out of the join. Snowflake joined the sales rows first and sent 500,000 rows from the join to the segment aggregate. These are actual operator counts for this workload. Snowflake also supports automatic aggregation placement, and we observed the join-first plan for this query in our test account. Snowflake Engineering

Teradata Query Performance Tuning: The DWHPro Guide book cover

Get the complete Teradata tuning book, free

Teradata Query Performance Tuning: The DWHPro Guide
By Roland Wenzlofsky & Artemios Vogiatzis

206 pages on statistics and demographics, access paths and indexes, query execution plans, and joins. Get the full PDF and practical Teradata updates from DWHPro.

Confirm your email to receive your download link. Privacy policy. Unsubscribe in any newsletter.

About the free book

Teradata to Snowflake

If this query missed its runtime target after migration, I would check the Join and Aggregate operators in Snowflake Query Profile. Then I would test this rewrite on the target warehouse.

WITH sales_by_customer AS (
    SELECT customer_id, COUNT(*) AS sale_count, SUM(amount) AS sale_amount
    FROM sales
    GROUP BY customer_id
)
SELECT c.segment_id,
       SUM(s.sale_count) AS sales_rows,
       SUM(s.sale_amount) AS amount_sum
FROM sales_by_customer s
JOIN customers c ON c.customer_id = s.customer_id
GROUP BY c.segment_id
ORDER BY c.segment_id;

The inner COUNT counts sales. The outer SUM adds those counts across customers in the segment. An outer COUNT(*) would count the joined groups instead. With the rewrite, Snowflake’s join output went from 500,000 to 10,000 rows. The counts and amounts by segment matched the original.

Snowflake to Teradata

Start by carrying over the Snowflake SQL that meets the current runtime target. Use Teradata EXPLAIN to see where the work lands, then check actual step counts in DBQL. If the original SQL already has a manual rewrite, you can test the simpler query as a candidate. In our lab, Teradata automatically preaggregated the simpler query. For a different workload, the target could require a different rewrite. Compare both versions against the same result and runtime targets before deciding which to keep.

Prove both before cutover

First compare counts and amounts for every segment. Then compare repeated elapsed times against the source baseline, using representative volumes, data distributions and concurrency. Check the target plan if it misses the runtime budget. For this join, also confirm customer-key matches and test missing keys or NULLs if they occur in the real data.

The experiment ran on Teradata Express with four AMPs and a Snowflake trial with an X-Small warehouse. The factor of 50 describes join output rows. Runtime parity is a separate acceptance test for the migrated workload.

Measured join output

Execution plan Actual join output rows
Teradata original 10,000
Snowflake original 500,000
Snowflake with explicit preaggregation 10,000

We ran the original query five times per data distribution on each engine. We verified the Snowflake rewrite on the skewed dataset.

Planning a Teradata-to-Snowflake migration?

Review the assessment, conversion, reconciliation and cutover work needed for a controlled transition.

Explore migration services

Teradata Query Performance Tuning: The DWHPro Guide book cover

Get the complete Teradata tuning book, free

Teradata Query Performance Tuning: The DWHPro Guide
By Roland Wenzlofsky & Artemios Vogiatzis

206 pages on statistics and demographics, access paths and indexes, query execution plans, and joins. Get the full PDF and practical Teradata updates from DWHPro.

Confirm your email to receive your download link. Privacy policy. Unsubscribe in any newsletter.

About the free book

Follow DWHPro in Google to see our articles more often in Search.

Leave a Comment

Teradata Query Performance Tuning: The DWHPro Guide book cover

Get the complete Teradata tuning book, free

Teradata Query Performance Tuning: The DWHPro Guide
By Roland Wenzlofsky & Artemios Vogiatzis

206 pages on statistics and demographics, access paths and indexes, query execution plans, and joins. Get the full PDF and practical Teradata updates from DWHPro.

Confirm your email to receive your download link. Privacy policy. Unsubscribe in any newsletter.

About the free book

Protected by Security by CleanTalk and CleanTalk Anti-Spam