150 likes | 332 Views
Chapter 18 Query Processing and Optimization. Chapter Outline. Introduction . Using Heuristics in Query Optimization Query Trees and Query Graphs Transformation Rules for RA expressions Heuristic Optimization. Introduction.
E N D
Chapter 18 Query Processing and Optimization
Chapter Outline • Introduction . • Using Heuristics in Query Optimization • Query Trees and Query Graphs • Transformation Rules for RA expressions • Heuristic Optimization
Introduction • With declarative languages such as SQL, user specifies what data is required rather than how it is to be retrieved. • Relieves user of knowing what constitutes good execution strategy. • Also gives DBMS more control over system performance. • 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. • 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. • As there are many equivalent transformations of same high-level query, aim of QO is to choose one that minimizes resource usage. • Generally, reduce total execution time of query. • May also reduce response time of query. • Problem computationally intractable with large number of relations, so strategy adopted is reduced to finding near optimum solution.
Transformation Rules forRA Operations • Cascade of Selection: Conjunctive selection operations can cascade into individual selection operations spq r(R) = sp (sq ( sr (R))) • Commutativity of selection. sp (sq (R)) = sq ( sp(R)) • Cascade of Projection:In a sequence of projection operations, only the last in the sequence is required. L M … N (R) = L (R) • Commutativity of s and : Ifthe selection condition c involves only those attributes A1, … An in the projection list, then the two operations can commute: Ai, …, Am (sc (R)) = sc (Ai, …, Am(R))
Transformation Rules forRA Operations • Commutativity of (and ). R p S = S p R R S = S R • 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 forRA Operations • Commutativity of s 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: sc (R r S) = (sc (R)) r S sc (R S) = (sc (R)) S Alternatively if the condition c can be written as (c1 and c2), where condition c1 involves only the attributes of the relations R and condition c2 involves only the attributes of the relations S then the two operations can be commuted as follows: sc1 (R r S) = (sc1 (R)) r (sc2 ( S)) sc1 (R S) = (sc1 (R)) (sc2 ( S))
Transformation Rules for RA Operations • Commutativity of and (or ). If projection list is of form L = L1 L2, where L1 only involves attributes of R, and L2 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 = M1 M2 where M1 only involves attributes of R, and M2 only involves attributes of S, a final projection operation is required: L1 L2(RrS) =L1 L2( (L1 M1(R)) r(L2 M2(S))) • Commutativity of and (but not - ). R S = S R R S = S R • Associativity of and (but not - ). (R S) T = S (R T) (R S) T = S (R T)
Transformation Rules for RA Operations Commutativity of s and set operations (, , and -). sp(R S) = sp(S) sp(R) sp(R S) = sp(S) sp(R) sp(R - S) = sp(S) -sp(R) • Commutativity of and . L (R S) = L(S) L(R)
Heuristical Processing Strategies • Perform selection operations as early as possible. • Keep predicates on same relation together. • Combine Cartesian product with subsequent selection whose predicate represents join condition into a join operation. • Use associativity of binary operations to rearrange leaf nodes so leaf nodes with most restrictive selection operations executed first. • Perform projection as early as possible. • Keep projection attributes on same relation together. • 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’. SELECT LNAME, FROM EMPLOYEE, WORKS_ON, PROJECT, WHERE PNAME=‘Aquarius’ AND ESSN= SSN AND PNUMBER=PNO AND BDATE > ‘1957-12-31’