Query Optimization. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.

Slides:



Advertisements
Similar presentations
Chapter 14 Query Optimization
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Query processing and optimization. Advanced DatabasesQuery processing and optimization2 Definitions Query processing –translation of query into low-level.
Database System Concepts 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Query Optimization.
Ch.14: Query Optimization  Introduction  Catalog Information for Cost Estimation  Estimation of Statistics  Transformation of Relational Expressions.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Query Processing (overview)
Query Optimization Chapter 15. Query Evaluation Catalog Manager Query Optmizer Plan Generator Plan Cost Estimator Query Plan Evaluator Query Parser Query.
Database System Concepts 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Query Optimization.
Ch.14: Query Optimization  Introduction  Catalog Information for Cost Estimation  Estimation of Statistics  Transformation of Relational Expressions.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapter 15.
Query Processing Overview Catalog Information for Cost Estimation
©Silberschatz, Korth and Sudarshan14.1Database System Concepts 3 rd Edition Chapter 14: Query Optimization Overview Catalog Information for Cost Estimation.
1 Query Processing Query Processing Overview Measures of Query Cost Selection Operation Sorting Join Operation Other Operations Evaluation of Expressions.
Quick Review of Apr 24 material Sorting (Sections 13.4) Sort-merge Algorithm for external sorting Join Operation implementations (sect. 13.5) –Size estimation.
José Alferes Versão modificada de Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan Chapter 14: Query Optimization.
Query Processing Presented by Aung S. Win.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Chapter 13: Query Optimization
Database System Concepts 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Dr. Alexandra I. Cristea.
1 Chapter 13 CS 157 B Presentation -- Query Processing (origional from Silberschatz, Korth and Sudarshan) Presented By Laptak Lee.
©Silberschatz, Korth and Sudarshan1.1Database System Concepts - 6 th Edition Chapter 13: Query Optimization.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 12 Query Processing and Optimization.
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.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts 3 rd Edition Chapter 12: Query Processing  Overview  Catalog Information for Cost Estimation.
Chapter 14 Query Optimization. Chapter 14: Query Optimization Introduction Catalog Information for Cost Estimation Estimation of Statistics Transformation.
Database Management 9. course. Execution of queries.
©Silberschatz, Korth and Sudarshan1 Query Optimization Introduction Statistical (Catalog) Information for Cost Estimation Estimation of Statistics Cost-based.
Database System Concepts 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 14: Query Optimization.
Database System Concepts 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Query Optimization.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
CMSC424: Database Design Instructor: Amol Deshpande
Query Optimization Arash Izadpanah. Introduction: What is Query Optimization? Query optimization is the process of selecting the most efficient query-evaluation.
+ Under the hood: Query Optimization, Query Execution plans.
Lecture 4 - Query Optimization Advanced Databases Masood Niazi Torshiz Islamic Azad university- Mashhad Branch
©Silberschatz, Korth and Sudarshan14.1Database System Concepts 3 rd Edition Chapter 14: Query Optimization Overview Catalog Information for Cost Estimation.
12.1Database System Concepts - 6 th Edition Chapter 12: Query Processing Overview Measures of Query Cost Selection Operation Join Operation Sorting 、 Other.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan Chapter 13: Query Processing.
Computing & Information Sciences Kansas State University Tuesday, 03 Apr 2007CIS 560: Database System Concepts Lecture 29 of 42 Tuesday, 03 April 2007.
Database Techniek – Query Optimization Database Techniek Query Optimization (chapter 14)
Chapter 14: Query Optimization Chapter 14: Query Optimization Introduction Transformation of Relational Expressions Catalog Information for Cost.
Chapter 14 Query Optimization. ©Silberschatz, Korth and Sudarshan14.2Database System Concepts 3 rd Edition Chapter 14: Query Optimization Introduction.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 13: Query Optimization.
Lecture 3 - Query Processing (continued) Advanced Databases Masood Niazi Torshiz Islamic Azad university- Mashhad Branch
13.1 Chapter 13: Query Processing n Overview n Measures of Query Cost n Selection Operation n Sorting n Join Operation n Other Operations n Evaluation.
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
Chapter 13 Query Optimization Yonsei University 1 st Semester, 2015 Sanghyun Park.
Chapter 13: Query Processing
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
Query Processing  Basic Steps in Query Processing – an overview  Measures of Query Cost  Query Processing- Several algorithms  Selection Operation.
Query Processing and Query Optimization Database System Implementation CSE 507 Slides adapted from Silberschatz, Korth and Sudarshan Database System Concepts.
J. GamperDMS 2006/07 1 Introduction Statistical information for cost estimation Transformation of relational expressions (equivalence rules) Rule-based.
Chapter 14: Query Optimization
Database System Implementation CSE 507
Database Management System
Query processing and optimization
Overview of Query Optimization
File Processing : Query Processing
Query Processing and Optimization
Chapter 14: Query Optimization
Chapter 14: Query Optimization
Yan Huang - CSCI5330 Database Implementation – Query Processing
Chapter 14: Query Optimization
Presentation transcript:

