Query Processing Bayu Adhi Tama, MTI. 1 ownerNoclient © Pearson Education Limited 1995, 2005.

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
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
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.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
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.
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.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
©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.
Context Tailoring the DBMS –To support particular applications Beyond alphanumerical data Beyond retrieve + process –To support particular hardware New.
CS 8630 Database Administration, Dr. Guimaraes , Physical Design and Performance Class Will Start Momentarily… CS8630 Database Administration.
Database Management 9. course. Execution of queries.
Chapter 16 Methodology – Physical Database Design for Relational Databases.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Query Optimization Chap. 19. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying where.
1 Chapter 20 Query Processing Transparencies. 2 Chapter 20 - Objectives u Objectives of query processing and optimization. u Static versus dynamic query.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Methodology – Physical Database Design for Relational Databases.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
Chapter 18 Query Processing. 2 Chapter - Objectives u Objectives of query processing and optimization. u Static versus dynamic query optimization. u How.
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.
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 13 Query Optimization Yonsei University 1 st Semester, 2015 Sanghyun Park.
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.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
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
Prepared by : Ankit Patel (226)
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Chapter 15 QUERY EXECUTION.
Chapter Name SQL: Data Manipulation
QUERY OPTIMIZATION.
Chapter 23 Query Processing Pearson Education © 2014.
CS222: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Presentation transcript:

Query Processing Bayu Adhi Tama, MTI. 1 ownerNoclient © Pearson Education Limited 1995, 2005

Introduction 2  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.  Disk access tends to be dominant cost in query processing for centralized DBMS.  Programmer’s responsibility to select most appropriate execution strategy. © Pearson Education Limited 1995, 2005

Query Processing 3 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 strategy to retrieve required data. © Pearson Education Limited 1995, 2005

Query Optimization 4 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. © Pearson Education Limited 1995, 2005

Example Different Strategies 5 Find all Managers who work at a London branch. SELECT * FROM Staff s, Branch b WHERE s.branchNo = b.branchNo AND (s.position = ‘Manager’ AND b.city = ‘London’); © Pearson Education Limited 1995, 2005

Example Different Strategies 6  Three equivalent RA queries are: (1)  (position='Manager')  (city='London')  (Staff.branchNo=Branch.branchNo) (Staff X Branch) (2)  (position='Manager')  (city='London') ( Staff Staff.branchNo=Branch.branchNo Branch) (3) (  position='Manager' (Staff)) Staff.branchNo=Branch.branchNo (  city='London' (Branch)) © Pearson Education Limited 1995, 2005

Example Different Strategies 7  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. © Pearson Education Limited 1995, 2005

Example Cost Comparison 8  Cost (in disk accesses) are: (1) ( ) + 2*(1000 * 50) = (2) 2* ( ) = (3) * (50 + 5) =  Cartesian product and join operations much more expensive than selection, and third option significantly reduces size of relations being joined together. © Pearson Education Limited 1995, 2005

Phases of Query Processing 9  QP has four main phases:  decomposition (consisting of parsing and validation);  optimization;  code generation;  execution. © Pearson Education Limited 1995, 2005

Phases of Query Processing 10 © Pearson Education Limited 1995, 2005

Dynamic versus Static Optimization 11  Two times when first three phases of QP can be carried out:  dynamically every time query is run;  statically when query is first submitted.  Advantages of dynamic QO arise from fact that information is up to date.  Disadvantages are that performance of query is affected, time may limit finding optimum strategy. © Pearson Education Limited 1995, 2005

Dynamic versus Static Optimization 12  Advantages of static QO are removal of runtime overhead, and more time to find optimum strategy.  Disadvantages arise from fact that chosen execution strategy may no longer be optimal when query is run.  Could use a hybrid approach to overcome this. © Pearson Education Limited 1995, 2005

Query Decomposition 13  Aims are to transform high-level query into RA query and check that query is syntactically and semantically correct.  Typical stages are:  analysis,  normalization,  semantic analysis,  simplification,  query restructuring. © Pearson Education Limited 1995, 2005

Analysis 14  Analyze query lexically and syntactically using compiler techniques.  Verify relations and attributes exist.  Verify operations are appropriate for object type. © Pearson Education Limited 1995, 2005

Analysis - Example 15 SELECT staff_no FROM Staff WHERE position > 10;  This query would be rejected on two grounds:  staff_no is not defined for Staff relation (should be staffNo).  Comparison ‘>10’ is incompatible with type position, which is variable character string. © Pearson Education Limited 1995, 2005

Analysis 16  Finally, query transformed into some internal representation more suitable for processing.  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. © Pearson Education Limited 1995, 2005

Example R.A.T. 17 © Pearson Education Limited 1995, 2005

Normalization 18  Converts query into a normalized form for easier manipulation.  Predicate can be converted into one of two forms: Conjunctive normal form: (position = 'Manager'  salary > 20000)  (branchNo = 'B003') Disjunctive normal form: (position = 'Manager'  branchNo = 'B003' )  (salary >  branchNo = 'B003') © Pearson Education Limited 1995, 2005

