Query Optimization (CB Chapter 23.1-23.3) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems: An Application Oriented.

Slides:



Advertisements
Similar presentations
Chapter 13: Query Processing
Advertisements

Query optimisation.
Examples of Physical Query Plan Alternatives
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Foundations of Relational Implementation n Defining Relational Data n Relational Data Manipulation n Relational Algebra.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
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.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
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.
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
CS 4432query processing1 CS4432: Database Systems II.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapter 15.
Query Processing & Optimization
©Silberschatz, Korth and Sudarshan14.1Database System Concepts 3 rd Edition Chapter 14: Query Optimization Overview Catalog Information for Cost Estimation.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
Query Processing Presented by Aung S. Win.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 13: Query Processing.
Access Path Selection in a Relational Database Management System Selinger et al.
Database systems/COMP4910/Melikyan1 Relational Query Optimization How are SQL queries are translated into relational algebra? How does the optimizer estimates.
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
Database Management 9. course. Execution of queries.
Query optimization in relational DBs Leveraging the mathematical formal underpinnings of the relational model.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
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.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
CS 338Query Evaluation7-1 Query Evaluation Lecture Topics Query interpretation Basic operations Costs of basic operations Examples Textbook Chapter 12.
12.1Database System Concepts - 6 th Edition Chapter 12: Query Processing Overview Measures of Query Cost Selection Operation Join Operation Sorting 、 Other.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
The Volcano Optimizer Generator Extensibility and Efficient Search.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
QUERY PROCESSING RELATIONAL DATABASE KUSUMA AYU LAKSITOWENING
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
CSCI Query Processing1 QUERY PROCESSING & OPTIMIZATION Dr. Awad Khalil Computer Science Department AUC.
Query Optimization CMPE 226 Database Systems By, Arjun Gangisetty
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
Query Processing – Implementing Set Operations and Joins Chap. 19.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
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
UNIT 11 Query Optimization
Database Management System
Relational Algebra Chapter 4, Part A
Overview of Query Optimization
Chapter 15 QUERY EXECUTION.
CPSC-310 Database Systems
Examples of Physical Query Plan Alternatives
Query Processing B.Ramamurthy Chapter 12 11/27/2018 B.Ramamurthy.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Query Optimization.
CPSC-608 Database Systems
Query Processing.
Course Instructor: Supriya Gupta Asstt. Prof
Presentation transcript:

Query Optimization (CB Chapter ) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems: An Application Oriented Approach 2ed by Kifer, Bernstein & Lewis, © Addison Wesley 2005)

SQL Widely used (only?) standard query language for relational databases Once SEQUEL (Structured English QUEry Language), now Structured Query Language Objectives –Easy to learn, easy to use –Create and modify the database and query from it DDL defines DML manipulates

SQL is Declarative, RA is Procedural SQL Statements describe the desired results, but do not specify a sequence of operations to get those results Relational Algebra expressions describe a specific sequence of operations to perform To evaluate a SQL statement, it needs to be translated into (a computer implementation of) RA first!

Query Processing Query Processing is the translation of SQL into RA- like nested function calls One query can have multiple translations SELECT roomNo, HotelName FROM Room, Hotel WHERE HotelName = ‘Savoy’ and Room.hotelNo=Hotel.hotelNo;  roomNo,HotelName (  hotelName=Savoy and Room.hotelNo=Hotel.hotelNo (Room x Hotel) )  roomNo,HotelName (  Room.hotelNo=Hotel.hotelNo ( (  hotelName=Savoy (Hotel)) x Room) )

Query Optimization Choose the translation that minimizes resource use (time, space) The second translation below is better. (Why?) SELECT roomNo, HotelName FROM Room, Hotel WHERE HotelName = ‘Savoy’ and Room.hotelNo=Hotel.hotelNo;  roomNo,HotelName (  hotelName=Savoy and Room.hotelNo=Hotel.hotelNo (Room x Hotel) )  roomNo,HotelName ( (  hotelName=Savoy (Hotel))  Room.hotelNo=Hotel.hotelNo Room)

Query Processing User SQL Decom- position Relational Algebra Optimi- zation Processing Engine Result (table) Efficient Rel. Algebra

More Detail of Query Processing

Steps in Query Processing Query Decomposition (create relational algebra expression) Query Optimization (create execution plan) Code Generation Query Execution

Parts of Optimization Query Plan Generator –Comes up with viable relational algebra expressions to improve the initial naïve one Cost Estimator –Estimates the cost (time / space) of each plan Optimization –Choosing the plan with the lowest cost, or at least “reasonably cheap”

Query Decomposition Check Syntax Build a relational algebra tree HotelRoom  hotelName=Savoy  Room.hotelNo=Hotel.hotelNo X

Query Transformation (Selection) Select with multiple AND conditions can be sequence of selects  hotelName=Savoy and Room.hotelNo=Hotel.hotelNo ( …) =  hotelName=Savoy (  Room.hotelNo=Hotel.hotelNo ( …)) Order of Select operations doesn’t matter =  Room.hotelNo=Hotel.hotelNo (  hotelName=Savoy ( …))

Query Transformation (Projection) Extra intermediate projections don’t matter  Name (  Name, Status (Student))=  Name (Student) Order of select and project doesn’t matter  Status=‘SR’ (  Status (Student)) =  Status (  Status=‘SR’ (Student))

Query Transformation (Join) Push Select through Join –Replace a select on a cross-product with a join –Joins can be implemented at the lowest level more efficiently than “materialized cross-product” Push Select through Product –If the attributes of the condition all belong to one table of the join, put the select on only the one table, so a smaller table is joined –Joins on smaller tables are faster than on larger ones More rules pp

Pushing Select Example Find all seniors that take CPSC356  stu_id=id & crs=‘CPSC356’ (Student x Transcript) Separate the selects  stu_id=id (  crs=‘CPSC356’ (Student x Transcript)) Push the inner select  stu_id=id (Student x (  crs=‘CPSC356’ (Transcript))) Replace Select/Product by Join Student |x| stu_id=id (  crs=‘CPSC356’ (Transcript)))

Query Processing Example

Execution Plans Add specific algorithms to each relational algebra step Determine whether/how indices will be used Add pipelining (not storing intermediate data) where possible

Choosing Transformations Estimate cost of each tree based on –Table sizes –Numbers of distinct attribute values –Average number of tuples for selection condition –Methods used for join (e.g. indexed, hashed) Choose lowest cost tree Because estimates aren’t perfect, the absolute best tree might not be chosen!

Heuristics (Rules of Thumb) Perform Selection as early as possible –Unless doing it later lets you use an index Combine X and Selection into join operation Execute most restrictive Selections first Perform Projection as early as possible Compute common expressions once –Creating a view is a way to do this!

Consequences for SQL Programmer Using RA operations in SQL (e.g. explicit Join) constrains optimization –Good when “programmer knows best” –Bad when programmer prevents a better optimization Intermediate tables (i.e. views) can constrain optimization –Use views to compute common subexpressions When performance is substandard, tweaking the SQL can help! (Remember the heuristics).