Chapter 18 Query Processing. 2 Chapter - Objectives u Objectives of query processing and optimization. u Static versus dynamic query optimization. u How.

Slides:



Advertisements
Similar presentations
Query Processing Chapter 21 in Textbook.
Advertisements

Query Processing Reading: CB, Chaps 5 & 23. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the basic concepts of Query.
Advanced Database Systems
Distributed DBMS © M. T. Özsu & P. Valduriez Ch.7/1 Outline Introduction Background Distributed Database Design Database Integration Semantic Data Control.
Manajemen Basis Data Pertemuan Matakuliah: M0264/Manajemen Basis Data Tahun: 2008.
1 CSE 480: Database Systems Lecture 22: Query Optimization Reference: Read Chapter 15.6 – 15.8 of the textbook.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
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.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
QUERY OPTIMIZATION AND QUERY PROCESSING.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization See Sections 15.1, 2, 3, 7.
ICS (072)Query Processing and Optimization 1 Chapter 15 Algorithms for Query Processing and Optimization ICS 424 Advanced Database Systems Dr.
Query Optimization. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
16.5 Introduction to Cost- based plan selection Amith KC Student Id: 109.
Chapter 19 Query Processing and Optimization
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
©Silberschatz, Korth and Sudarshan14.1Database System Concepts 3 rd Edition Chapter 14: Query Optimization Overview Catalog Information for Cost Estimation.
1 Chapter 21 Query Processing Transparencies ownerNoclient © Pearson Education Limited 1995, 2005.
Chapter 17 Methodology – Physical Database Design for Relational Databases Transparencies © Pearson Education Limited 1995, 2005.
Team Dosen UMN Physical DB Design Connolly Book Chapter 18.
Query Processing Presented by Aung S. Win.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Context Tailoring the DBMS –To support particular applications Beyond alphanumerical data Beyond retrieve + process –To support particular hardware New.
Lecture 9 Methodology – Physical Database Design for Relational Databases.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Database Management 9. course. Execution of queries.
Chapter 16 Methodology – Physical Database Design for Relational Databases.
Overview of Query Processing
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Department of Computer Science and Engineering, HKUST Slide Query Processing and Optimization Query Processing and Optimization.
Querying Structured Text in an XML Database By Xuemei Luo.
Query Optimization Chap. 19. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying where.
Academic Year 2014 Spring. MODULE CC3005NI: Advanced Database Systems “QUERY OPTIMIZATION” Academic Year 2014 Spring.
1 Chapter 20 Query Processing Transparencies. 2 Chapter 20 - Objectives u Objectives of query processing and optimization. u Static versus dynamic query.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
Lecture 11: Query processing and optimization Jose M. Peña
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
Query Processing Bayu Adhi Tama, MTI. 1 ownerNoclient © Pearson Education Limited 1995, 2005.
Methodology – Physical Database Design for Relational Databases.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
CSCI Query Processing1 QUERY PROCESSING & OPTIMIZATION Dr. Awad Khalil Computer Science Department AUC.
Advance Database Systems Query Optimization Ch 15 Department of Computer Science The University of Lahore.
Relational Algebra p BIT DBMS II.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Introduction to Query Processing (1) Query optimization: The process of choosing a suitable execution.
Distributed Database Design Bayu Adhi Tama, MTI Fasilkom-Unsri Adapted from Connolly, et al., Database Systems 4 th Edition, Pearson Education Limited,
Chapter 18 Query Processing and Optimization. Chapter Outline u Introduction. u Using Heuristics in Query Optimization –Query Trees and Query Graphs –Transformation.
Chapter 13: Query Processing
1 Minggu 6, Pertemuan 12 Query Processing Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
1 Chapter 23 Query Processing ownerNoclient Pearson Education © 2009.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Query Processing and Query Optimization Database System Implementation CSE 507 Slides adapted from Silberschatz, Korth and Sudarshan Database System Concepts.
Query Processing and Optimization, and Database Tuning
Query Optimization Heuristic Optimization
COMP3017 Advanced Databases
Database System Implementation CSE 507
Database Management System
Methodology – Physical Database Design for Relational Databases
Query Optimization Phases of query processing
Chapter 15 QUERY EXECUTION.
QUERY OPTIMIZATION.
Chapter 23 Query Processing Pearson Education © 2014.
Presentation transcript:

Chapter 18 Query Processing

2 Chapter - Objectives u Objectives of query processing and optimization. u Static versus dynamic query optimization. u How a query is decomposed and semantically analyzed. u How to create a R.A.T. to represent a query. u Rules of equivalence for RA operations. u How to apply heuristic transformation rules to improve efficiency of a query.