Simplification 19  Detects redundant qualifications,  eliminates common sub-expressions,  transforms query to semantically equivalent but more easily and efficiently computed form.  Typically, access restrictions, view definitions, and integrity constraints are considered.  Assuming user has appropriate access privileges, first apply well-known idempotency rules of boolean algebra. © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 20 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.  branchNo='B003'  salary>15000 (Staff) =  branchNo='B003' (  salary>15000 (Staff)) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 21 Commutativity of Selection.  p (  q (R)) =  q (  p (R))  For example:  branchNo='B003' (  salary>15000 (Staff)) =  salary>15000 (  branchNo='B003' (Staff)) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 22 In a sequence of Projection operations, only the last in the sequence is required.  L  M …  N (R) =  L (R)  For example:  lName  branchNo, lName (Staff) =  lName (Staff) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 23 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 2, …, A m }  For example:  fName, lName (  lName='Beech' (Staff)) =  lName='Beech' (  fName,lName (Staff)) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 24 Commutativity of Theta join (and Cartesian product). R p S = S p R R X S = S X R u Rule also applies to Equijoin and Natural join. For example: Staff staff.branchNo=branch.branchNo Branch = Branch staff.branchNo=branch.branchNo Staff © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 25 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 } © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 26  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)) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 27  For example:  position='Manager'  city='London' (Staff Staff.branchNo=Branch.branchNo Branch) = (  position='Manager' (Staff)) Staff.branchNo=Branch.branchNo (  city='London' (Branch)) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 28 Commutativity of Projection and Theta join (or Cartesian product).  If projection list is of form L = L 1  L 2, where L 1 only has attributes of R, and L 2 only has attributes of S, provided join condition only contains attributes of L, Projection and Theta join commute:  L1  L2 (R r S) = (  L1 (R)) r (  L2 (S)) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 29  If join condition contains additional attributes not in L (M = M 1  M 2 where M 1 only has attributes of R, and M 2 only has attributes of S), a final projection operation is required:  L1  L2 (R r S) =  L1  L2 ( (  L1  M1 (R)) r (  L2  M2 (S))) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 30  For example:  position,city,branchNo (Staff Staff.branchNo=Branch.branchNo Branch) = (  position, branchNo (Staff)) Staff.branchNo=Branch.branchNo (  city, branchNo (Branch))  and using the latter rule:  position, city (Staff Staff.branchNo=Branch.branchNo Branch) =  position, city ((  position, branchNo (Staff)) Staff.branchNo=Branch.branchNo (  city, branchNo (Branch))) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 31 Commutativity of Union and Intersection (but not set difference). R  S = S  R R  S = S  R © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 32 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) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 33 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) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 34 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: (R p S) q  r T = R p  r (S q T) © Pearson Education Limited 1995, 2005

Transformation Rules for RA Operations 35  For example: (Staff Staff.staffNo=PropertyForRent.staffNo PropertyForRent) ownerNo=Owner.ownerNo  staff.lName=Owner.lName Owner = Staff staff.staffNo=PropertyForRent.staffNo  staff.lName=lName (PropertyForRent ownerNo Owner) © Pearson Education Limited 1995, 2005

Example 21.3 Use of Transformation Rules 36 For prospective renters of flats, find properties that match requirements and owned by CO93. SELECT p.propertyNo, p.street FROM Client c, Viewing v, PropertyForRent p WHERE c.prefType = ‘Flat’ AND c.clientNo = v.clientNo AND v.propertyNo = p.propertyNo AND c.maxRent >= p.rent AND c.prefType = p.type AND p.ownerNo = ‘CO93’; © Pearson Education Limited 1995, 2005

Example 21.3 Use of Transformation Rules 37 © Pearson Education Limited 1995, 2005

Example 21.3 Use of Transformation Rules 38 © Pearson Education Limited 1995, 2005

Example 21.3 Use of Transformation Rules 39 © Pearson Education Limited 1995, 2005

Heuristical Processing Strategies 40  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. © Pearson Education Limited 1995, 2005

Heuristical Processing Strategies 41  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. © Pearson Education Limited 1995, 2005

Enumeration of Alternative Strategies 42  Fundamental to efficiency of QO is the search space of possible execution strategies and the enumeration algorithm used to search this space.  Query with 2 joins gives 12 join orderings: R (S T) R (T S)(S T) R(T S) R S (R T) S (T R)(R T) S(T R) S T (R S) T (S R)(R S) T(S R) T  With n relations, (2(n – 1))!/(n – 1)! orderings.  If n = 4 this is 120; if n = 10 this is > 176 billion.  Compounded by different selection/join methods. © Pearson Education Limited 1995, 2005

Pipelining 43  Materialization - output of one operation is stored in temporary relation for processing by next.  Could also pipeline results of one operation to another without creating temporary relation.  Known as pipelining or on-the-fly processing.  Pipelining can save on cost of creating temporary relations and reading results back in again.  Generally, pipeline is implemented as separate process or thread. © Pearson Education Limited 1995, 2005

Types of Trees 44 © Pearson Education Limited 1995, 2005