Data Engineering SQL Query Processing Shivnath Babu.

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
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Query Compiler. The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical.
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
CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 183 Database Systems II Query Compiler.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
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
CS405G: Introduction to Database Systems Final Review.
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.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
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.
Database systems/COMP4910/Melikyan1 Relational Query Optimization How are SQL queries are translated into relational algebra? How does the optimizer estimates.
Advanced Database Systems Notes:Query Processing (Overview) Shivnath Babu.
CPS216: Advanced Database Systems Notes 07:Query Execution Shivnath Babu.
CPS216: Advanced Database Systems Notes 02:Query Processing (Overview) 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.
Query Optimization Chap. 19. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying where.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
SCUHolliday - COEN 17814–1 Schedule Today: u Query Processing overview.
DBMS 2001Notes 5: Query Processing1 Principles of Database Management Systems 5: Query Processing Pekka Kilpeläinen (partially based on Stanford CS245.
CPS216: Data-Intensive Computing Systems Introduction to Query Processing Shivnath Babu.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
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.
Spark SQL: Relational Data Processing in Spark Michael Armbrust, Reynold Xin, Cheng Lian, Yin Huai, Davies Liu, Joseph K. Bradley, Xiangrui Meng, Tomer.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
Query Processing CS 405G Introduction to Database Systems.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
Query Processing – Implementing Set Operations and Joins Chap. 19.
CS 540 Database Management Systems
Lecture 15: Query Optimization. Very Big Picture Usually, there are many possible query execution plans. The optimizer is trying to chose a good one.
Chapter 13: Query Processing
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
CS4432: Database Systems II
CS 440 Database Management Systems
Data Engineering Query Optimization (Cost-based optimization)
CS 245: Database System Principles
Overview of Query Optimization
Chapter 15 QUERY EXECUTION.
CS143:Evaluation and Optimization
Focus: Relational System
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
CS 245: Database System Principles
Query Optimization CS 157B Ch. 14 Mien Siao.
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
CPS216: Data-Intensive Computing Systems Query Processing (contd.)
Yan Huang - CSCI5330 Database Implementation – Query Processing
CPS216: Advanced Database Systems Notes 03:Query Processing (Overview, contd.) Shivnath Babu.
CPS216: Data-Intensive Computing Systems Query Processing (Overview)
Presentation transcript:

Data Engineering SQL Query Processing Shivnath Babu

: Declarative Big Data Processing Let Developers Create and Run Spark Programs Faster: Write less code Read less data Let the optimizer do the hard work 2 SQL

Write Less Code: Compute an Average private IntWritable one = new IntWritable(1) private IntWritable output = new IntWritable() proctected void map( LongWritable key, Text value, Context context) { String[] fields = value.split("\t") output.set(Integer.parseInt(fields[1])) context.write(one, output) } IntWritable one = new IntWritable(1) DoubleWritable average = new DoubleWritable() protected void reduce( IntWritable key, Iterable values, Context context) { int sum = 0 int count = 0 for(IntWritable value : values) { sum += value.get() count++ } average.set(sum / (double) count) context.Write(key, average) } Using RDDs data = sc.textFile(...).split("\t") data.map(lambda x: (x[0], [int(x[1]), 1])) \.reduceByKey(lambda x, y: [x[0] + y[0], x[1] + y[1]]) \.map(lambda x: [x[0], x[1][0] / x[1][1]]) \.collect() Using SQL SELECT name, avg(age) FROM people GROUP BY name

4 Using RDDs data = sc.textFile(...).split("\t") data.map(lambda x: (x[0], [int(x[1]), 1])) \.reduceByKey(lambda x, y: [x[0] + y[0], x[1] + y[1]]) \.map(lambda x: [x[0], x[1][0] / x[1][1]]) \.collect() Using SQL SELECT name, avg(age) FROM people GROUP BY name Write Less Code: Compute an Average

Spark Seamlessly Integrates SQL with a Full Programming Language Embedding in a full programming language allows composition with functions to develop complex programs 5 zipToCity = udf(lambda city: ) def add_demographics(events): u = sqlCtx.table("users") events \.join(u, events.user_id == u.user_id) \.withColumn("city", zipToCity(df.zip))

6 def add_demographics(events): u = sqlCtx.table("users") # Load partitioned table events \.join(u, events.user_id == u.user_id) \ # Join on user_id.withColumn("city", zipToCity(df.zip)) # Run func to add city column Physical Plan with Predicate Pushdown and Column Pruning join optimized scan (events) optimized scan (users) events = add_demographics(sqlCtx.load("/data/events", "parquet")) training_data = events.where(events.city == "Melbourne").select(events.timestamp).collect() Logical Plan filter join events fileusers table Physical Plan join scan (events) filter scan (users)

Plan Optimization & Execution 7 SQL AST DataFrame Unresolved Logical Plan Logical Plan Optimized Logical Plan RDDs Selected Physical Plan Analysis Logical Optimization Physical Planning Cost Model Physical Plans Code Generation Catalog

Query Processing Declarative SQL Query  Query Plan

SQL Primer Select From Where Example Filter Query over R(A,B,C): Select B From R Where R.A = “c”  R.C > 10 SPJ, or Select-Project-Join Queries

SQL Primer (contd.) Select From Where Example Join Query over R(A,B,C) and S(C,D,E): Select B, D From R, S Where R.A = “c”  S.E = 2  R.C = S.C SPJ, or Select-Project-Join-Queries

RABC S CDE a11010x2 b12020y2 c21030z2 d23540x1 e34550y3 AnswerB D 2 x Select B,D From R,S Where R.A = “c”  S.E = 2  R.C=S.C

How do we execute this query? - Do Cartesian product - Select tuples - Do projection One idea Select B,D From R,S Where R.A = “c”  S.E = 2  R.C=S.C

R X SR.AR.BR.CS.CS.DS.E a x 2 a y 2. c x 2. Bingo! Got one... Select B,D From R,S Where R.A = “c”  S.E = 2  R.C=S.C

Relational Algebra - can be used to describe plans Ex: Plan I  B,D  R.A =“c”  S.E=2  R.C=S.C  X RS

Relational Algebra Primer Select:  R.A =“c”  R.C=10 Project:  B,D Cartesian Product: R X S Natural Join: R S

Relational Algebra - can be used to describe plans Ex: Plan I  B,D  R.A =“c”  S.E=2  R.C=S.C  X RS OR:  B,D [  R.A=“c”  S.E=2  R.C = S.C (RXS)]

Another idea:  B,D  R.A = “c”  S.E = 2 R(A,B,C) S(C,D,E) Plan II natural join Select B,D From R,S Where R.A = “c”  S.E = 2  R.C=S.C

R S A B C  ( R )  ( S ) C D E a 1 10 A B C C D E 10 x 2 b 1 20c x 2 20 y 2 c y 2 30 z 2 d z 2 40 x 1 e y 3 Select B,D From R,S Where R.A = “c”  S.E = 2  R.C=S.C

Plan III Use R.A and S.C Indexes (1) Use R.A index to select R tuples with R.A = “c” (2) For each R.C value found, use S.C index to find matching tuples (3) Eliminate S tuples S.E  2 (4) Join matching R,S tuples, project B,D attributes, and place in result

R S A B C C D E a x 2 b y 2 c z 2 d x 1 e y 3 c 7 15 AC I1I1 I2I2 =“c”=“c” check=2? output: next tuple:

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

Example Query Select B,D From R,S Where R.A = “c”  R.C=S.C

Example: AST: Abstract Syntax Tree SELECT FROM WHERE AND B R S R.A=“c”“c” R.CS.C= D Select B,D From R,S Where R.A = “c”  R.C=S.C

Along with Parsing … Semantic checks –Do the projected attributes exist in the relations in the From clause? –Ambiguous attributes? –Type checking, ex: R.A > 17.5 Expand views

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

Initial Logical Plan Relational Algebra :  B,D [  R.A=“c”  R.C = S.C (RXS)] Select B,D From R,S Where R.A = “c”  R.C=S.C  B,D  R.A = “c” Λ R.C = S.C X RS

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

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

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

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 =SR 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

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

Rules: Project Let: X = set of attributes Y = set of attributes XY = X U Y  xy (R) =  x [  y (R)]

Let p = predicate with only R attribs q = predicate with only S attribs m = predicate with only R,S attribs  p (R S) =  q (R S) = Rules:  combined [  p (R)] S R [  q (S)]

Rules:  combined (continued)  p  q (R S) = [  p (R)] [  q (S)]  p  q  m (R S) =  m [ (  p R) (  q S) ]  pvq (R S) = [ (  p R) S ] U [ R (  q S) ]

 p1  p2 (R)   p1 [  p2 (R)]  p (R S)  [  p (R)] S R S  S R  x [  p (R)]   x {  p [  xz (R)] } Which are “good” transformations?

Conventional wisdom: do projects early Example: R(A,B,C,D,E) P: (A=3)  (B=“cat”)  E {  p (R)} vs.  E {  p {  ABE (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) Which join trees avoid cross-products? If you can't avoid cross products, perform them as late as possible Select B,D From R,S,T,U Where R.A = S.B  R.C=T.C  R.D = U.D

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

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

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

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

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

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

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

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

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 Open() { /** initialize child */ Child.Open(); } GetNext() { LOOP: t = Child.GetNext(); IF (t == EOT) { /** no more tuples */ RETURN EOT; } ELSE IF (t.A == “c”) RETURN t; ENDLOOP: } Close() { /** inform child */ Child.Close(); }  R.A = “c”

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

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

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

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

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