Introduction to Database Systems

Slides:



Advertisements
Similar presentations
Examples of Physical Query Plan Alternatives
Advertisements

Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Query Optimization Goal: Declarative SQL query
1 Overview of Query Evaluation Chapter Objectives  Preliminaries:  Core query processing techniques  Catalog  Access paths to data  Index matching.
1 Relational Query Optimization Module 5, Lecture 2.
Relational Query Optimization CS186, Fall 2005 R & G Chapters 12/15.
Relational Query Optimization 198:541. Overview of Query Optimization  Plan: Tree of R.A. ops, with choice of alg for each op. Each operator typically.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
1 Implementation of Relational Operations Module 5, Lecture 1.
Relational Query Optimization (this time we really mean it)
Relational Query Optimization CS 186, Spring 2006, Lectures16&17 R & G Chapter 15 It is safer to accept any chance that offers itself, and extemporize.
Query Optimization Chapter 15. Query Evaluation Catalog Manager Query Optmizer Plan Generator Plan Cost Estimator Query Plan Evaluator Query Parser Query.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
Overview of Query Evaluation R&G Chapter 12 Lecture 13.
Query Optimization: Transformations May 29 th, 2002.
Query Optimization II R&G, Chapters 12, 13, 14 Lecture 9.
CS186 Final Review Query Optimization.
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.
Overview of Query Optimization v Plan : Tree of R.A. ops, with choice of alg for each op. –Each operator typically implemented using a `pull’ interface:
Query Optimization R&G, Chapter 15 Lecture 16. Administrivia Homework 3 available today –Written exercise; will be posted on class website –Due date:
1 Implementation of Relational Operations: Joins.
1.1 CAS CS 460/660 Introduction to Database Systems Query Optimization.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Overview of Implementing Relational Operators and Query Evaluation
Introduction to Database Systems1 Relational Query Optimization Query Processing: Topic 2.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Relational Query Optimization Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
1 Overview of Query Evaluation Chapter Overview of Query Evaluation  Plan : Tree of R.A. ops, with choice of alg for each op.  Each operator typically.
Relational Query Optimization R & G Chapter 12/15.
Database systems/COMP4910/Melikyan1 Relational Query Optimization How are SQL queries are translated into relational algebra? How does the optimizer estimates.
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
Database Systems/comp4910/spring20031 Evaluation of Relational Operations Why does a DBMS implements several algorithms for each algebra operation? What.
1 Database Systems ( 資料庫系統 ) December 3, 2008 Lecture #10.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Implementing Relational Operators and Query Evaluation Chapter 12.
Introduction to Query Optimization, R. Ramakrishnan and J. Gehrke 1 Introduction to Query Optimization Chapter 13.
Relational Query Optimization R & G Chapter 12/15.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
1.1 CAS CS 460/660 Introduction to Database Systems Query Optimization.
1 Database Systems ( 資料庫系統 ) December 13, 2004 Chapter 15 By Hao-hua Chu ( 朱浩華 )
Implementation of Database Systems, Jarek Gryz1 Relational Query Optimization Chapters 12.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction To Query Optimization and Examples Chpt
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
Query Optimization. overview Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g., SAP admin) DBA,
Relational Query Optimization
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Introduction to Query Optimization
Overview of Query Optimization
Examples of Physical Query Plan Alternatives
Introduction to Database Systems
Implementing Relational Operators Query Evaluation
Relational Query Optimization
Relational Query Optimization
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
Query Optimization.
Relational Query Optimization
Overview of Query Evaluation
Overview of Query Evaluation
Implementation of Relational Operations
Relational Query Optimization
Relational Query Optimization
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Database Systems (資料庫系統)
Relational Query Optimization (this time we really mean it)
Overview of Query Evaluation
CS222: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Relational Query Optimization
Relational Query Optimization
Relational Algebra Chpt 4a Xintao Wu Raghu Ramakrishnan
Presentation transcript:

Introduction to Database Systems CAS CS 460/660 Introduction to Database Systems Query Optimization

Review Implementation of Relational Operations as Iterators Focus largely on External algorithms (sorting/hashing) Choices depend on indexes, memory, stats,… Joins Blocked nested loops: simple, exploits extra memory Indexed nested loops: best if 1 rel small and one indexed Sort/Merge Join good with small amount of memory, bad with duplicates Hash Join fast (enough memory), bad with skewed data Relatively easy to parallelize Sort and Hash-Based Aggs and DupElim

Query Optimization Overview Query can be converted to relational algebra Rel. Algebra converted to tree, joins as branches Each operator has implementation choices Operators can also be applied in different order! SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5 sname bid=100 rating > 5 sid=sid sname((bid=100  rating > 5) (Reserves  Sailors)) Reserves Sailors 4

Iterator Interface (pull from the top) Recall: Relational operators at nodes support uniform iterator interface: Open( ), get_next( ), close( ) Unary Ops – On Open() call Open() on child. Binary Ops – call Open() on left child then on right. By convention, outer is on left. sname bid=100 rating > 5 sid=sid Reserves Sailors Alternative is pipelining (i.e. a “push”-based approach). Can combine push & pull using special operators.

Query Optimization Overview (cont) Logical Plan: Tree of R.A. ops Physical Plan: Tree of R.A. ops, with choice of algorithm for each operator. Two main issues: For a given query, what plans are considered? Algorithm to search plan space for cheapest (estimated) plan. How is the cost of a plan estimated? Ideally: Want to find best plan. Reality: Avoid worst plans! 2

Cost-based Query Sub-System Select * From Blah B Where B.blah = blah Queries Usually there is a heuristics-based rewriting step before the cost-based steps. Query Parser Query Optimizer Plan Generator Plan Cost Estimator Catalog Manager Schema Statistics Query Plan Evaluator

Schema for Examples As seen in previous lectures… Reserves: Sailors: Sailors (sid: integer, sname: string, rating: integer, age: real) Reserves (sid: integer, bid: integer, day: dates, rname: string) As seen in previous lectures… Reserves: Each tuple is 40 bytes long, 100 tuples per page, 1000 pages. Let’s say there are 100 boats. Sailors: Each tuple is 50 bytes long, 80 tuples per page, 500 pages. Let’s say there are 10 different ratings. Assume we have 5 pages in our buffer pool. 3

Motivating Example SELECT S.sname FROM Reserves R, Sailors S WHERE R.sid=S.sid AND R.bid=100 AND S.rating>5 Cost: 500+500*1000 I/Os By no means the worst plan! Misses several opportunities: selections could have been `pushed’ earlier, no use is made of any available indexes, etc. Goal of optimization: To find more efficient plans that compute the same answer. Sailors Reserves sid=sid bid=100 rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) Plan: 4

