CPS216: Data-Intensive Computing Systems Query Processing (contd.)

Slides:



Advertisements
Similar presentations
CS CS4432: Database Systems II Logical Plan Rewriting.
Advertisements

Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
CS 540 Database Management Systems
Query Execution Optimizing Performance. Resolving an SQL query Since our SQL queries are very high level, the query processor must do a lot of additional.
COMP 451/651 Optimizing Performance
CS Spring 2002Notes 61 CS 277: Database System Implementation Notes 6: Query Processing Arthur Keller.
CS 4432query processing - lecture 131 CS4432: Database Systems II Lecture #13 Query Processing Professor Elke A. Rundensteiner.
CS 4432query processing1 CS4432: Database Systems II.
CS 4432logical query rewriting - lecture 151 CS4432: Database Systems II Lecture #15 Logical Query Rewriting Professor Elke A. Rundensteiner.
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
Query Execution Chapter 15 Section 15.1 Presented by Khadke, Suvarna CS 257 (Section II) Id
CS 4432query processing - lecture 121 CS4432: Database Systems II Lecture #12 Query Processing Professor Elke A. Rundensteiner.
CPS216: Advanced Database Systems Notes 03:Query Processing (Overview, contd.) Shivnath Babu.
CPS216: Advanced Database Systems Notes 06:Query Execution (Sort and Join operators) Shivnath Babu.
Access Path Selection in a Relational Database Management System Selinger et al.
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
Advanced Database Systems Notes:Query Processing (Overview) Shivnath Babu.
CPS216: Advanced Database Systems Notes 07:Query Execution Shivnath Babu.
CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites) Shivnath Babu.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
CPS216: Data-Intensive Computing Systems Introduction to Query Processing Shivnath Babu.
Query Execution Section 15.1 Shweta Athalye CS257: Database Systems ID: 118 Section 1.
Chapters 15-16a1 (Slides by Hector Garcia-Molina, Chapters 15 and 16: Query Processing.
CPS216: Advanced Database Systems Notes 09:Query Optimization (Cost-based optimization) Shivnath Babu.
CS4432: Database Systems II Query Processing- Part 2.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
Data Engineering SQL Query Processing Shivnath Babu.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
CS 540 Database Management Systems
CS4432: Database Systems II Query Processing- Part 1 1.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
CPS216: Advanced Database Systems Notes 02:Query Processing (Overview) Shivnath Babu.
1 Ullman et al. : Database System Principles Notes 6: Query Processing.
1/14/2005Yan Huang - CSCI5330 Database Implementation – Query Optimization Query Optimization.
Query Execution Chapter 15 Section 15.1 Presented by Khadke, Suvarna CS 257 (Section II) Id
Chapter 14: Query Optimization
Advanced Database Systems: DBS CB, 2nd Edition
CPS216: Data-intensive Computing Systems
CS 540 Database Management Systems
CS 440 Database Management Systems
Prepared by : Ankit Patel (226)
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Data Engineering Query Optimization (Cost-based optimization)
CS 245: Database System Principles
Overview of Query Optimization
Chapter 15 QUERY EXECUTION.
Introduction to Database Systems
April 20th – RDBMS Internals
Query Optimization Overview
CS143:Evaluation and Optimization
Focus: Relational System
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
Query Optimization Overview
CS 245: Database System Principles
Relational Query Optimization
Database Management Systems (CS 564)
Query Execution Presented by Jiten Oswal CS 257 Chapter 15
Relational Query Optimization
Monday, 5/13/2002 Hash table indexes, query optimization
Yan Huang - CSCI5330 Database Implementation – Query Processing
CPS216: Advanced Database Systems Notes 03:Query Processing (Overview, contd.) Shivnath Babu.
CS222: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
CPS216: Data-Intensive Computing Systems Query Processing (Overview)
Relational Query Optimization
Relational Query Optimization
Presentation transcript:

CPS216: Data-Intensive Computing Systems Query Processing (contd.) Shivnath Babu

Physical plan generation Overview of Query Processing SQL query parse parse tree Query rewriting Query Optimization statistics logical query plan Physical plan generation physical query plan execute Query Execution result

Physical plan generation SQL query Initial logical plan parse Rewrite rules parse tree Query rewriting Logical plan statistics logical query plan Physical plan generation “Best” logical plan physical query plan execute result

B,D B,D R.C = S.C R.A = “c” Λ R.C = S.C R.A = “c” Query Rewriting X X R S R S We will revisit it towards the end of this lecture

Physical plan generation SQL query parse parse tree Query rewriting statistics Best logical query plan Physical plan generation Best physical query plan execute result

Physical Plan Generation B,D Project Natural join Hash join R.A = “c” S Index scan Table scan R R S Best logical plan

Physical plan generation SQL query parse parse tree Enumerate possible physical plans Query rewriting statistics Best logical query plan Find the cost of each plan Physical plan generation Best physical query plan Pick plan with minimum cost execute result

Physical Plan Generation Logical Query Plan P1 P2 …. Pn C1 C2 …. Cn Pick minimum cost one Physical plans Costs

Plans for Query Execution Roadmap Path of a SQL query Operator trees Physical Vs Logical plans Plumbing: Materialization Vs pipelining

Logical Plans Vs. Physical Plans B,D Project Natural join Hash join R.A = “c” S Index scan Table scan R R S Best logical plan

Operator Plumbing B,D R.A = “c” S R Materialization: output of one operator written to disk, next operator reads from the disk Pipelining: output of one operator directly fed to next operator

Materialization B,D Materialized here R.A = “c” S R

Iterators: Pipelining B,D  Each operator supports: Open() GetNext() Close() R.A = “c” S R

Iterator for Table Scan (R) Open() { /** initialize variables */ b = first block of R; t = first tuple in block b; } GetNext() { IF (t is past last tuple in block b) { set b to next block; IF (there is no next block) /** no more tuples */ RETURN EOT; ELSE t = first tuple in b; } /** return current tuple */ oldt = t; set t to next tuple in block b; RETURN oldt; Close() { /** nothing to be done */ }

Iterator for Select R.A = “c” GetNext() { LOOP: t = Child.GetNext(); IF (t == EOT) { /** no more tuples */ RETURN EOT; } ELSE IF (t.A == “c”) RETURN t; ENDLOOP: Open() { /** initialize child */ Child.Open(); } Close() { /** inform child */ Child.Close(); }

Iterator for Sort R.A GetNext() { IF (more tuples) RETURN next tuple in order; ELSE RETURN EOT; } Open() { /** Bulk of the work is here */ Child.Open(); Read all tuples from Child and sort them } Close() { /** inform child */ Child.Close(); }

Iterator for Tuple Nested Loop Join Lexp Rexp TNLJ (conceptually) for each r  Lexp do for each s  Rexp do if Lexp.C = Rexp.C, output r,s

Example 1: Left-Deep Plan TNLJ TableScan TNLJ R3(C,D) TableScan TableScan R1(A,B) R2(B,C) Question: What is the sequence of getNext() calls?

Example 2: Right-Deep Plan TNLJ R3(C,D) TableScan R1(A,B) TableScan R2(B,C) TNLJ Question: What is the sequence of getNext() calls?

Cost Measure for a Physical Plan There are many cost measures Time to completion Number of I/Os (we will see a lot of this) Number of getNext() calls Tradeoff: Simplicity of estimation Vs. Accurate estimation of performance as seen by user

Why do we need Query Rewriting? Pruning the HUGE space of physical plans Eliminating redundant conditions/operators Rules that will improve performance with very high probability Preprocessing Getting queries into a form that we know how to handle best Reduces optimization time drastically without noticeably affecting quality

Some Query Rewrite Rules Transform one logical plan into another Do not use statistics Equivalences in relational algebra Push-down predicates Do projects early Avoid cross-products if possible

Equivalences in Relational Algebra R S = S R Commutativity (R S) T = R (S T) Associativity Also holds for: Cross Products, Union, Intersection R x S = S x R (R x S) x T = R x (S x T) R U S = S U R R U (S U T) = (R U S) U T

B,D B,D R.C = S.C R.A = “c” Λ R.C = S.C R.A = “c” Apply Rewrite Rule (1) B,D B,D R.C = S.C R.A = “c” Λ R.C = S.C R.A = “c” X X R S R S B,D [ sR.C=S.C [R.A=“c”(R X S)]]

pxy (R) = px [py (R)] Rules: Project Let: X = set of attributes Y = set of attributes XY = X U Y pxy (R) = px [py (R)]

sp (R S) = sq (R S) = Rules: s + combined Let p = predicate with only R attribs q = predicate with only S attribs m = predicate with only R,S attribs sp (R S) = sq (R S) = [sp (R)] S R [sq (S)]

B,D B,D R.C = S.C R.C = S.C R.A = “c” R.A = “c” Apply Rewrite Rule (2) B,D B,D R.C = S.C R.C = S.C R.A = “c” X R.A = “c” S X R S R B,D [ sR.C=S.C [R.A=“c”(R)] X S]

B,D B,D R.C = S.C R.A = “c” R.A = “c” Apply Rewrite Rule (3) X S Natural join R.A = “c” X S R.A = “c” S R R B,D [[R.A=“c”(R)] S]

Rules: s + combined (continued) spq (R S) = [sp (R)] [sq (S)] spqm (R S) = sm [(sp R) (sq S)] spvq (R S) = [(sp R) S] U [R (sq S)]

Which are “good” transformations? sp1p2 (R)  sp1 [sp2 (R)] sp (R S)  [sp (R)] S R S  S R px [sp (R)]  px {sp [pxz (R)]}

Conventional wisdom: do projects early Example: R(A,B,C,D,E) P: (A=3)  (B=“cat”) pE {sp (R)} vs. pE {sp{pABE(R)}}

But: What if we have A, B indexes? B = “cat” A=3 Intersect pointers to get pointers to matching tuples

Bottom line: No transformation is always good Some are usually good: Push selections down Avoid cross-products if possible Subqueries  Joins

Avoid Cross Products (if possible) Select B,D From R,S,T,U Where R.A = S.B  R.C=T.C  R.D = U.D Which join trees avoid cross-products? If you can't avoid cross products, perform them as late as possible

More Query Rewrite Rules Transform one logical plan into another Do not use statistics Equivalences in relational algebra Push-down predicates Do projects early Avoid cross-products if possible Use left-deep trees Subqueries  Joins Use of constraints, e.g., uniqueness