Query Optimization

General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design Storage Txns, Concurrency Control Indexing Query Processing and Optimization

Review-Plan Generation Metadata: DBMS maintains statistics about each relation, attribute and index. Plan generation: 1. Generate many alternative plans We saw many for selections, joins 2. Estimate cost for each and choose best Plans examined: Selection: Linear, binary, PI, SI Range: PI, SI Joins: NLJ, BNLJ, INLJ, SMJ, HJ

Review-Plan Generation Depends upon a cost model For any query, must know  its estimated cardinality  its estimated cost (in # of I/Os) E.g.:  A = K (R )  cardinality SC(A, R)  cost: depends on the plan, attribute: Linear Scan b R /2 b R Binary Search  log 2 (b R )   log 2 (b R )  +  sc(A, R)/f R  -1 PI ScanHT i +1 HT i +  sc(A, R) / f R 

Other Operations Duplicate elimination can be implemented via hashing or sorting.  Optimization: duplicates can be deleted during run generation as well as at intermediate merge steps in external sort-merge.  Hashing is similar – duplicates will come into the same bucket. Projection is implemented by performing projection on each tuple followed by duplicate elimination.

Transformation of Relational Expressions Two RA expressions are equivalent if they produce the same results on the same inputs In SQL, inputs and outputs are multisets of tuples  Two expressions in the multiset version of the relational algebra are said to be equivalent if on every legal database instance the two expressions generate the same multiset of tuples An equivalence rule says that expressions of two forms are equivalent  Can replace expression of first form by second, or vice versa

Equivalence Rules 1.Conjunctive selection operations can be deconstructed into a sequence of individual selections. 2.Selection operations are commutative. 3.Only the last in a sequence of projection operations is needed, the others can be omitted. 4. Selections can be combined with Cartesian products and theta joins. a.   (R 1 X R 2 ) = R 1  R 2 b.   1 (R 1  2 R 2 ) = R 1  1   2 R 2

Equivalence Rules (Cont.) 5.Theta-join operations (and natural joins) are commutative. R 1  R 2 = R 2  R 1 6.(a) Natural join operations are associative: (R 1 R 2 ) R 3 = R 1 (R 2 R 3 ) (b) Theta joins are associative in the following manner: (R 1  1 R 2 )  2   3 R 3 = R 1  2   3 (R 2  2 R 3 ) where  2 involves attributes from only R 2 and R 3.

 R1 R2  R1 R2 R3 R1 R2 R3

Equivalence Rules (Cont.) 7. The selection operation distributes over the theta join operation under the following two conditions: (a) When all the attributes in  0 involve only the attributes of one of the expressions (R 1 ) being joined.   0  R 1  R 2 ) = (   0 (R 1 ))  R 2 (b) When  1 involves only the attributes of R 1 and  2 involves only the attributes of R 2.   1    R 1  R 2 ) = (   1 (R 1 ))  (   (R 2 ))

Equivalence Rules (Cont.) 8.The projections operation distributes over the theta join operation as follows: (a) if  involves only attributes from L 1  L 2 : (b) Consider a join E 1  E 2.  Let L 1 and L 2 be sets of attributes from E 1 and E 2, respectively.  Let L 3 be attributes of E 1 that are involved in join condition , but are not in L 1  L 2, and  let L 4 be attributes of E 2 that are involved in join condition , but are not in L 1  L 2.

Transformation Example Query: Find the names of all customers who have an account at some branch located in Brooklyn.  customer-name (  branch-city = “Brooklyn” (branch (account depositor))) Transformation using rule 7a.  customer-name ((  branch-city =“Brooklyn” (branch)) (account depositor)) Performing the selection as early as possible reduces the size of the relation to be joined.

