








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Optimization of Nested Queries in a Complex Object Model
Hennie J. Steenhagen, Peter M.G. Apers, and Henk M. Blanken
Department of Computer Science, University of Twente, PO Box 217, 7500 AE Enschede, The Netherlands
Abstract. Transformation of nested SQL queries into join queries is advantageous because a nested SQL query can be looked upon as a nested-loop join, which is just one of the several join implementations that may be available in a relational DBMS. In join queries, dangling (unmatched) operand tuples are lost, which causes a problem in transforming nested queries having the aggregate function COUNT between query blocks–a problem that has become well-known as the COUNT bug. In the relational context, the outerjoin has been employed to solve the COUNT bug. In complex object models supporting an SQL-like query language, transfor- mation of nested queries into join queries is an important optimization issue as well. The COUNT bug turns out to be a special case of a general problem being revealed in a complex object model. To solve the more general problem, we intro- duce the nest join operator, which is a generalization of the outerjoin for complex objects.
1 Introduction
Currently, at the University of Twente, work is being done on the high-level object- oriented data model TM. TM is a database specification language incorporating standard object-oriented features such as classes and types, object identity, complex objects, and multiple inheritance of data, methods, and constraints. In TM, methods and constraints are specified in a high-level, declarative language of expressions. An important language construct is the SELECT-FROM-WHERE (SFW) construct. The SFW-construct of TM is comparable to the SFW-query block from HDBL, the query language of the experi- mental DBMS AIM [10]. HDBL is an orthogonal extension of SQL for extended NF^2 data structures. Optimization of TM SFW-expressions therefore has much in common with optimization of the SFW-expressions of SQL and HDBL. Optimization of SQL queries has received quite some attention the last decade. An important problem in this area is the optimization of nested SQL queries [7, 5, 4, 8, 9]. SQL offers possibilities to formulate nested queries: SFW-query blocks containing other SFW-blocks in the WHERE clause. In [7], it was pointed out that it is advantageous to replace nested SQL queries by flat, or join queries. Flat SQL queries are SFW-blocks not containing subqueries in the WHERE clause. Replacing nested SQL queries by join queries is advantageous because a nested SQL query can be looked upon as a nested- loop join, which is just one of the several join implementations possible. After rewriting a nested query into a join query, the optimizer has better possibilities to choose the most appropriate join implementation.
In nested queries, inner operand tuples are grouped by the values of the outer operand tuples. Whenever aggregate functions occur between query blocks, the transformed, i.e. join query also requires grouping (expressed by means of the GROUP BY clause). In nested queries, dangling outer operand tuples, i.e. outer operand tuples that are not matched by any of the inner operand tuples, deliver a subquery result equal to the empty set. Transformation of a nested query into a join query causes the loss of dangling tuples. In the relational context, this may cause a problem when the aggregate function COUNT occurs between query blocks. As a solution to this problem (that has become well known as the COUNT bug), it has been proposed to use the outerjoin instead of the regular join [5].
In complex object models supporting an SQL-like query language, transformation of nested queries into join queries is just as important as in the relational context. However, in complex object models grouping of the inner operand is required not only if aggregate functions occur between query blocks, but in many other cases as well. The reason for this is that attributes may be set valued. Moreover, each time grouping is necessary, we have to deal with some kind of COUNT bug caused by the loss of dangling tuples, i.e. the COUNT bug is just a special case of a more general problem being revealed in a complex object model. An important result of this paper is that from the form of the predicate between query blocks it can easily be derived when grouping is not necessary. Nested queries that do not require grouping can be transformed into join queries; for the efficient and correct processing of nested queries that do require grouping a new join operator is introduced–the nest join operator.
Instead of producing the concatenation of every pair of matching tuples as in the reg- ular join operation, in the nest join operation each left operand tuple is extended with the set of matching right operand tuples. This way two birds are killed with one stone: group- ing is performed, and also dangling tuples are preserved. Implementation of the nest join operator is a simple modification of any common join implementation method, however, like the outerjoin operator, the nest join has limited rewrite possibilities compared to the regular join operator.
In general, in the logical optimization of a declarative query language, two approaches can be distinguished: (1) rewriting expressions in the query language itself and (2) trans- lation into and rewriting in some intermediate language, for example an algebraic lan- guage. Also a combination of the two approaches is possible. For the logical optimiza- tion of TM, we have defined the language ADL, an algebra for complex objects which is an extension of the NF^2 algebra of [12]. This work will be used in the ESPRIT III project IMPRESS (Integrated, Multi-Paradigm, Reliable, and Extensible Storage System). The IMPRESS project started in 1992, and one of the subtasks is to translate (a subset of) the language TM into an algebra for complex objects resembling ADL. In this paper, our ideas with regard to query transformation will be presented using the language TM; we will not introduce the algebra for reasons of simplicity.
The structure of this paper is as follows. In Section 2, we briefly review the work that has been done with regard to nested SQL queries. In Section 3, we describe the lan- guage TM and the types of nested SFW-expressions that are of interest for the purpose of this paper. Nesting in the WHERE clause and in the SELECT clause are discussed in Sections 4 and 5, respectively, and we will see that in the transformation of nested
SELECT * FROM R WHERE R :B = SELECT COUNT (*) FROM S WHERE R :C = S:C
Following Kim’s algorithm, we get the following queries:
(1) T (C ; C N T ) = SELECT S:C , COUNT (*) FROM S GROUP BY S:C
SELECT R :A; R :B ; R :C FROM R , T WHERE R :B = T :C N T AND R :C = T :C
Alternatively, if the relation R does not contain duplicates, the nested query may be transformed into:
(2) SELECT R :A; R :B ; R :C FROM R , S WHERE R :C = S:C GROUP BY R :A; R :B ; R :C HAVING R :B = COUNT (S:C )
In the former, grouping of the inner operand and computation of the aggregate precedes the join operation; in the latter the join is executed first. The queries resulting from the transformations do not give the correct result. In the original, nested query, dangling R -tuples for which R :B = 0 , are included in the result; these tuples are lost in the join queries. To solve the COUNT bug, it has been proposed in [5] to modify (2) by using out- erjoins instead of joins in case the COUNT function occurs between query blocks. The right outerjoin operator preserves dangling tuples of the left join operand: unmatched left operand tuples are extended with NULL values in the right operand attribute positions. As another solution, it has been proposed in [9] to modify (1), because in some cases (1) is more efficient than (2). It is proposed to have two types of join predicates in the sec- ond query of (1): a regular join predicate and an additional, so-called antijoin predicate, to be applied to the dangling tuples. In the example given above the antijoin predicate would be: R :B = 0. In Kim’s second query the join is replaced by an outerjoin operation with join predicate R :C = T :C ; to the tuples that match the predicate R :B = T :C N T is applied, and to the unmatched tuples in R the antijoin predicate R :B = 0 is applied.
3 Nested TM Queries
3.1 General Description of TM
In this section we describe the features of TM that are important for the purpose of this paper–support for complex objects and the SELECT-FROM-WHERE construct. For a more comprehensive description of TM we refer the reader to [1, 2, 3].
TM is a high-level, object-oriented database specification language. It is formally founded in the language FM, a typed lambda calculus allowing for subtyping and multi- ple inheritance. Characteristic features of TM are the distinction between types, classes, and sorts, support for object identity and complex objects, and multiple inheritance of attributes, methods, and constraints. In TM, attribute types may be arbitrarily complex: the type constructors supported are the tuple, variant, set, and list type constructor; type constructors may be arbitrarily nested. Besides basic types, class names may be used in type specifications. Sets do not contain duplicates. In constraint and method specifications we may use the SELECT-FROM-WHERE construct, having the following general format:
SELECT
The meaning of the SFW-expression is as follows. The operand expression is evaluated; a variable is iterated over the resulting set; for each value of the variable it is determined whether the predicate holds, and if so, the result expression is evaluated and this value is included in the resulting set.
3.2 Types of Nesting in TM
One important difference between SQL on the one hand, and TM and HDBL on the other is that TM and HDBL are orthogonal languages. The operand and result expression of the SFW-query block of TM may be arbitrary expressions, also containing other (nested) SFW-expressions, provided they are correctly typed. The predicate may also be built up from arbitrary expressions (including quantifiers FORALL and EXISTS), as long as it delivers a Boolean result. We give some examples of SFW-expressions. Assume we have specified classes ‘Em- ployee’ and ‘Department’:
CLASS Employee WITH EXTENSION EMP ATTRIBUTES name : STRING, address : Address, sal : INT, children : Phname : STRING, age : INTi END Employee
CLASS Department WITH EXTENSION DEPT ATTRIBUTES name : STRING, address : Address, emps : PEmployee END Department
SORT Address TYPE hstreet : STRING, nr : STRING, city : STRINGi END Address
The WITH clause is a TM construct enabling local definitions, used here to facilitate the description of the syntactical form of the predicate P. In this paper, we do not consider multiple subqueries, P (x; z ) contains only one occurrence of z. We want to transform the nested query into a join query of the following format (re- member that, in SQL, grouping is necessary only if aggregate functions occur between query blocks):
SELECT F (x) FROM X x, Y y WHERE P 0 (x; v ) ^ Q(x; y ) WITH v = G(x; y )
For notational convenience, also the expression G(x; y ) has been named by means of a WITH clause. The goal of the transformation process is to transform the predicate P (x; z ), whose second argument z is set valued, into a predicate P 0 (x; v ), where values v are the mem- bers of z. The types of P and P 0 clearly differ: from the second argument of P a set constructor is removed, resulting in predicate P 0.
4.1 Example Predicates
Assume that the predicate P only involves attribute a of the outer operand X and z , the subquery result. Because the attribute a may be set valued, this (already restricted) pred- icate between query blocks may take many different forms. We may have for instance^1 :
- x:a OP z with OP 2 f2; ; ; =; ; ; 3g, - expressions involving quantifiers, for example 9 s 2 z (s = x:a), - x:a OP H (z ) with H an aggregate function and OP an arithmetical comparison op- erator, - expressions involving set operators, for example x:a \ z = ;, or - negations of expressions listed above.
Predicates can be divided into two groups: predicates that require grouping of the in- ner operand tuples and the predicates that do not. In Section 7, we give a formal charac- terization of predicates that do and do not require grouping; below, the need for grouping is discussed more informally. Grouping is not necessary if the question whether outer operand tuples belong to the result or not (whether, for some outer operand tuple, the predicate evaluates to tr ue or f al se) can be answered on the basis of the individual members of the subquery result, i.e. by scanning the subquery result. For example, consider the expression x:a 2 z. The moment we encounter a tuple y in the inner operand Y such that the condition Q(x; y ) holds and x:a equals the value of v , we know that tuple x belongs to the result. If no such v is found in the end, the predicate evaluates to f al se. Grouping is necessary if the subquery result has to be available as a whole to decide whether the predicate holds. In this case, all tuples belonging to the subquery result must
(^1) In the rest of the paper we will use the common set-theoretical notation for comparison operators and boolean connectives occurring in TM-predicates because of its conciseness.
be kept, because the predicate can only be evaluated having all values in the subquery result at hand. An obvious example of a predicate requiring grouping is the expression x:a = COUNT(z ): not until the entire subquery result is at our disposal it is possible to compute (or output, if accumulated) the cardinality of the subquery result. Another predicate requiring grouping is for example the expression x:a z.
Whenever a predicate needs grouping, we have to deal with some sort of COUNT bug if the nested query is transformed according to the algorithm of [7]. For example, the nested query:
SELECT x FROM X x WHERE x:a z WITH z = SELECT y :a FROM Y y WHERE x:b = y :b
is, following the ideas of [7], transformed into the following TM queries:
T = SELECT hb = y :b, as = SELECT y 0 :a FROM Y y 0 WHERE y 0 :b = y :bi FROM Y y
SELECT x FROM X x, T t WHERE x:b = t:b ^ x:a t:as
The first query groups the set of y :a values by the values of the attribute b (cf. the oper- ator nest ( ) from the NF^2 algebra [12]). The transformed query also suffers from a bug (which we might call the SUBSETEQ bug in this case): X -tuples for which x:a = ; that are not matched by any t-tuple on the condition x:b = t:b are lost. In summary, in TM grouping of the inner operand is required not only if aggregate functions occur between query blocks, but in many other cases too. If Kim’s solution is chosen, the transformed query will suffer from a bug each time grouping is needed. We will not use the outerjoin operator to solve these bugs. Instead, in Section 6, we will introduce the nest join operator, which is a much cleaner solution in a model supporting complex objects.
5 Nesting in the SELECT Clause
In this section, we will show that, with one notable exception, nesting in the SELECT clause always requires grouping of the inner operand. SFW-expressions having subqueries in the SELECT clause are not new. In HDBL, it is also allowed to have SFW-query blocks in the SELECT clause. SFW-expressions nested in the SELECT clause commonly describe nested results, as in query Q2 from Section 3.2 where employees are grouped by departments. Consider the general format of a two-block query with nesting in the SELECT clause:
x;y :Q;G;a
d = fx ++ ha = z i j x 2 X ^ z = fG(x; y ) j y 2 Y ^ Q(x; y )gg
In this expression, x++ha = z i denotes the concatenation of the tuple x and the unary tuple ha = z i, in which a is an arbitrary label not occurring on the top level of X. An example of the nest join operation is found in Table 1, where flat relations X and Y are equijoined on the second attribute (the join function is the identity function). Note that for dangling tuples x 2 X , the tuple x ++ ha = ;i is present in the result. The nest join
a b 1 1 1 2 2 3
c d 1 1 2 1 3 3
a b s(c; d) 1 1 f(1,1), (2,1)g 1 2 ; 2 3 f(3,3)g
Table 1. X , Y , and the nest equijoin of X and Y on the second attribute
operation is a neatly defined operation. Grouping, which is performed during the join, is made explicit by means of a set-valued attribute. Because dangling tuples are preserved, bugs like the COUNT bug are prevented.
Algebraic Properties
Assuming the nest join function is identity, the nest join can be expressed using the out- erjoin, denoted by the symbol , and the nest operator ^ :
X 4 x;y :Q;id;a
Y X ;a (X x;y :Q
In this algebraic expression, the operator ^ is a slightly modified version of the stan- dard nest operator performing nesting in the usual way, and then mapping nested sets consisting of a NULL-tuple to the empty set [13]. By using the nest join instead of the outerjoin followed by the nest operator, we do not have to resort to NULLs to avoid the loss of dangling tuples. A disadvantage of the nest join operator is that it, like the outerjoin, has less pleas- ant algebraic properties. For example, the nest join operation is neither commutative nor associative. As another example, the nest join does not always associate with the regu- lar join: X 4 (Y 1 Z ) is not equivalent to (X 4 Y ) 1 Z , the two expressions already being typed differently. Below we list some examples of algebraic equivalences con- cerning the nest join operator. Let X (^4) p Y denote a nest join operation on predicate p in which the nest join func- tion equals the identity function (for simplicity omitting variable names and the nest join label). Let r (a; b) denote a predicate referencing attributes in tables A and B (and no other), then we have:
- X (X 4 Y ) X - (X (^1) r (x;y ) Y ) (^4) r (x;z ) Z (X (^4) r (x;z ) Z ) (^1) r (x;y ) Y ) - (X (^1) r (x;y ) Y ) (^4) r (y ;z ) Z X (^1) r (x;y ) (Y (^4) r (y ;z ) Z )
Implementation
To implement the nest join, common join implementation methods like the sort-merge join, or the hash join can be used. However, some restrictions hold. First, in nest join im- plementations, an output tuple can be produced not until the entire set of matching right operand tuples has been found. Second, because in the nest join operation the output has to be grouped according to the values of the left operand tuples, the choice for outer and inner loop operand is restricted. For example, in a (simple) hash join implementation, if the join attribute does not form a key attribute of the right join operand, only the right join operand may be the build table. (For the regular join, usually the smaller operand is chosen as the build table.)
Use
The nest join operation can be employed to process queries with nesting in the SELECT- as well in the WHERE clause. Queries having subqueries in the SELECT clause often describe nested results, so processing by means of the nest join operation will be an ap- propriate method of processing. For queries with nesting in the WHERE clause, how- ever, sometimes there are other, more efficient possibilities. In the following section we show in which cases grouping certainly is not necessary, so that, instead of the nest join operator, we may employ some flat join operator to obtain the results needed.
7 The Need for Grouping
In this section, we present a class of predicate expressions for which it is known that grouping is not necessary. Again consider the general format of a two-block query with nesting in the WHERE clause given in Section 4, then we have:
Theorem 1. Grouping is not necessary if the predicate expression P (x; z ) can be rewrit- ten into a calculus expression of the form (1) 9 v 2 z (P 0 (x; v )) or (2) 69 v 2 z (P 0 (x; v )). In this expression, P 0 (x; v ) may be arbitrary.
Proof of Theorem 1 is omitted due to lack of space. It is an open question whether grouping is always necessary in case predicate P cannot be rewritten into one of the two forms given above. Generally, a nested query may be processed by applying nest join operators, possibly followed by (nested) function applications (e.g. projections) and selections. However, sometimes nest join operators may be replaced by flat join operators. For a two-block query, in case the predicate between the two blocks can be rewritten into a calculus ex- pression of the first format, involving a non-negated existential quantifier, a semijoin operation will provide the correct result. If it is possible to rewrite the predicate into a calculus expression involving a negated existential quantifier, then the flat join opera- tion needed is the antijoin operation. The join predicate is P 0 (x; G(x; y )) ^ Q(x; y ). (Remember that the semi-and antijoin operations are defined as follows. Let X and Y be tables (sets of tuples having possibly complex attributes), and let P be a predicate, then the semijoin operation X nx;y :P Y is defined as fx j x 2 X ^ 9 y 2 Y (P (x; y ))g
join operator. Note that, like the semijoin, the antijoin operation may be implemented as a modification of the regular join. Consider the following query:
SELECT x FROM X x WHERE x:a SELECT y :a (P 1 ) FROM Y y WHERE x:b = y :b ^ y :c SELECT z :c (P 2 ) FROM Z z WHERE y :d = z :d
Predicates P 1 and P 2 between query blocks do require grouping (see Table 2), so we may have the following execution strategy:
(1) A nest join with operands Y and Z on join predicate y :d = z :d. Each element of Z satisfying the join predicate is projected on the c attribute. The result of this step is the set: T 1 = fy ++ hz s = fz :c j z 2 Z ^ y :d = z :dgi j y 2 Y g. Note z s is an arbitrary label.
(2) The result of (1) is restricted such that y :c y :z s: fy j y 2 T 1 ^ y :c y :z sg. (3) The result of (2) is nest joined with X on join predicate x:b = y :b and projected on attribute a. (A projection of (2) on attributes a and b may proceed the nest join op- eration.) We now have: T 3 = fx ++ hy s = fy :a j y 2 T 2 ^ x:b = y :bg j x 2 X g. Again, label y s is arbitrary. (4) Finally, the result of (3) is restricted such that x:a x:y s and projected on the at- tributes of X (attributes a and b): T 4 = fha = x:a; b = x:bi j x 2 T 3 ^ x:a x:y sg.
Now assume that the operators in predicates P 1 and P 2 are changed in 2 and 62 , respectively, then the nest join operation in (1) may be replaced by an antijoin operation, and the nest join in (3) may be replaced by a semijoin operation. The additional join predicates are y :c = z :c and x:a = y :a, respectively.
9 Conclusions and Future Work
As in relational systems supporting SQL, in complex object models supporting an SQL- like query language optimization of nested queries is an important issue. A naive way to handle nested queries is by nested-loop processing. However, it is better to transform nested queries into flat, or join queries, because join queries can be implemented in many different ways. In a complex object model, it is not always possible to flatten nested queries–in this paper, we have described a class of nested SFW-expressions that can be flattened without problem. For those nested queries that cannot be transformed into join queries we have introduced the nest join operator, allowing correct and efficient process- ing of general nested queries. Future work concerns a number of issues. We need a formal algorithm to translate general SFW-query blocks of TM into the algebra, also taking into account nesting in the SELECT clause, multiple subqueries, and multiple nesting levels (including cyclic
queries). Logical optimization (rewriting algebraic expressions) may follow the trans- lation process. Therefore, the algebraic properties of the nest join operator have to be further investigated, by analogy with for example the work of [11] concerning the out- erjoin operator.
Acknowledgements
We thank our colleagues Rolf de By and Paul Grefen for comments on earlier drafts of this paper.
References
This article was processed using the LATEX macro package with LLNCS style