DWHPro Insights

Pre-Aggregation Before a Join: From Teradata to Snowflake

By Roland Wenzlofsky ·

In a recent lab test, a Snowflake join returned 500,000 rows. After a small SQL rewrite, the join returned 10,000 rows and the query still produced the same totals. We grouped sales by customer before joining them to the customer table. Teradata users will recognise this as the idea behind Partial GROUP BY. Snowflake’s Aggregation Placement builds on the same idea and can decide during execution whether the extra aggregation pays off.

The history of this optimisation is a good example of how proven data warehouse techniques find their way into newer data platforms. It also gives us a practical way to evaluate a feature announcement. Ask which part of the technique is familiar, what the new implementation changes, and what it saves on a real query.

Why aggregate before a join?

Suppose we want total sales and the number of sales by customer segment. The sales table has many rows per customer, and the customer table has one row per customer. One plan joins every sale to its customer, then groups the result by segment.

Another plan first calculates the sum and count for each customer. It joins those customer-level totals to the customer table and then adds them up by segment. The more sales each customer has, the fewer sales-side rows reach the join. A rewrite must preserve the original result, including the effect of duplicate matches and the behaviour of the aggregate functions.

Join first produces 500,000 joined rows. Grouping sales by customer first produces 10,000 joined rows. Both paths return 10 segment totals.
Two ways to calculate the same segment totals. The counts come from the original and rewritten Snowflake queries in our lab.

Teradata had the technique in V2R5

In 1994, Surajit Chaudhuri and Kyuseok Shim described how a query optimiser could move a GROUP BY ahead of one or more joins. In November 2002, NCR documented Teradata V2R5’s cost-based implementation. Teradata could move an aggregation in full or perform part of it early when the transformation preserved the result and the optimiser estimated a saving. VLDB paper, 1994 · NCR technical paper, 2002

In a static Teradata plan, the optimiser decides whether to use Partial GROUP BY when it creates the plan. Teradata also supports Incremental Planning and Execution (IPE), which can use results or statistics from earlier steps to plan later fragments. The comparison below refers to the static case we observed in the lab. Teradata documentation on IPE

Snowflake makes the decision adaptive

Snowflake’s cloud data warehouse became generally available in June 2015. Snowflake reported that Aggregation Placement was already being used in queries from more than 8,000 customers by April 2023. In August 2023, it reported that the rollout to all production accounts was complete. That was just over eight years after general availability and nearly 21 years after NCR documented the Teradata optimisation. Snowflake’s SIGMOD paper · Snowflake Engineering overview

Snowflake’s optimiser puts eligible pre-aggregation operators into the plan before execution. As batches of rows flow through the plan, the engine uses information such as row counts and estimated distinct grouping keys to assess each operator. If the work it saves downstream is smaller than the work it adds, the operator can switch itself off. That is a useful addition when the benefit is hard to estimate before the query runs. Snowflake Engineering technical deep dive

A static Teradata plan selects the step during planning. Snowflake places eligible operators in the plan and evaluates them with runtime data.
The static Teradata plan selects the step up front. Snowflake places eligible operators up front and evaluates their benefit at runtime.

What the lab test showed

We ran the same analytical query on Teradata Express and a Snowflake trial warehouse with 500,000 sales and 10,000 customers. In Teradata’s DBQL step records, the original SQL grouped sales by customer before joining. In Snowflake’s Query Profile, the original SQL joined the sales rows first. With an explicit pre-aggregation, the Snowflake join produced 10,000 rows and the query returned the same counts and amounts for all ten segments.

Join output in the lab: Teradata original SQL, 10,000 rows. Snowflake original SQL, 500,000 rows. Snowflake with explicit pre-aggregation, 10,000 rows.
The chart compares rows produced by the join. The two environments were used to inspect plans, and the figures measure intermediate rows rather than elapsed time.

The recorded Snowflake profile shows what executed. It leaves open whether the optimiser did not insert an eligible operator or whether an inserted operator was disabled at runtime. The test shows why it is worth checking the execution profile for the SQL and data you actually run.

How to evaluate the next feature announcement

Pre-aggregation is one example of a broader pattern. In 2002, NCR also described how Teradata’s Join Indexes could store precomputed results for the optimiser to use automatically. Snowflake’s Materialized Views apply a related principle. The optimiser can route suitable queries to stored results and save repeated work. NCR paper on materialised views in Teradata · Snowflake documentation on Materialized Views

When migrating in either direction, look at the execution profiles for the queries that matter. If far more rows reach a join on the target platform, test a rewrite and check that it preserves the result. Then measure elapsed time and resource use under comparable conditions.

A feature can be new to a platform and still have decades of engineering behind it. In this case, Teradata shows how long the idea has been useful, and Snowflake shows how a newer engine can adapt it at runtime. The value to your workload is something you can measure.

Related reading on DWHPro: Teradata Early and Partial GROUP BY · Skewed Joins in Snowflake and Teradata

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