Example with Multiple Transformations Query: Find the names of all customers with an account at a Brooklyn branch whose account balance is over $1000.  customer-name( (  branch-city = “Brooklyn”  balance > 1000 (branch (account depositor))) Transformation using join associatively (Rule 6a):  customer-name ((  branch-city = “Brooklyn”  balance > 1000 (branch account) depositor) Second form provides an opportunity to apply the “perform selections early” rule, resulting in the subexpression  branch-city = “Brooklyn” (branch)  balance > 1000 (account)

Multiple Transformations (Cont.)

Projection Operation Example When we compute (  branch-city = “Brooklyn” (branch) account ) we obtain a relation whose schema is: (branch-name, branch-city, assets, account-number, balance) Push projections using equivalence rules 8a and 8b; eliminate unneeded attributes from intermediate results to get:  customer-name ((  account-number ( (  branch-city = “Brooklyn” (branch) account )) depositor)  customer-name ((  branch-city = “Brooklyn” (branch) account) depositor)

Join Ordering Example For all relations r 1, r 2, and r 3, (r 1 r 2 ) r 3 = r 1 (r 2 r 3 ) If r 2 r 3 is quite large and r 1 r 2 is small, we choose (r 1 r 2 ) r 3 so that we compute and store a smaller temporary relation.

Join Ordering Example (Cont.) Consider the expression  customer-name ((  branch-city = “Brooklyn” (branch)) account depositor) Could compute account depositor first, and join result with  branch-city = “Brooklyn” (branch) but account depositor is likely to be a large relation. Since it is more likely that only a small fraction of the bank’s customers have accounts in branches located in Brooklyn, it is better to compute  branch-city = “Brooklyn” (branch) account first.

Enumeration of Equivalent Expressions Query optimizers use equivalence rules to generate equivalent expressions 1st Approach: Generate all equivalent expressions  But... Very expensive 2nd Approach: Exploit common subexpressions:  when E1 is generated from E2 by an equivalence rule, usually only the top level of the two are different, subtrees below are the same and can be shared  E.g. when applying join associativity Time requirements are reduced by not generating all expressions

Evaluation Plan Example

Cost-Based Optimization Consider finding the best join-order for r 1 r 2... r n. There are (2(n – 1))!/(n – 1)! different join orders for above expression. With n = 7, the number is , with n = 10, the number is greater than 176 billion! No need to generate all the join orders. Using dynamic programming, the least-cost join order for any subset of {r 1, r 2,... r n } is computed only once and stored for future use.

Dynamic Programming in Optimization To find best join tree for a set of n relations:  To find best plan for a set S of n relations, consider all possible plans of the form: S 1 (S – S 1 ) where S 1 is any non-empty subset of S.  Recursively compute costs for joining subsets of S to find the cost of each plan. Choose the cheapest of the 2 n – 1 alternatives. With dynamic programming time complexity of optimization with bushy trees is O(3 n ). Space complexity is O(2 n ) If only left-deep trees are considered, time complexity of finding best join order is O(n 2 n )  Space complexity remains at O(2 n ) Cost-based optimization is expensive, but worthwhile for queries on large datasets (typical queries have small n, generally < 10)

Heuristic Optimization Cost-based optimization is expensive, even with dynamic programming. Systems may use heuristics to reduce the number of choices that must be made in a cost-based fashion. Heuristic optimization transforms the query-tree by using a set of rules that typically (but not in all cases) improve execution performance:  Perform selection early (reduces the number of tuples)  Perform projection early (reduces the number of attributes)  Perform most restrictive selection and join operations before other similar operations.  Some systems use only heuristics, others combine heuristics with partial cost-based optimization.

Steps in Typical Heuristic Optimization 1.Deconstruct conjunctive selections into a sequence of single selection operations (Equiv. rule 1.). 2.Move selection operations down the query tree for the earliest possible execution (Equiv. rules 2, 7a, 7b, 11). 3.Execute first those selection and join operations that will produce the smallest relations (Equiv. rule 6). 4.Replace Cartesian product operations that are followed by a selection condition by join operations (Equiv. rule 4a). 5.Deconstruct and move as far down the tree as possible lists of projection attributes, creating new projections where needed (Equiv. rules 3, 8a, 8b, 12). 6.Identify those subtrees whose operations can be pipelined, and execute them using pipelining).