Overview of Query Optimization

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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
Query Optimization Goal: Declarative SQL query
Advanced Databases: Lecture 2 Query Optimization (I) 1 Query Optimization (introduction to query processing) Advanced Databases By Dr. Akhtar Ali.
1 Overview of Query Evaluation Chapter Objectives  Preliminaries:  Core query processing techniques  Catalog  Access paths to data  Index matching.
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.
1 Relational Query Optimization Module 5, Lecture 2.
ICS 421 Spring 2010 Relational Query Optimization Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
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.
Relational Query Optimization (this time we really mean it)
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.
1 Query Optimization. 2 Why Optimize? Given a query of size n and a database of size m, how big can the output of applying the query to the database be?
Overview of Query Evaluation R&G Chapter 12 Lecture 13.
1 Optimization - Selection. 2 The Selection Operation Table: Reserves(sid, bid, day, agent) A page (block) can hold 100 Reserves tuples There are 1,000.
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:
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.
1 Revision and Exam Briefing M. Akhtar Ali School of CEIS.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Implementing Relational Operators and Query Evaluation Chapter 12.
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
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.
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.
Advanced Databases: Lecture 6 Query Optimization (I) 1 Introduction to query processing + Implementing Relational Algebra Advanced Databases By Dr. Akhtar.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
1 Database Systems ( 資料庫系統 ) December 13, 2004 Chapter 15 By Hao-hua Chu ( 朱浩華 )
Implementation of Database Systems, Jarek Gryz1 Relational Query Optimization Chapters 12.
1 Database Systems ( 資料庫系統 ) Chapter 12 Overview of Query Evaluation November 22, 2004 By Hao-hua Chu ( 朱浩華 )
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,
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Introduction to Query Optimization
Evaluation of Relational Operations: Other Operations
Introduction to Database Systems
Examples of Physical Query Plan Alternatives
Relational Operations
Implementing Relational Operators Query Evaluation
Query Optimization Overview
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
Evaluation of Relational Operations: Other Techniques
Database Applications (15-415) DBMS Internals- Part X Lecture 22, April 3, 2018 Mohammad Hammoud.
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
Evaluation of Relational Operations: Other Techniques
Relational Algebra Chpt 4a Xintao Wu Raghu Ramakrishnan
Presentation transcript:

Overview of Query Optimization Logical Optimization: Using some transformation rules and algebraic equivalences To choose different join orders R ⋈ S  S ⋈ R (Commutative) (R ⋈ S) ⋈ T  R ⋈ (S ⋈ T) (Associative) To push selections and projections bellow of joins (attr1 op value (R) ) ⋈ S  attr1 op value (R ⋈ S ) attr1, attr2, attr3 ((attr1, attr2 (R) ) ⋈ S )  attr1, attr2,attr3 (R ⋈ S ) Physical Optimization: For a given R.A expression There could be several different plans possible using different implementation of the R.A operators Calculate the cost of these different plans and choose the best Ideally, we want the best plan, but Practically, we should avoid worst plans Advanced Databases: Lecture # 8 Query Optimization (III)

Pushing Selection & Projection below Join A join is quite expensive operation. The cost can be reduced by reducing the sizes of the input relations. The sizes of the Input Relations can be reduced by applying: Selection: restricting the input relation i.e. number of tuples Projection: reducing the number of columns. Usually Selection reduces the size of the input relation more than Projection. Projection before the Join should be done quite carefully as the cost of Projection could increase the overall cost if it does not reduce the size of the input by a good factor. Advanced Databases: Lecture # 8 Query Optimization (III)

Estimating the Evaluation Cost of a Plan For each plan, we should be able to estimate the overall cost. For each node in the query tree, we estimate the cost of performing the corresponding operation; For each node in the query tree, we estimate the size of the result, which is used by the operation in the parent node; In order to correctly estimate the cost of each operation and the size of its result, the optimizer uses certain statistical information maintained by the DBMS. Advanced Databases: Lecture # 8 Query Optimization (III)

Statistics Maintained by a DBMS Cardinality The number of tuples for each relation. Size The number of pages for each relation. Index Cardinality The number of distinct key values for each index. Index Size The number of pages for each index. Index Height The number of non-leaf levels for each tree index. Index Range The minimum present key value and the maximum present key value for each index. Advanced Databases: Lecture # 8 Query Optimization (III)

A 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 (Using nested loops – page-oriented) 500,500 I/Os This is not an efficient plan. We could have pushed selections down before the join, no index is used. Goal of Optimization: To find more efficient plans that compute the same answer. Advanced Databases: Lecture # 8 Query Optimization (III)

Optimization: Alternative 1 (no index) Push Selects before join Assuming that there are 1000 tuples in Reserves with bid=100 and 20000 tuples in Sailors with rating > 5. So Cost of Selections: Scan Reserves (1000 pages) and write the selected 1000 tuples to temp relation T1 (10 pages), so in total 1010 I/Os Scan Sailors (500 pages) and write the selected 20000 tuples to temp relation T2 (250 pages), so in total 750 I/Os Total cost = 1010 + 750 = 1760 I/Os so far Using SNL the cost = 10 + 10 * 250 = 2510 I/Os Total cost = 1760 + 2510 = 4270 I/Os about 117 times less than the cost of the initial plan i.e. 500,500 I/Os Advanced Databases: Lecture # 8 Query Optimization (III)

Optimization: Alternative 2 (uses indexes) The same RA as alternative 1 Using a clustered Hash index on Reserves: A hash index will take 1 plus 10 to retrieve the 1000 qualifying tuples Cost of Selection = 1 + 10 = 11 I/Os Cost of Writing to T1 = 10 I/Os Sub-Total = 11 +10 = 21 The size of T1 = 10 pages Using a clustered B+ tree index on Sailors: Cost of Selection = 2 (the constant cost) + 250 = 252 I/Os, Cost of Writing to T2 = 250 I/Os, Sub-Total = 252 + 250 = 502, and the size of T2 = 250 pages Using SNL the cost is: Cost = 10 + 10 * 250 = 2510 I/Os Total cost = 21 + 502 + 2510 = 3033 I/Os, which is 165 times less than the cost of the initial plan i.e. 500,500 I/Os Advanced Databases: Lecture # 8 Query Optimization (III)

Optimization: Alternative 3(using pipelining i. e Optimization: Alternative 3(using pipelining i.e. intermediate results are not written to disk) The same RA as alternative 1 Using a clustered Hash index on Reserves: A hash index will take 1 plus 10 to retrieve the 1000 qualifying tuples Cost of Selection = 1 + 10 = 11 I/Os The size of T1 = 10 pages Using a clustered B+ tree index on Sailors: Cost of Selection = 2 (the constant cost) + 250 = 252 I/Os The size of T2 = 250 pages Using Block-Nested Loops join with 7 buffer pages: Cost = Total cost = 11 + 252 + 510 = 773 I/Os, which is 647 times faster than the initial plan and about 4 times faster than the previous one. Advanced Databases: Lecture # 8 Query Optimization (III)