Alternative Plans – Push Selects (No Indexes) Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 Sailors Reserves sid=sid bid=100 rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) 500,500 IOs 250,500 IOs 5

Alternative Plans – Push Selects (No Indexes) Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 Sailors Reserves sid=sid bid = 100 sname (Page-Oriented Nested loops) (On-the-fly) rating > 5 250,500 IOs 250,500 IOs 5

Alternative Plans – Push Selects (No Indexes) Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 6000 IOs 250,500 IOs 5

Alternative Plans – Push Selects (No Indexes) Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 (Scan & Write to temp T2) 4250 IOs 1000 + 500+ 250 + (10 * 250) 6000 IOs 5

Alternative Plans – Push Selects (No Indexes) Sailors Reserves sid=sid rating > 5 sname (Page-Oriented Nested loops) (On-the-fly) bid=100 (Scan & Write to temp T2) Reserves Sailors sid=sid bid=100 sname (Page-Oriented Nested loops) (On-the-fly) rating>5 (Scan & Write to temp T2) 4250 IOs 4010 IOs 500 + 1000 +10 +(250 *10) 5

Alternative Plans 1 (No Indexes) Reserves Sailors sid=sid bid=100 sname (On-the-fly) rating > 5 (Scan; write to temp T1) temp T2) (Sort-Merge Join) Main difference: Sort Merge Join With 5 buffers, cost of plan: Scan Reserves (1000) + write temp T1 (10 pages, if we have 100 boats, uniform distribution). Scan Sailors (500) + write temp T2 (250 pages, if have 10 ratings). Sort T1 (2*2*10), sort T2 (2*4*250), merge (10+250) Total: 4060 page I/Os. (note: T2 sort takes 4 passes with B=5) If use BNL join, join = 10+4*250, total cost = 2770. Can also `push’ projections, but must be careful! T1 has only sid, T2 only sid, sname: T1 fits in 3 pgs, cost of BNL under 250 pgs, total < 2000. 5

Alt Plan 2: Indexes (On-the-fly) sname (On-the-fly) rating > 5 With clustered hash index on bid of Reserves, we get 100,000/100 = 1000 tuples on 1000/100 = 10 pages. INL with outer not materialized. (Index Nested Loops, sid=sid with pipelining ) (Use hash Index, do not write to temp) bid=100 Sailors Projecting out unnecessary fields from outer doesn’t help. Reserves Join column sid is a key for Sailors. At most one matching tuple, unclustered index on sid OK. Decision not to push rating>5 before the join is based on availability of sid index on Sailors. Cost: Selection of Reserves tuples (10 I/Os); then, for each, must get matching Sailors tuple (1000*1.2); total 1210 I/Os. 6

What is needed for optimization? Iterator Interface Cost Estimation Statistics and Catalogs Size Estimation and Reduction Factors

Query Blocks: Units of Optimization SELECT S.sname FROM Sailors S WHERE S.age IN (SELECT MAX (S2.age) FROM Sailors S2 GROUP BY S2.rating) Outer block Nested block An SQL query is parsed into a collection of query blocks, and these are optimized one block at a time. Inner blocks are usually treated as subroutines Computed: once per query (for uncorrelated sub-queries) or once per outer tuple (for correlated sub-queries) 7

Translating SQL to Relational Algebra SELECT S.sid, MIN (R.day) FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = “red” AND S.rating = ( SELECT MAX (S2.rating) FROM Sailors S2) GROUP BY S.sid HAVING COUNT (*) >= 2 For each sailor with the highest rating (over all sailors), and at least two reservations for red boats, find the sailor id and the earliest date on which the sailor has a reservation for a red boat. 7

Translating SQL to Relational Algebra SELECT S.sid, MIN (R.day) FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = “red” AND S.rating = ( SELECT MAX (S2.rating) FROM Sailors S2) GROUP BY S.sid HAVING COUNT (*) >= 2 Inner Block val p S.sid, MIN(R.day) (HAVING COUNT(*)>2 ( GROUP BY S.Sid ( B.color = “red” ÙS.rating = (Sailors Reserves Boats)))) s 7

Relational Algebra Equivalences Allow us to choose different operator orders and to `push’ selections and projections ahead of joins. Selections: (Cascade) ( ) s c cn R 1 Ù º . (Commute) Projections: (Cascade) (if an includes an-1 includes… a1) Joins: R (S T) (R S) T (Associative) (R S) (S R) (Commute) These two mean we can do joins in any order. 10