Chapter 18 Query Processing and Optimization
Chapter Outline u Introduction. u Using Heuristics in Query Optimization –Query Trees and Query Graphs –Transformation Rules for RA expressions –Heuristic Optimization
Introduction u With declarative languages such as SQL, user specifies what data is required rather than how it is to be retrieved. u Relieves user of knowing what constitutes good execution strategy. u Also gives DBMS more control over system performance. u Two main techniques for query optimization: – heuristic rules that order operations in a query. – comparing different strategies based on relative costs, and selecting one that minimizes resource usage.
Introduction Query Processing (QP) : Activities involved in retrieving data from the database. u Aims of QP: – transform query written in high-level language (e.g. SQL), into correct and efficient execution strategy expressed in low-level language (implementing RA); – execute the strategy to retrieve required data. Query Optimization (QO): Activity of choosing an efficient execution strategy for processing query. u As there are many equivalent transformations of same high-level query, aim of QO is to choose one that minimizes resource usage. u Generally, reduce total execution time of query. u May also reduce response time of query. u Problem computationally intractable with large number of relations, so strategy adopted is reduced to finding near optimum solution.
Introduction
Query Trees and Query Graphs
Transformation Rules for RA Operations u Cascade of Selection: Conjunctive selection operations can cascade into individual selection operations p q r (R) = p ( q ( r (R))) u Commutativity of selection. p ( q (R)) = q ( p (R)) u Cascade of Projection: In a sequence of projection operations, only the last in the sequence is required. L M … N (R) = L (R) u Commutativity of and : If the selection condition c involves only those attributes A 1, … A n in the projection list, then the two operations can commute: Ai, …, Am ( c (R)) = c ( Ai, …, Am (R))
Transformation Rules for RA Operations u Commutativity of (and ). R p S = S p R R S = S R u Associativity of (and ). and natural join ( * )are always associative: (R * S) T = R * (S * T) (R S) T = R (S T) If join condition q involves attributes only from S and T, then is associative as follows: (R p S) q r T = R p r (S q T)
Transformation Rules for RA Operations u Commutativity of and (or ). If all the attributes in the selection condition c involves only the attributes of one of the relations being joined – Say R – then the two operations can be commuted as follows: c (R r S) = ( c (R)) r S c (R S) = ( c (R)) S Alternatively if the condition c can be written as (c 1 and c 2 ), where condition c 1 involves only the attributes of the relations R and condition c 2 involves only the attributes of the relations S then the two operations can be commuted as follows: c1 (R r S) = ( c1 (R)) r ( c2 ( S)) c1 (R S) = ( c1 (R)) ( c2 ( S))
Transformation Rules for RA Operations u Commutativity of and (or ). If projection list is of form L = L 1 L 2, where L 1 only involves attributes of R, and L 2 only involves attributes of S, provided join condition only contains attributes of L, projection and theta-join operations commute as: L1 L2 (R r S) = ( L1 (R)) r ( L2 (S)) If join condition contains additional attributes not in L, say attributes M = M 1 M 2 where M 1 only involves attributes of R, and M 2 only involves attributes of S, a final projection operation is required: L1 L2 ( R r S ) = L1 L2 ( ( L1 M1 ( R )) r ( L2 M2 (S))) u Commutativity of and (but not - ). R S = S R R S = S R u Associativity of and (but not - ). (R S) T = S (R T) (R S) T = S (R T)
Transformation Rules for RA Operations Commutativity of and set operations ( , , and - ). p (R S) = p (S) p (R) p (R S) = p (S) p (R) p (R - S) = p (S) - p (R) u Commutativity of and . L (R S) = L (S) L (R)
Heuristical Processing Strategies u Perform selection operations as early as possible. – Keep predicates on same relation together. u Combine Cartesian product with subsequent selection whose predicate represents join condition into a join operation. u Use associativity of binary operations to rearrange leaf nodes so leaf nodes with most restrictive selection operations executed first. u Perform projection as early as possible. – Keep projection attributes on same relation together. u Compute common expressions once. – If common expression appears more than once, and result not too large, store result and reuse it when required. – Useful when querying views, as same expression is used to construct view each time.
Use of Transformation Rules Q: Find the last name of employees born after 1975 who work on a project named ‘Aquarius’. SELECTLNAME, FROMEMPLOYEE, WORKS_ON, PROJECT, WHEREPNAME=‘Aquarius’ AND ESSN= SSN AND PNUMBER=PNO AND BDATE > ‘ ’
Use of Transformation Rules
Example 18.3 Use of Transformation Rules