Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Optimizing an SQL-like Nested Query.

Slides:



Advertisements
Similar presentations
Examples of Physical Query Plan Alternatives
Advertisements

Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
2005conjunctive-ii1 Query languages II: equivalence & containment (Motivation: rewriting queries using views)  conjunctive queries – CQ’s  Extensions.
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
CPSC 504: Data Management Discussion on Chandra&Merlin 1977 Laks V.S. Lakshmanan Dept. of CS UBC.
CS4432: Database Systems II
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
1 Conjunctions of Queries. 2 Conjunctive Queries A conjunctive query is a single Datalog rule with only non-negated atoms in the body. (Note: No negated.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization See Sections 15.1, 2, 3, 7.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
16.5 Introduction to Cost- based plan selection Amith KC Student Id: 109.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapter 15.
Query Optimization Overview Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems December 2, 2004 Some slide content derived.
Query Processing & Optimization
Access Path Selection in a Relation Database Management System (summarized in section 2)
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
Database systems/COMP4910/Melikyan1 Relational Query Optimization How are SQL queries are translated into relational algebra? How does the optimizer estimates.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Chapter 13 Query Processing Melissa Jamili CS 157B November 11, 2004.
Query Optimization Arash Izadpanah. Introduction: What is Query Optimization? Query optimization is the process of selecting the most efficient query-evaluation.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Optimization of Nested Queries Sujatha Thanigaimani COSC 6421.
Relational Algebra Operators
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Advance Database Systems Query Optimization Ch 15 Department of Computer Science The University of Lahore.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
CSE202 Database Management Systems
Chapter (6) The Relational Algebra and Relational Calculus Objectives
STRUCTURE OF PRESENTATION :
Database Management System
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Chapter 12: Query Processing
Basic SQL Lecture 6 Fall
Database Management Systems (CS 564)
Introduction to Database Systems
File Processing : Query Processing
The Relational Algebra and Relational Calculus
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
Dealing with Uniqueness Constraint in Query Optimization
Advance Database Systems
STRUCTURE OF PRESENTATION :
CS222: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Presentation transcript:

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Optimizing an SQL-like Nested Query

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, Introduction - Classification of nested queries - Algorithms to transform nested to non-nested queries - Potential performance improvements - Strategy to process SQL-like nested queries of arbitrary complexity Overview:

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 The illustrative examples used in this presentation are based on the following database of suppliers, parts, projects, and shipments: Introduction: SUPPLIER(NO, SNAME, SLOC, SBUDGET) PART(PNO, PNAME, DIM, PRICE, COLOR) PROJECT(JNO, JNAME, PNO, JBUDGET, JLOC) SHIPMENT(SNO, PNO, JNO, QTY) SELECT FROM WHERE whe X – const. or number of const. op – scalar comparison operator (, <=, etc.)or set membership operator (IS IN, IS NOT IN) Throughout this presentation R i denotes a relation in the database, C k the k th column of a relation, B the size in pages of available main-memory buffer The fundamental structure of an SQL-like query is syntactically represented by a query block which consists of SELECT, FROM, and WHERE clauses. Ex.:1 SELECT MAX(PN0) FROM PART WHERE PRICE > ‘25’;

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Extension of the simple predicates of the form [Ri.Ck op X], for n-relation queries: Nesting of the Query Blocks: predicates Nested predicate. [R i.C k op Q], where Q - SQL-like query block op - may be a scalar or set membership operator. if [Q op Ri.Ck]. set membership operator is then replaced by the set containment operator (CONTAINS, DOES NOT CONTAIN). Join predicate. [R i.C k op R j.C h ], where R i ¬= R j op - a scalar comparison operator, here (=) operator. Division predicate. [Qi op Qi]. where op - scalar comparison operator, set comparison operator, set membership and containment operator. Q i (or Q j ) may be a constant or a list of constants, provided - Qi (or Qi) has in its WHERE clause a join predicate which references the relation of the outer query block.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Nesting of the Query Blocks: predicates Only the nested predicate and the division predicate give rise to the nesting of query blocks. A nested predicate may cause one of four basic types of nesting, and a division predicate yields a fifth basic nesting.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 The inner query block Q does not contain a join predicate that references the relation of the outer query block the SELECT clause of Q indicates an aggregate function associated with the column name. Nesting of the Query Blocks: type – A nesting Ex.2 SELECT SNO FROM SHIPMENT WHERE PNO = (SELECT MAX(PN0) FROM PART WHERE PRICE > ‘25’) ; There is only one way to process a type-A nested query on a single processor: 1.the inner block has to be evaluated first. 2.the nested predicate of the outer block then becomes a simple predicate, since Q can be replaced by a constant. 3. the outer block then is no longer nested and can be processed completely. RiRi RjRj A (C k ) A type-A nested query (predicate) of depth 1 is graphically represented as follows.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Q does not contain a join predicate that references the relation of the outer query block the column name in the SELECT clause of Q does not have an aggregate function associated with it. Nesting of the Query Blocks: type – N nesting Ex.3 SELECT SNO FROM SHIPMENT WHERE PNO IS IN (SELECT PNO FROM PART WHERE PRICE > ‘25’) ; The System R approach to processing the above query is: 1. to process the inner query block Q in order to replace the type-N nested predicate, PNO IS IN Q, with a simple predicate PNO IS IN X, where X is the result of evaluating Q, 2. then to completely process the resulting nonnested query. RiRi RjRj N (C k ) The query graph for a type-N nested query of depth 1: If op is the set non-inclusion operator (IS NOT IN) requires a different treatment. RiRi RjRj N x (C k ) A type-N nested query of depth n(n >= 1) is defined as a nested query whose query graph consists of only straight arcs labeled ‘N’. RiRi RjRj N (C n ) RkRk N (C m ) Note!!! Note that Ex. 3 shows other formulations of the canonical query: SELECT SNO FROM SHIPMENT, PART WHERE SHIPMENT.PNO = PART.PNO AND PRICE > ‘25’; This observation has some interesting implications,as can be seen later.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 the WHERE clause of Q contains a join predicate that references the relation of the outer query block the column name in the SELECT clause of Q is not associated with an aggregate function, Nesting of the Query Blocks: type – J nesting Ex.4 SELECT SNO FROM SHIPMENT WHERE PNO IS IN (SELECT PNO FROM PROJECT WHERE SHIPMENT.SNO = PROJECT.JNO AND JLOC = ‘NEW YORK’) ; R i.C n = R j.C m RiRi RjRj N (C k ) The query graph for a type-J nested query of depth 1: The query graph for a type-J nested query of depth 1 involving the set non-inclusion operator. N x (C k ) R i.C n = R j.C m RjRj RiRi A type-J nested query of depth n(n >= 1) is defined as a nested query whose query graph consists of at least one circular arc and no straight arc labeled ‘A’. RjRj N (C n )N (C m ) RiRi RkRk R i.C n = R k.C m

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 the WHERE clause of Q contains a join predicate that references the relation of the outer query block an aggregate function is associated with the column name in the SELECT clause of Q. Nesting of the Query Blocks: type – JA nesting Ex.5 SELECT SNO FROM SHIPMENT WHERE PNO = (SELECT MAX( PNO) FROM PROJECT WHERE PROJECT.JNO = SHIPMENT.JNO AND JLOC = ‘NEW YORK’); RiRi RjRj A (C k ) The query graph for a type-JA nested query of depth 1: R i.C n = R j.C m A type-JA nested query of depth n(n >= 1) is defined as a nested query whose query graph exhibits at least one circular arc and at least one straight arc labeled ‘A’, RjRj N (C n )A (C m ) RiRi RkRk R i.C n = R k.C m

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 the join predicate in either Qi or Qj (or both) references the relation of the outer block. Nesting of the Query Blocks: type – D nesting Ex.6 SELECT SNAME FROM SUPPLIER WHERE (SELECT PNO FROM SHIPMENT WHERE SHIPMENT.SNO = SUPPLIER.SNO) CONTAINS (SELECT PNO FROM PART WHERE COLOR = ‘RED’ AND PRICE > ‘25’) ; A join predicate and a division predicate together give rise to a type-D nesting. T ype-D nested query expresses the relational division operation. RiRi A (C k ) R i.C n = R j.C m RkRk RjRj The query graph for a type-D nested query of depth 1:

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 LEMMA 1. Q 1 and Q 2 are equivalent; that is, they yield the same result. establishes the equivalence of the canonical and type-N nested form of a two-relation query in which the op is not the set non-inclusion operator,IS NOT IN. Let Q 1 be SELECT R i. C k FROM R i, R j WHERE R i.C h = R j.C m And let Q 2 be SELECT R i.C k FROM R, WHERE R i.C h ISIN (SELECT R j.C m FROM R j ); some query may be expressed in its canonical form or type-N nested form. Processing a Type-N or Type-J Nested Query: 1 Proof: By definition, the inner block of Q2 can be evaluated independently of the outer block and the result of evaluating it is X, a list of values in the C, column of Rj. Qz is then reduced to: SELECT Ri.Ck FROM Ri WHERE Ri.Ch ISIN X; The predicate Ri. Ch IS IN X is satisfied only if X contains a constant x such that Ri.Ch = X. That is, it can be satisfied only for those tuples of Ri and Rj which have common values in the Ch and Cm, columns, respectively. The join predicate Ri. Ch = Rj. Cm specifies exactly this condition.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 It is important to recognize that Lemma 1 only establishes that the type-N nested form of a query in which the op of the nested predicate is the set inclusion operator can be transformed to its canonical form. Processing a Type-N or Type-J Nested Query: 2 Lemma 1 suggests Algorithm NEST-N-J for transforming a type-N nested query of depth n - 1 to its canonical form.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Algorithm NEST-N-J: 1 Combine the FROM clauses of all query blocks into one FROM clause. 2 AND the WHERE clauses of all query blocks into one WHERE clause. 3 Replace [Ri.Ch op (SELECT Rj.Cm] by a join predicate [Ri.Ch, new-op Rj.Cm], and AND it to the combined WHERE clause obtained on step 2. Note that if op is IS IN, the corresponding new-op is ‘=‘; otherwise - the same as op. 4 Retain the SELECT clause of the outermost query block. Processing a Type-N or Type-J Nested Query: 3 SELECT Ri.Ck FROM Ri WHERE Ri.Ch IS IN (SELECT Rj.Cm FROM Rj WHERE Ri.Cn=Rj.CP); AND = Rj.Cm SELECT Ri.Ck FROM Ri, Rj WHERE Ri.Ch = Rj.Cm AND Ri.Cn=Rj.Cp);

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 SELECT SNAME FROM SUPPLIER WHERE SNO IS NOT IN (SELECT SNO FROM SHIPMENT WHERE PNO = ‘Pl’) ; Processing a Type-N or Type-J Nested Query: 4 alternative interpretation of the query is a type-D nested query. SELECT SNAME FROM SUPPLIER WHERE (SELECT PNO FROM SHIPMENT WHERE SHIPMENT.SNO = SUPPLIER.SNO) DOES NOT CONTAIN Pl; Pseudo-canonical form: SELECT SNAME FROM SUPPLIER, X WHERE ¬(SUPPLIER.SNO = X.SNO);

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a Type-N or Type-J Nested Query: 5 The set noninclusion operator in a type-N or type-J nested query requires an extension to Algorithm NEST-N-J. Now [Ri.Ck IS NOT IN (SELECT Rj.Ch] is replaced by the antijoin predicate ¬(Ri.Ck = Rj.Ch) and ANDed to the merged WHERE clause obtained on steps 2 and 3 of the algorithm. The result of a query is independent of the order in which its predicates are evaluated. However, an antijoin predicate must be evaluated only after all join predicates have been evaluated. SELECT Ri.Ck FROM Ri WHERE Ri.Ch IS NOT IN (SELECT Rj.Cm FROM Rj WHERE Rj.Cn = Ri.Cp);

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 LEMMA 2. Q 3 and Q 4 are equivalent; that is, they produce the same result. Let Q 3 be SELECT Ri.Ck FROM Ri WHERE Ri.Ch = (SELECT AGG(Rj.Cm) FROM Rj WHERE Rj.Cn = Ri.Cp); And let Q 4 be SELECT Ri.Ck FROM Ri WHERE Ri.Ch = (SELECT Rt.C 2 FROM Rt WHERE Rt.C1 = Ri.Cp); where R t (C 1, C 2 ) is obtained by R t (C 1, C 2 ) = (SELECT Rj.Cn, AGG(Rj.Cm) FROM Rj GROUP BY Rj.Cn); Processing a Type-JA Nested Query: 1 Lemma 2 directly leads to an algorithm which transforms a type-JA nested query of depth 1 to an equivalent type-J nested query of depth 1. PROOF. It is shown in Section 2 that the operation of Q3 may be thought of as first fetching a tuple of Ri and all tuples of Rj whose Cn column values are the same as the Cp column value of the Ri tuple, then applying the aggregate function AGG on the Cm column of the Rj tuples to obtain a constant X, and, finally, outputting the Ck value of the Ri tuple if x = C,, column value of the Ri tuple. Now RI is a binary relation of each distinct value in the Cn column of Rj and the corresponding value obtained by applying the aggregate function AGG on the Rj tuples. Then it is clear that the query may be processed by fetching each tuple of Ri, then fetching the Rt tuple whose C1column has the same value as the Cp column of the Ri tuple, and outputting the Ck column value of the Ri tuple if the C2 column value of the Rt tuple is the same as the Ch value of the Ri tuple. But this is exactly the operation of Q4.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Algorithm NEST-JA: 1 Generate a temporary relation R t ( C 1,..., C n, C n+1 ) from R 2 such that each C n+1 column value of R t is a constant obtained by applying the aggregate function AGG on the C n+1 column of the R 2 tuples which share a common value in columns C 1 through C n. In other words, the primary key of R t is its first n columns. 2 In inner block of the initial query change all references to R 2 columns in join predicates that reference R 1 to corresponding R t columns. Processing a Type-JA Nested Query: 2 SELECT R 1. C n+2 FROM R 1 WHERE R 1.C n+1 = (SELECT AGG(R 2.C n+1 ) FROM R 2 WHERE R 2.C 1 = R 1.C 1 AND R 2.C 2 = R 1.C 2 AND : R 2.C n = R 1.C n ); (SELECT R t.C n+1 FROM R t WHERE R t.C 1 =R 1.C 1 AND R t.C 2 =R 1.C 2 AND R t.C n = R 1.C n ); R t (C 1,..., C n, C n+1 ) = (SELECT C 1,..., C n, AGG(C n+1 ) FROM R 2 GROUP BY C 1,..., C n ) ;

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 transformation of a type-JA nested query of depth 2 Processing a Type-JA Nested Query: 3 N A A A A N type-JA (NA) type-JA (AA) type-JA (AN) N N type-J NEST- JA A N type-JA(AN) NEST- JA A type-JA NEST- N -J

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Algorithm for transforming a type-JA query of depth n to an equivalent type-J Processing a Type-JA Nested Query: 4 A/N Algorithm NEST-JA(G) I = n (the nesting depth of the query); DO WHILE (there-is-at-least-one-straight-arc-labeled-A); IF the I-th straight arc is labeled N THEN I = I - 1; ELSE DO; Apply Algorithms NEST-JA and (NEST-N-J) to the n - I + 1 nodes to the right of the I-th straight arc; The I-th straight arc of the resulting query of depth I is labeled N; n = I; END; N N A N N N N

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 LEMMA 3. Q 5 and Q 6 are equivalent; that is, they produce the same result. Let Q 5 be SELECT Ri.Ck FROM Ri WHERE (SELECT R j.C h FROM R j WHERE R j.C n =R i.C p ) op (SELECT R k.C m FROM R k ); And let Q 6 be SELECT R i.C k FROM R i WHERE R i.C p = (SELECT C 1 FROM R t ) ; where Rt is obtained as: R t (C 1 ) = (SELECT R j.C n FROM R j.RX WHERE (SELECT R j. C h FROM R j. RY WHERE RY.C n = RX.C n ) op (SELECT R k.C m FROM R k ) ; Processing a Type-D Nested Query: 1 Lemma 3 provides the basis for Algorithm NEST-D, which transforms a general type-D nested query to an equivalent canonical two-relation query. PROOF. As has been shown, the operation of Q 5 may be thought of as fetching each tuple of Ri and checking whether the division predicate is satisfied by the C p column value of the tuple. But what if there is a list of the C n column values of Rj which satisfy the division predicate? Then all that needs to be done is to fetch each Ri tuple and determine whether the C p column value of the tuple is in the list. But this is precisely the operation of Qs, since Rt is just such a list.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Algorithm NEST-D 1. Generate a temporary relation R t1 C1,..., Cm): R tl (C 1,...,Cm) = (SELECT C l,..., Cm FROM R 3 ); Also generate R t2 (C1,..,Cn) : R t2 (C1,..., Cn) = (SELECT C 1,..., Cn FROM R 2 ) ; 2. Divide R t2 (C 1,...,Cn) by R tl (C 1,..., Cm). The result is a new temporary relation R t3 (C m+1,..., Cn). 3.Transform the initial query to canonical - drop the query block on R 3, - replace all references to columns of R 2 to corresponding columns of R t3 in the query block on R 2, - eliminate the SELECT and FROM clauses of the query block on R 2. The FROM clause of the resulting canonical query must now include R t3 as well. Processing a Type-N or Type-J Nested Query: 3 SELECT R I. C 1 FROM RI WHERE (SELECT R 2. C 1 FROM R 2 WHERE R 2.C 2 = R 1 C 2 AND R 2.C 3 = R1.C 3 AND R 2.C n = RI.Cn) = (SELECT R 3. C 1 FROM R 3 WHERE R 3.C 2 = R1.C 2 AND R 3.C 3 = R1.C 3 AND R 3. C m = R1.C m ); SELECT R1.C1 FROM R1.Rt3 WHERE R1.C m+I = R t3. C m+1 AND R1.C m+2 = Rt 3.C m+2 AND... R1.Cn = Rt3.Cn

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 1 Algorithm NEST-G 1.Transform each type-A predicate to a simple predicate by evaluating the Q represented by the right-hand node on the query graph for the subquery (predicate). then eliminate Q and the straight arc labeled ‘A’, leading to Q from the outermost query block. 2.Transform each type-JA nested subquery to an equivalent type-N or type-J subquery by Algorithm NEST-JA(G). The query graph for the resulting type-N or type-J nested subquery replaces the query graph for the initial type-JA subquery. 3. Transform each type-D nested subquery to its canonical form by Algorithm NEST-D. Replace the division predicate by an appropriate set of join predicates, and remove from the query graph of the initial subquery the two right-hand nodes and both the straight and the circular arc leading to them. 4. Transform the resulting query, which consists only of type-N and type-J subqueries, to an equivalent canonical query by Algorithm NEST-N-J.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 2 SELECT R 1. C 1 FROM R 1 WHERE R 1.C 2 ISIN (SELECT R 2.C 2 FROM R 2 WHERE R 2. C 3 = (SELECT AGG(R 3.C 3 ) FROM R 2 WHERE R 3.C 4 = R 1.C 4 ) AND R 2.C 4 IS IN (SELECT R 4.C 4, FROM Rd) AND R 1.C 3 = (SELECT AGG(R 5. C 5 ) FROM R 5 WHERE R 5.C 6 IS NOT IN (SELECT R 6.C 6 FROM R 6 )) AND (SELECT R 7.C 7 FROM R 7 WHERE R 7.C 8 = R 1.C 5 ) (SELECT= R 8.C 8 FROM R 8 );

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 3 N(C 2 ) A(C 3 ) R3R3 R8R8 R5R5 R7R7 R6R6 R4R4 R2R2 R1R1 N(C 4 ) Nx(C 6 ) R 1.C 4 = R 3.C 4 D R 1.C 5 = R 7.C 8

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 4 N(C2 ) A(C3 ) R3R3 R8R8 R5R5 R7R7 R6R6 R4R4 R2R2 R1R1 N(C 4 ) Nx(C 6 ) R 1.C 4 = R 3.C 4 D R 1.C 5 = R 7.C 8 type-JA depth 2  type-J equivalent). NEST-JA(G) type-JA depth 1  conjunction of join predicates NEST-JA, R 2.C 3 = R t1.C 2 AND R t1.C 1 = R 1. C 4 where the temporary relation R t1 is obtained by R t1 (C 1, C 2 ) = (SELECT R 3.C 4, AGG(R 3.C 3 ) FROM GROUP BY R 3.C 4 ); type-N depth1  join predicate, R 2.C 4 = R 4.C 4 NEST-N-J Then type-JA depth 2  type-J depth 1 NEST-N-J. R 1.C 2 IS IN (SELECT R 2.C 2 FROM R 2, R 4, R t1 WHERE R 2. C 3 = R t1.C 2 AND R t1.C 1 = R 1.C 4 AND R 2.C 4 = R 4.C 4 );

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 5 N(C 2 ) R8R8 R5R5 R7R7 R6R6 R 2 R 4 R t1 R1R1 Nx(C 6 ) R 2.C 3 = R t1.C 2 AND R t1.C 1 = R 1.C 4 AND R 2.C 4 = R 4.C 4 D R 1.C 5 = R 7.C 8 A(C 5 ) N(C2 ) A(C3 ) R3R3 R8R8 R5R5 R7R7 R6R6 R4R4 R2R2 R1R1 N(C 4 ) Nx(C 6 ) R 1.C 4 = R 3.C 4 D R 1.C 5 = R 7.C 8

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 6 N(C 2 ) R8R8 R5R5 R7R7 R6R6 R 2 R 4 R t1 R1R1 Nx(C 6 ) R 2.C 3 = R t1.C 2 AND R t1.C 1 = R 1.C 4 AND R 2.C 4 = R 4.C 4 D R 1.C 5 = R 7.C 8 A(C 5 ) type-A depth 2 is evaluated. type-N  SELECT AGG(R 5.C 5 ) recursive call FROM R 5, R 6 NEST-G WHERE ¬(R 5.C 6 = R 6.C 6 ); The transformed node is then evaluated to a constant X, and the nested predicate of the initial type-A subquery becomes a simple predicate, R 1.C 3 = x.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 7 N(C 2 ) R8R8 R7R7 R 2 R 4 R t1 R1R1 R 2.C 3 = R t1.C 2 AND R t1.C 1 = R 1.C 4 AND R 2.C 4 = R 4.C 4 AND R 1.C 3 = x D R 1.C 5 = R 7.C 8 N(C 2 ) R8R8 R5R5 R7R7 R6R6 R 2 R 4 R t1 R1R1 Nx(C 6 ) R 2.C 3 = R t1.C 2 AND R t1.C 1 = R 1.C 4 AND R 2.C 4 = R 4.C 4 D R 1.C 5 = R 7.C 8 A(C 5 )

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Processing a General Nested Query: 6 Then, type-D subquery is evaluated so as to replace the division predicate with a join predicate by Algorithm NEST-D. The resulting join predicate is RI. CS = Rn. Cl, where the unary relation Ra(C,) is the quotient of dividing RT(CB, CT) by RdCs The resulting query is type-J nested and Algorithm NEST-N-J can be used to transform it into its canonical equivalent, shown in the following. N(C 2 ) R8R8 R7R7 R 2 R 4 R t1 R1R1 R 2.C 3 = R t1.C 2 AND R t1.C 1 = R 1.C 4 AND R 2.C 4 = R 4.C 4 AND R 1.C 3 = x D R 1.C 5 = R 7.C 8 SELECT R 1.C 1 FROM R 1, R t1, R t2 WHERE R 1.C 2 = R 2.C 2 AND R 2.C 3 = R t1.C 2 AND R t1.C 1 =R 1.C 4 AND R 2.C 4 = R 4.C 4 AND R 1.C 3 = x AND R 1.C 5 = R t2.C 1 ;

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Performance improvements: Consider Q3 and Q4: Let P, = 50, Pj = 30, Pt = 5, B = 6, and fi.Ni = 100. The nested-iteration method of processing Q3 is, worst case, 3050 page fetches. Q4 incurs 615 page fetches. type-D nested query: Let Pi = 50, Pj = Pn = 30, pt = 5, Pk = Pts = 1, B = 4, and fi.Ni = The nested-iteration method requires,in the worst case,30051 page fetches. Processing the type-D nested query by the algorithms shown here is 751 page I/OS.

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Thank you for the attention!!!