Database System II Lecture :6 Query Processing and Optimization
Query processing steps
Query processing steps • Query Processing The activities involved in scanning, parsing, validating, optimizing, and executing a query. • The scanner identifies the query tokens • the parser checks the query syntax (grammar) • The query must also be validated by checking that all attribute and relation names are valid and semantically meaningful names in the schema of the particular database being queried. • Query optimization The activity of choosing an efficient execution strategy for processing a query.
Chapter 15-4 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.
TRANSLATING SQL QUERIES INTO RELATIONAL ALGEBRA (EXAMPLE)
Using Heuristics in Query Optimization  Process for heuristics optimization 1. The parser of a high-level query generates an initial internal representation; 2. Apply heuristics rules to optimize the internal representation. 3. A query execution plan is generated to execute groups of operations based on the access paths available on the files involved in the query.  The main heuristic is to apply first the operations that reduce the size of intermediate results.  E.g., Apply SELECT and PROJECT operations before applying the JOIN or other binary operations.
Using Heuristics in Query Optimization Query tree:  A tree data structure that corresponds to a relational algebra expression. It represents the input relations of the query as leaf 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 indicate an order on which operations to perform first. There is only a single graph corresponding to each query.
Using Heuristics in Query Optimization  Example:  For every project located in ‘Stafford’, retrieve the project number, the controlling department number and the department manager’s last name, address and birthdate.  SQL query: Q2: SELECT P.NUMBER,P.DNUM,E.LNAME, E.ADDRESS, E.BDATE FROM PROJECT AS P,DEPARTMENT AS D, EMPLOYEE AS E WHERE P.DNUM=D.DNUMBER AND D.MGRSSN=E.SSN AND P.PLOCATION=‘STAFFORD’;  Relation algebra: PNUMBER, DNUM, LNAME, ADDRESS, BDATE (((PLOCATION=‘STAFFORD’(PROJECT)) DNUM=DNUMBER (DEPARTMENT)) MGRSSN=SSN(EMPLOYEE))
Two query trees for the query Q2. (a) Query tree corresponding to the relational algebra expression for Q2. (b) Initial (canonical) query tree for SQL query Q2. (c) Query graph for Q2.
Using Heuristics in Query Optimization Heuristic Optimization of Query Trees:  The same query could correspond to many different relational algebra expressions — and hence many different query trees.  The task of heuristic optimization of query trees is to find a final query tree that is efficient to execute. Example: Q: SELECT LNAME FROM EMPLOYEE, WORKS_ON, PROJECT WHERE PNAME = ‘AQUARIUS’ AND PNMUBER=PNO AND ESSN=SSN AND BDATE > ‘1957-12-31’;
Steps in converting a query tree during heuristic optimization. (a) Initial (canonical) query tree for SQL query Q. (b) Moving SELECT operations down the query tree. (c) Applying the more restrictive SELECT operation first. (d) Replacing CARTESIAN PRODUCT and SELECT with JOIN operations. (e) Moving PROJECT operations down the query tree. Using Heuristics in Query Optimization
General Transformation Rules for Relational Algebra Operations: 1. Cascade of : A conjunctive selection condition can be broken up into a cascade (sequence) of individual  operations:   c1 AND c2 AND ... AND cn(R) = c1 (c2 (...(cn(R))...) ) 2. Commutativity of : The  operation is commutative:  c1 (c2(R)) = c2 (c1(R)) 3. Cascade of : In a cascade (sequence) of  operations, all but the last one can be ignored:  List1 (List2 (...(Listn(R))...) ) = List1(R) 4. Commuting  with : If the selection condition c involves only the attributes A1, ..., An in the projection list, the two operations can be commuted:  A1, A2, ..., An (c (R)) = c (A1, A2, ..., An (R))
General Transformation Rules for Relational Algebra Operations: 5. Commutativity of ( and x ): The operation is commutative as is the x operation:  R C S = S C R; R x S = S x R 6. Commuting  with (or x ): If all the attributes in the selection condition c involve only the attributes of one of the relations being joined—say, R—the two operations can be commuted as follows:  c ( R S ) = (c (R)) S Alternatively, if the selection condition c can be written as (c1 and c2), where condition c1 involves only the attributes of R and condition c2 involves only the attributes of S, the operations commute as follows:  c ( R S ) = (c1 (R)) (c2 (S))
General Transformation Rules for Relational Algebra Operations: 7. Commuting  with (or x): Suppose that the projection list is L = {A1, ..., An, B1, ..., Bm}, where A1, ..., An are attributes of R and B1, ..., Bm are attributes of S. If the join condition c involves only attributes in L, the two operations can be commuted as follows:  L ( R C S ) = (A1, ..., An (R)) C ( B1, ..., Bm (S)) If the join condition C contains additional attributes not in L, these must be added to the projection list, and a final  operation is needed.
General Transformation Rules for Relational Algebra Operations: 8. Commutativity of set operations: The set operations υ and ∩ are commutative but “–” is not. 9. Associativity of , x, υ, and ∩ : These four operations are individually associative; that is, if  stands for any one of these four operations (throughout the expression), we have • ( R  S )  T = R  ( S  T ) 10. Commuting  with set operations: The  operation commutes with υ , ∩ , and –. If  stands for any one of these three operations, we have • c ( R  S ) = (c (R))  (c (S))
• The  operation commutes with υ. • L ( R υ S ) = (L (R)) υ (L (S)) • Converting a (, x) sequence into : • If the condition c of a  that follows a x Corresponds to a join condition, convert the (, x) sequence into a as follows: • (C (R x S)) = (R C S) General Transformation Rules for Relational Algebra Operations:
Using Heuristics in Query Optimization  Outline of a Heuristic Algebraic Optimization Algorithm: 1. Using rule 1, break up any select operations with conjunctive conditions into a cascade of select operation 2. Using rules 2, 4, 6, and 10 concerning the commutativity of select with other operations, move each select operation as far down the query tree as is permitted by the attributes involved in the select condition. 3. Using rule 9 concerning associativity of binary operations, rearrange the leaf nodes of the tree so that the leaf node relations with the most restrictive select operations are executed first in the query tree representation. 4. Using Rule 12, combine a Cartesian product operation with a subsequent select operation in the tree into a join operation. 5. Using rules 3, 4, 7, and 11 concerning the cascading of project and the commuting of project with other operations, break down and move lists of projection attributes down the tree as far as possible by creating new project operations as needed. 6. Identify subtrees that represent groups of operations that can be executed by a single algorithm.
Using Heuristics in Query Optimization  Summary of Heuristics for Algebraic Optimization: 1. The main heuristic is to apply first the operations that reduce the size of intermediate results. 2. Perform select operations as early as possible to reduce the number of tuples and perform project operations as early as possible to reduce the number of attributes. (This is done by moving select and project operations as far down the tree as possible.) 3. The select and join operations that are most restrictive should be executed before other similar operations. (This is done by reordering the leaf nodes of the tree among themselves and adjusting the rest of the tree appropriately.)

