Every comprehension is first normalized and then translated into an algebraic form consisting of regular joins, selections, unnests, and reductions--the last being the root of the algebraic form.

Thus, T(e, (), (), {()}) translates the term e into an algebraic form. Terms are tree-like data structures that represent both calculus and algebraic terms.

We prove that the algebraic form above is equivalent to the original comprehension.

Our unnesting algorithm generates the algebraic form in Figure 12.A, but we prefer the form in Figure 12.B, which is more efficient.

Hence it does not make a difference whether this query is expressed as a group-by query or a nested query; both forms are optimized to the same efficient algebraic form.

Our unnesting algorithm will generate the algebraic form shown in Figure 12.C for this query, but it is an easy task for an optimizer to transform this algebraic form into the form shown in Figure 12.D (by pulling the outer-join up in the operator tree).

The [Lambda]-DB system uses a polynomial-time heuristic algorithm, called GOO [Fegaras 1998a], that generates a "good quality" order of the monoid algebra operators in query algebraic form. GOO is a bottom-up greedy algorithm that always performs the most profitable operations first.