5 Introduction u In network and hierarchical DBMSs, low-level procedural query language is generally embedded in high-level programming language. u Programmer’s responsibility to select most appropriate execution strategy. 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. u Disk access tends to be dominant cost in query processing for centralized DBMS.

7 Query Processing 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 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.

9 Example Different Strategies Find all Managers that work at a London branch. SELECT * FROM staff s, branch b WHERE s.bno = b.bno AND (s.position = ‘Manager’ AND b.city = ‘London’); u Three equivalent RA queries are: (1)  (position = 'Manager')  (city = 'London')  (staff.bno = branch.bno) (Staff X Branch) (2)  (position='Manager')  (city='London') (Staff  staff.bno = branch.bno Branch) (3) (  position = 'Manager' (Staff))  staff.bno = branch.bno (  city = 'London' (Branch))

11 Example Different Strategies u Assume: – 1000 tuples in Staff; 50 tuples in Branch; – 50 Managers; 5 London branches; – No indexes or sort keys; – Results of any intermediate operations stored on disk; – Cost of the final write is ignored; – Tuples are accessed one at a time. u Cost (in disk accesses) are: (1) ( ) + 2*(1000 * 50) = (2) 2* ( ) = (3) * (50 + 5) = u Cartesian product and join operations are much more expensive than selection, and third option significantly reduces size of relations being joined together.

13 Phases of Query Processing u QP has four main phases: – decomposition (consisting of parsing and validation) – optimization – code generation – execution.

15 Dynamic versus Static Optimization u Two times when first three phases of QP can be carried out: – dynamically every time query is run. – Statically when query is first submitted. u Advantages if dynamic QO arise from fact that information is up-to-date. u Disadvantages are that performance of query is affected, time may limit finding optimum strategy. u Advantages of static QO are removal of runtime overhead, and more time to find optimum strategy. u Disadvantages arise from fact that chosen execution strategy may no longer be optimal when query is run. u Could use a hybrid approach to overcome this.

17 Query Decomposition u Aims are to transform high-level query into RA query and check that query is syntactically and semantically correct. u Typical stages are: – analysis, – normalization, – semantic analysis, – simplification, – query restructuring. u Analysis: – Analyze query lexically and syntactically using compiler techniques. –Verify relations and attributes exist. –Verify operations are appropriate for object type.

19 Analysis - Example SELECT staff_no FROM staff WHERE position > 10; u This query would be rejected on two grounds: – Staff_No is not defined for Staff relation (should be Sno). – Comparison ‘>10’ is incompatible with type Position, which is variable character string. u Finally, query transformed into some internal representation more suitable for processing. u Some kind of query tree is typically chosen, constructed as follows: – Leaf node created for each base relation. – Non-leaf node created for each intermediate relation produced by RA operation. – Root of tree represents query result. – Sequence is directed from leaves to root.

21 Example R.A.T.

22 Normalization u Converts query into a normalized form for easier manipulation. u Predicate can be converted into one of two forms: – Conjunctive normal form: (position = 'Manager'  salary > 20000)  (bno = 'B3') – Disjunctive normal form: (position = 'Manager'  bno = 'B3' )  (salary >  bno = 'B3')

23 Semantic Analysis u Rejects normalized queries that are incorrectly formulated or contradictory. u Query is incorrectly formulated if components do not contribute to generation of result. u Query is contradictory if its predicate cannot be satisfied by any tuple. u Algorithms to determine correctness exist only for queries that do not contain disjunction and negation. u For these queries, could construct: – Construct a relation connection graph. – Normalized attribute connection graph. Relation connection graph Create node for each relation and node for result. Create edges between two nodes that represent a join, and edges between nodes that represent projection. If not connected, query is incorrectly formulated.

Semantic Analysis - Normalized Attribute Connection Graph u Create node for each reference to an attribute, or constant 0. u Create directed edge between nodes that represent a join, and directed edge between attribute node and 0 node that represents selection. u Weight edges a  b with value c, if it represents inequality condition (a  b + c); weight edges 0  a with -c, if it represents inequality condition (a  c). u If graph has cycle for which valuation sum is negative, query is contradictory.

26 Example Checking Semantic Correctness SELECT p.pno, p.street FROM renter r, viewing v, property_for_rent p WHERE r.rno = v.rno AND r.max_rent >= 500 AND r.pref_type = 'Flat' AND p.ono = 'CO93'; u Relation connection graph not fully connected, so query is not correctly formulated. u Have omitted the join condition (v.pno = p.pno).

Example Checking Semantic Correctness Relation Connection graph Normalized attribute connection graph