query-processing-database-lectures .ppt

  • 1.
    Database System II Lecture:6 Query Processing and Optimization
  • 2.
  • 3.
    Query processing steps •Query Processing The activities involved in scanning, parsing, validating, optimizing, and executing a query. • The scanner identifies the query tokens • the parser checks the query syntax (grammar) • The query must also be validated by checking that all attribute and relation names are valid and semantically meaningful names in the schema of the particular database being queried. • Query optimization The activity of choosing an efficient execution strategy for processing a query.
  • 4.
    Chapter 15-4 TRANSLATING SQLQUERIES 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.
  • 5.
    TRANSLATING SQL QUERIESINTO RELATIONAL ALGEBRA (EXAMPLE)
  • 6.
    Using Heuristics inQuery Optimization  Process for heuristics optimization 1. The parser of a high-level query generates an initial internal representation; 2. Apply heuristics rules to optimize the internal representation. 3. A query execution plan is generated to execute groups of operations based on the access paths available on the files involved in the query.  The main heuristic is to apply first the operations that reduce the size of intermediate results.  E.g., Apply SELECT and PROJECT operations before applying the JOIN or other binary operations.
  • 7.
    Using Heuristics inQuery Optimization Query tree:  A tree data structure that corresponds to a relational algebra expression. It represents the input relations of the query as leaf 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 indicate an order on which operations to perform first. There is only a single graph corresponding to each query.
  • 8.
    Using Heuristics inQuery Optimization  Example:  For every project located in ‘Stafford’, retrieve the project number, the controlling department number and the department manager’s last name, address and birthdate.  SQL query: Q2: SELECT P.NUMBER,P.DNUM,E.LNAME, E.ADDRESS, E.BDATE FROM PROJECT AS P,DEPARTMENT AS D, EMPLOYEE AS E WHERE P.DNUM=D.DNUMBER AND D.MGRSSN=E.SSN AND P.PLOCATION=‘STAFFORD’;  Relation algebra: PNUMBER, DNUM, LNAME, ADDRESS, BDATE (((PLOCATION=‘STAFFORD’(PROJECT)) DNUM=DNUMBER (DEPARTMENT)) MGRSSN=SSN(EMPLOYEE))
  • 9.
    Two query treesfor the query Q2. (a) Query tree corresponding to the relational algebra expression for Q2. (b) Initial (canonical) query tree for SQL query Q2. (c) Query graph for Q2.
  • 10.
    Using Heuristics inQuery Optimization Heuristic Optimization of Query Trees:  The same query could correspond to many different relational algebra expressions — and hence many different query trees.  The task of heuristic optimization of query trees is to find a final query tree that is efficient to execute. Example: Q: SELECT LNAME FROM EMPLOYEE, WORKS_ON, PROJECT WHERE PNAME = ‘AQUARIUS’ AND PNMUBER=PNO AND ESSN=SSN AND BDATE > ‘1957-12-31’;
  • 11.
    Steps in convertinga query tree during heuristic optimization. (a) Initial (canonical) query tree for SQL query Q. (b) Moving SELECT operations down the query tree. (c) Applying the more restrictive SELECT operation first. (d) Replacing CARTESIAN PRODUCT and SELECT with JOIN operations. (e) Moving PROJECT operations down the query tree. Using Heuristics in Query Optimization
  • 14.
    General Transformation Rulesfor Relational Algebra Operations: 1. Cascade of : A conjunctive selection condition can be broken up into a cascade (sequence) of individual  operations:   c1 AND c2 AND ... AND cn(R) = c1 (c2 (...(cn(R))...) ) 2. Commutativity of : The  operation is commutative:  c1 (c2(R)) = c2 (c1(R)) 3. Cascade of : In a cascade (sequence) of  operations, all but the last one can be ignored:  List1 (List2 (...(Listn(R))...) ) = List1(R) 4. Commuting  with : If the selection condition c involves only the attributes A1, ..., An in the projection list, the two operations can be commuted:  A1, A2, ..., An (c (R)) = c (A1, A2, ..., An (R))
  • 15.
    General Transformation Rulesfor Relational Algebra Operations: 5. Commutativity of ( and x ): The operation is commutative as is the x operation:  R C S = S C R; R x S = S x R 6. Commuting  with (or x ): If all the attributes in the selection condition c involve only the attributes of one of the relations being joined—say, R—the two operations can be commuted as follows:  c ( R S ) = (c (R)) S Alternatively, if the selection condition c can be written as (c1 and c2), where condition c1 involves only the attributes of R and condition c2 involves only the attributes of S, the operations commute as follows:  c ( R S ) = (c1 (R)) (c2 (S))
  • 16.
    General Transformation Rulesfor Relational Algebra Operations: 7. Commuting  with (or x): Suppose that the projection list is L = {A1, ..., An, B1, ..., Bm}, where A1, ..., An are attributes of R and B1, ..., Bm are attributes of S. If the join condition c involves only attributes in L, the two operations can be commuted as follows:  L ( R C S ) = (A1, ..., An (R)) C ( B1, ..., Bm (S)) If the join condition C contains additional attributes not in L, these must be added to the projection list, and a final  operation is needed.
  • 17.
    General Transformation Rulesfor Relational Algebra Operations: 8. Commutativity of set operations: The set operations υ and ∩ are commutative but “–” is not. 9. Associativity of , x, υ, and ∩ : These four operations are individually associative; that is, if  stands for any one of these four operations (throughout the expression), we have • ( R  S )  T = R  ( S  T ) 10. Commuting  with set operations: The  operation commutes with υ , ∩ , and –. If  stands for any one of these three operations, we have • c ( R  S ) = (c (R))  (c (S))
  • 18.
    • The operation commutes with υ. • L ( R υ S ) = (L (R)) υ (L (S)) • Converting a (, x) sequence into : • If the condition c of a  that follows a x Corresponds to a join condition, convert the (, x) sequence into a as follows: • (C (R x S)) = (R C S) General Transformation Rules for Relational Algebra Operations:
  • 19.
    Using Heuristics inQuery Optimization  Outline of a Heuristic Algebraic Optimization Algorithm: 1. Using rule 1, break up any select operations with conjunctive conditions into a cascade of select operation 2. Using rules 2, 4, 6, and 10 concerning the commutativity of select with other operations, move each select operation as far down the query tree as is permitted by the attributes involved in the select condition. 3. Using rule 9 concerning associativity of binary operations, rearrange the leaf nodes of the tree so that the leaf node relations with the most restrictive select operations are executed first in the query tree representation. 4. Using Rule 12, combine a Cartesian product operation with a subsequent select operation in the tree into a join operation. 5. Using rules 3, 4, 7, and 11 concerning the cascading of project and the commuting of project with other operations, break down and move lists of projection attributes down the tree as far as possible by creating new project operations as needed. 6. Identify subtrees that represent groups of operations that can be executed by a single algorithm.
  • 20.
    Using Heuristics inQuery Optimization  Summary of Heuristics for Algebraic Optimization: 1. The main heuristic is to apply first the operations that reduce the size of intermediate results. 2. Perform select operations as early as possible to reduce the number of tuples and perform project operations as early as possible to reduce the number of attributes. (This is done by moving select and project operations as far down the tree as possible.) 3. The select and join operations that are most restrictive should be executed before other similar operations. (This is done by reordering the leaf nodes of the tree among themselves and adjusting the rest of the tree appropriately.)