Báo cáo môn cơ sở dữ liệu Query optimization Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization Costbased query optimization Summary
Query optimization Lecturer: Assoc. Prof. Dr. DANG Tran Khanh Report: 13070243 Trần Duy Linh 13070263 Nguyễn Minh Thành Outline Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization Cost-based query optimization Summary 2 Outline Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization Cost-based query optimization Summary 3 Introduction to Query Processing Query processing: The process by which the query results are retrieved from a high-level query such as SQL or OQL, ODBMS Query optimization: The process of choosing a suitable execution strategy for processing a query. Two internal representations of a query: Query Tree Query Graph 4 Processing a high-level query Scanning, parsing, and validating Query optimizer Query code generator Runtime database processor Query in a high-level language Code to execute the query Execution plan Immediate form of query Result of query 5 Example Example of query optimazation 6 Outline Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization Cost-based query optimization Summary 7 Translating SQL Queries into Relational Algebra 8 SELECT * FROM R WHERE c σC(R) SELECT A1, A2, … FROM R Π A1, A2, … (R) SELECT * FROM R, S WHERE c R c S Translating SQL Queries into Relational Algebra Query block: the basic unit that can be translated into the algebraic operators and optimized. A query block contains a single SELECT-FROM- WHERE expression, as well as GROUP BY and HAVING clause if these are part of the block. Nested queries within a query are identified as separate query blocks. Aggregate operators (MAX, MIN, SUM, and COUNT) in SQL must be included in the extended algebra. 9 Translating SQL Queries into Relational Algebra 10 SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > (SELECT MAX (SALARY) FROM EMPLOYEE WHERE DNO = 5); SELECT MAX (SALARY) FROM EMPLOYEE WHERE DNO = 5 SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > C πLNAME, FNAME (σSALARY>C(EMPLOYEE)) ℱMAX SALARY (σDNO=5 (EMPLOYEE)) [...]... Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization Cost-based query optimization Summary 28 • Using Heuristics in Query Optimization Query Optimizer : choose good execution plan Two main approach : Heuristic-based query optimization Reduce the number of operations … Cost-based query optimization. .. Introduction to Query Processing Translating SQL Queries into Relational Algebra Query Trees and Query Graphs Rules for equivalent RAEs Using Heuristics in Query Optimization Cost-based query optimization Summary 11 Query Trees and Query Graphs Query tree: A tree data structure that corresponds to a relational algebra expression It represents the input relations of the query as leaf... SQL query: Q2: SELECT P.NUMBER,P.DNUM,E.LNAME, E.ADDRESS, E.BDATE FROM PROJECT AS P,DEPARTMENT AS D, WHERE P.DNUM=D.DNUMBER AND P.PLOCATION=‘STAFFORD’; EMPLOYEE AS E D.MGRSSN=E.SSN AND Query Trees and Query Graphs Query Trees and Query Graphs Outline Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization. .. relational calculus expression It does not indicate an order on which operations to perform first There is only a single graph corresponding to each query 12 Query Trees and Query Graphs Example: EMPLOYEE DEPARTMENT PROJECT Query Trees and Query Graphs Example: For every project located in ‘Stafford’, retrieve the project number, the controlling department number and the department manager’s... Heuristic-based query optimization Reduce the number of operations … Cost-based query optimization Estimate cost of each operation … Other : Semantic Query Optimization 29 • Heuristics? Each Relational Algebra Expression (E) is represented by a Query Tree (Q) Heuristic Algorithm ? Rewrite E (Q) E1 (Q1) En (Qn) + E ≡ E1 ≡ … ≡ En (≡ : equivalent ) + So En is better than E Rewrite must be... than 32 • Outline of a Heuristic Algebraic Optimization Algorithm 1 Break up any SELECT operations with conjunctive conditions into a cascade of SELECT operations (Rule 1) 2 Move each SELECT operation as far down the query tree as is permitted by the attributes involved in the select condition ( Rules 2, 4, 6, and 10 ) 33 • Outline of a Heuristic Algebraic Optimization Algorithm 3 Rearrange the leaf... Trees and Query Graphs Outline Introduction to Query Processing Translating SQL Queries into Relational Algebra Rules for equivalent RAEs Using Heuristics in Query Optimization Cost-based query optimization Summary 17 Equivalent Relational Expressions Two Relational Algebra Expressions are equivalent if they produce the same results (tuples) on the same input relations - Although their... nodes of the tree, and represents the relational algebra operations as internal nodes An execution of the query tree consists of executing an internal node operation whenever its operands are available and then replacing that internal node by the relation that results from executing the operation Query graph: A graph data structure that corresponds to a relational calculus expression It does not... operations ) – position the leaf node relations with the most restrictive SELECT operations so they are executed first in the query tree – representation the ordering of leaf nodes does make sure that not cause CARTESIAN PRODUCT operations 34 • Outline of a Heuristic Algebraic Optimization Algorithm 4 Combine a CARTESIAN PRODUCT operation with a subsequent SELECT operation in the tree into a JOIN operation,... 5 Break down and move lists of projection attributes down the tree as far as possible by creating new PROJECT operations as needed ( Using Rules 3, 4, 7, and 11 ) 35 • Outline of a Heuristic Algebraic Optimization Algorithm 6 Identify subtrees that represent groups of operations that can be executed by a single algorithm 36