Example Checking Semantic Correctness SELECT p.pno, p.street FROM renter r, viewing v, property_for_rent p WHERE r.max_rent > 500 AND r.rno = v.rno AND v.pno = p.pno AND r.pref_type = 'Flat' and r.max_rent < 200; u Normalized attribute connection graph has cycle between nodes R.Max_Rent and 0 with negative valuation sum, so query is contradictory.

29 Simplification – Detects redundant qualifications, – Eliminates common sub-expressions, – Transforms query to semantically equivalent but more easily and efficiently computed form. u Typically, access restrictions, view definitions, and integrity constraints are considered. u Assuming user has appropriate access privileges, first apply well-known idempotency rules of boolean algebra.

30 Transformation Rules for RA Operations u Conjunctive selection operations can cascade into individual selection operations (and vice versa).  p  q  r (R) =  p (  q (  r (R))) Sometimes referred to as cascade of selection.  bno='B3'  salary>15000 (Staff) =  bno='B3' (  salary>15000 (Staff)) u Commutativity of selection.  p (  q (R)) =  q (  p (R)) For example:  bno='B3' (  salary>15000 (Staff)) =  salary>15000 (  bno='B3' (Staff)) u In a sequence of projection operations, only the last in the sequence is required.  L  M …  N (R) =  L (R) For example:  lname  bno (Staff) =  lname (Staff)

Transformation Rules for RA Operations u Commutativity of selection and projection. If predicate p involves only attributes in projection list, selection and projection operations commute:  Ai, …, Am (  p (R)) =  p (  Ai, …, Am (R)) where p  {A 1, …, A m } For example:  fname,lname (  lname='Beech' (Staff))=  lname='Beech' (  fname,lname (Staff)) u Commutativity of theta-join (and Cartesian product). R  p S = S  p R R X S = S X R u Rule also applies to equi-join and natural join. For example: Staff  staff.bno=branch.bno Branch = Branch  staff.bno=branch.bno Staff u Commutativity of selection and theta-join (or Cartesian product). If selection predicate involves only attributes of one of join relations, selection and join (or Cartesian product) operations commute:  p (R  r S) = (  p (R))  r S  p (R X S) = (  p (R)) X S where p  {A 1, A 2, …, A n }

Transformation Rules for RA Operations u If selection predicate is conjunctive predicate having form (p  q), where p only involves attributes of R, and q only attributes of S, selection and theta-join operations commute as:  p  q (R  r S) = (  p (R))  r (  q (S))  p  q (R X S) = (  p (R)) X (  q (S)) For example:  position='Manager'  city='London' (Staff  staff.bno=branch.bno Branch) = (  position='Manager' (Staff))  staff.bno=branch.bno (  city='London' (Branch)) u Commutativity of projection and theta-join (or Cartesian product). 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))

Transformation Rules for RA Operations u 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))) For example:  position, city, bno (Staff  staff.bno=branch.bno Branch) = (  position, bno (Staff))  staff.bno=branch.bno (  city, bno (Branch)) u and using the latter rule:  position, city (Staff  staff.bno=branch.bno Branch) =  position, city ((  position, bno (Staff))  staff.bno=branch.bno (  city, bno (Branch))) u Commutativity of union and intersection (but not set difference). R  S = S  R R  S = S  R

Transformation Rules for RA Operations Commutativity of selection and set operations (union, intersection, and set difference).  p (R  S) =  p (S)   p (R)  p (R  S) =  p (S)   p (R)  p (R - S) =  p (S) -  p (R) u Commutativity of projection and union.  L (R  S) =  L (S)   L (R) Associativity of union and intersection (but not set difference). (R  S)  T = S  (R  T) (R  S)  T = S  (R  T)

Transformation Rules for RA Operations u Associativity of theta-join (and Cartesian product). Cartesian product and natural join are always associative: (R  S)  T = R  (S  T) (R X S) X T = R X (S X T) If join condition q involves attributes only from S and T, then theta-join is associative as follows: (R  p S)  q  r T = R  p  r (S  q T) For example: (Staff  staff.sno=property_for_rent.sno Property_for_Rent) ono=owner.ono  staff.lname=owner.lname Owner = Staff  staff.sno=property_for_rent.sno  staff.lname=lname (Property_for_Rent  ono Owner)

Example 18.3 Use of Transformation Rules For prospective renters of flats, find properties that match requirements and owned by CO93. SELECT p.pno, p.street FROM renter r, viewing v, property_for_rent p WHERE r.pref_type = 'Flat' AND r.rno = v.rno AND v.pno = p.pno AND r.max_rent >= p.rent AND r.pref_type = p.type AND p.ono = 'CO93';

47 Example 18.3 Use of Transformation Rules

48 Example 18.3 Use of Transformation Rules

49 Example 18.3 Use of Transformation Rules

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.