Teradata GROUP BY Query Optimization

Roland Wenzlofsky

April 3, 2015

minutes reading time

Teradata applies two very effective join optimization methods for decision support workload, which usually is involving a lot of aggregations:

Teradata GROUP BY-  Early and Partial Aggregation

Early and Partial GROUP BY are transformations, which allow moving group operations in front of one or more join operations.

While Early GROUP BY executes the group statement, as a whole, before they join, Partial GROUP BY divides the aggregation, doing the first part of the aggregation before the second part after the join.

This technique reduces the resource usage for the join and the aggregation after the join (in case of Early GROUP BY: no aggregation at all has to be done after the join).

The Early Aggregation technique is used to reduce the number of rows as early as possible.

In which situations the optimizer applies, this technique depends on the estimated join costs.

Consider the following SQL query:

FROM t01,t02,t03
WHERE t01.b = t02.b AND t02.c = t03.c
GROUP BY t01.d

The above SQL is an example where the Teradata optimizer can push the GROUP BY into relation t01, partially grouping t01 by columns {t01.b, t01.d}.

The grouped spool can be joined afterward with relation t02, and in the last step, the resulting spool will be joined with t03 with a consecutive final GROUP BY applied.

Although Early and Partial GROUP BY add complexity to the creation of the execution plan (as they offer more join options to the optimizer), they are widely used approaches in any modern database systems for early data reduction.

Partial grouping can be detected by taking a look at the explain plan:

  • Explicitly stated: “We do an all-AMPs partial SUM step” or
  • Before the join takes place:  “we do a SORT/GROUP”
To improve the optimizer’s chance to apply these techniques, you have to collect statistics on all join and aggregation columns.

  • Avatar
    Srivignesh KN says:

    Thanks for explaining, how would early group by being classified and how could it be identified through explain plan.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    You might also like