Magic Decorrelation: An Optimization Technique for SQL queries CS525 Lecture WPI.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
CPSC 504: Data Management Discussion on Chandra&Merlin 1977 Laks V.S. Lakshmanan Dept. of CS UBC.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
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.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Chapter 11 Group Functions
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CSEN 5314 Quiz 4.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
Initial QGM. Q1.CDept=‘mgmt’ And Q1.Grade>=Q2.$1 took Max took Q1 Q2 Q3 SELECT Grade FROM took Q3 WHERE Q3.CName=Q1.CName SELECT Max(Grade) AS Grade FROM.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Unary Query Processing Operators Not in the Textbook!
Database Systems More SQL Database Design -- More SQL1.
Copyright © Cengage Learning. All rights reserved.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Chapter 3 Single-Table Queries
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
Database Management COP4540, SCS, FIU SQL (Continued) Querying Multiple Tables.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
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,
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
Designing Streamable XPath Expressions Roger L. Costello January 5,
CC L A W EB DE D ATOS P RIMAVERA 2015 Lecture 7: SPARQL (1.0) Aidan Hogan
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Unary Query Processing Operators
CS580 Advanced Database Topics
Chapter # 6 The Relational Algebra and Calculus
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Day 4 - Simple Queries & More on Tables
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Writing Correlated Subqueries
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Extensible/Rule Based Query Rewrite Optimization in Starburst
SQL: Structured Query Language
Writing Basic SQL SELECT Statements
Access/SQL Server Eliminate Duplicates with SELECT DISTINCT
Advance Database Systems
Lecture 5- Query Optimization (continued)
Subqueries.
Presentation transcript:

Magic Decorrelation: An Optimization Technique for SQL queries CS525 Lecture WPI

DSRG, WPI 2 Query Graph Model (QGM) Internal representation of an SQL query used within IBM DB2 One box for each “query block” and for each aggregation Each box has a “head” and a “body” Head says what is returned by the box Body describes the computation performed DISTINT specified to work with bags Head specifies DISTINCT = TRUE/FALSE If DISTINCT = TRUE, then there will be no duplicates in the result returned by the box If DISTINCT = FALSE, then duplicates may be present in the results of the box

DSRG, WPI 3 QGM (Contd…) DISTINCT specified to work with bags Head specifies DISTINCT = TRUE/FALSE If DISTINCT = TRUE, then there will be no duplicates in the result returned by the box If DISTINCT = FALSE, then duplicates may be present in the results of the box Body specifies DISTINCT = ENFORCE/PRESERVE/PERMIT If DISTINCT = ENFORCE, then the body explicitly removes duplicates If DISTINCT = PRESERVE, then the body does not add/remove duplicates obtained from the operation If DISTINCT = PERMIT, then the body may add/remove duplicates

DSRG, WPI 4 QGM Example 1 Consider the following SQL query: SELECT DISTINCT q1.PartNo, q1.Desc, q2.SuppNo FROM inventory q1, quotations q2 WHERE q1.PartNo = q2.PartNo AND q1.Desc = 'engine' AND q2.price <= ALL (SELECT q3.Price FROM quotations q3 WHERE q2.PartNo = q3.PartNo)

DSRG, WPI 5 QGM Example 1 (Contd…) Note the following Box 3 corresponds to the outer block Box 3 has DISTINCT = TRUE for head, and DISTINCT = ENFORCE for the body. Box 3 takes 3 inputs – inventory, quotations and output of box 4 See the quantifiers specifying q1(F) (denoting q1 is from the FROM clause), q4(A) denotes the specified condition must be true for all the rows in q4. Box 4 corresponds to the inner block DISTINCT = PERMIT for the body of box 4. See the input from box 3 to box 4 denoting correlation

DSRG, WPI 6 QGM Example 2 Consider the following correlated query with aggregation. SELECT d.Name FROM dept d WHERE d.Budget (SELECT COUNT (*) FROM emp e WHERE d.Building = e.Building)

DSRG, WPI 7 QGM Example 2 (Contd…) (1) (2) (3)

DSRG, WPI 8 Some terminology Box (3) is directly correlated to Box (1) as it uses the input from (1). Box (3) and Box (2) are said to be correlated to Box (1) because at least one of the descendants of (3) and (2) are directly correlated to (1) q1.Building is the correlation column

DSRG, WPI 9 Removing Correlation Traverse the QGM in depth first order For our example, visit the boxes in the order (1), (2), (3) For each box A, check if a (descendant) box B is correlated to it/its ancestor. If yes, then feed the correlation to its child (if any) If A is correlated to an (ancestor) box, then Absorb the correlation for this box. Note Absorb will be different depending on whether the box is aggregate box/SPJ box

DSRG, WPI 10 Removing Decorrelation We first visit box (1). It has a descendant box, that is correlated to it. So perform the feed Box (1) is not correlated to an ancestor box, so there is no absorb Let us see how feed for box (1) is performed

DSRG, WPI 11 Feed for (1) Check if there is any condition on the “correlation” column in Box (1) If yes, push the selection condition before Box (1) – check figure (b) Create another box, which removes duplicate values of the correlation column – magic in figure (c) Create 2 boxes as in figure (d) DCO box takes the above values as input, box (3) will now depend on this box C1 box takes output of DCO box, is correlated to box (1) and performs the equi-join

DSRG, WPI 12 Pushing the Selection Condition (1) (2)

DSRG, WPI 13 Removing Duplicates (1) (2)

DSRG, WPI 14 Removing the correlation between (1) and (3) (1) (2)

DSRG, WPI 15 Decorrelating Box (2) Box (3) is correlated to the parent DCO box of (2). So perform the feed – fig [b] Push select conditions – here none Remove duplicates – here none Create a DCO box and a C1 box as before. Box (2) is correlated to its parent DCO box. So perform the absorb – fig [c] For aggregate operator, absorb includes a group by, followed by a LOJ In this case, we end up with an unnecessary C1 box. Remove it – fig (d)

DSRG, WPI 16 Starting point for box (2) (2) (3)

DSRG, WPI 17 Feed for Box (2) (2) (3)

DSRG, WPI 18 Absorb for box (2) (2) (3)

DSRG, WPI 19 Remove unnecessary C1 box (2) (3)

DSRG, WPI 20 Decorrelating Box (3) There is no descendant box that is correlation to box (3) or its ancestor. Therefore, no feed Box (3) is correlated to its parent DCO box. So perform the absorb – fig (b) Absorb for SPJ box means just remove the correlation, and feed the box directly as input to the SPJ box Remove unnecessary Q8 input to DCO box – fig (c) Remove unnecessary DCO box – fig (d)

DSRG, WPI 21 Starting point for box (3) (3)

DSRG, WPI 22 Absorb for box (3) (3)

DSRG, WPI 23 Remove unnecessary Q8 input to DCO box (3)

DSRG, WPI 24 Remove unnecessary DCO box (3)