April 3


Teradata GROUP BY Query Optimization

By Roland Wenzlofsky

April 3, 2015


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 to move group operations in front of one or more join operations.

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

This technique reduces the resource usage for the join and for 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 techniques 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

This is an example of an SQL 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 afterwards with relation t02 and in a 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 a widely used approaches in any modern database systems for early data reduction.

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

  • Explicitely stated: “We do an all-AMPs partial SUM step” or
  • Before the join takes place:  “we do a SORT/GROUP”
As always, in order to improve the chance that the optimizer applies this techniques you have to collect statistics on all join and aggregation columns.

Roland Wenzlofsky

Roland Wenzlofsky is a graduated computer scientist and Data Warehouse professional working with the Teradata database system for more than 20 years. He is experienced in the fields of banking and telecommunication with a strong focus on performance optimization.

You might also like

  • 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"}

    Never miss a good story!

     Subscribe to our newsletter to keep up with the latest trends!