Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
CS 540 Database Management Systems
Manajemen Basis Data Pertemuan Matakuliah: M0264/Manajemen Basis Data Tahun: 2008.
Foundations of Relational Implementation n Defining Relational Data n Relational Data Manipulation n Relational Algebra.
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.
Query Execution Optimizing Performance. Resolving an SQL query Since our SQL queries are very high level, the query processor must do a lot of additional.
COMP 451/651 Optimizing Performance
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Query Execution Professor: Dr T.Y. Lin Prepared by, Mudra Patel Class id: 113.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Query Processing (overview)
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
CS 4432query processing1 CS4432: Database Systems II.
ICS (072)Query Processing and Optimization 1 Chapter 15 Algorithms for Query Processing and Optimization ICS 424 Advanced Database Systems Dr.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Query Execution Professor: Dr T.Y. Lin Prepared by, Mudra Patel Class id: 113.
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.
Query Processing & Optimization
Chapter 19 Query Processing and Optimization
Query Execution Chapter 15 Section 15.1 Presented by Khadke, Suvarna CS 257 (Section II) Id
T HE Q UERY C OMPILER Prepared by : Ankit Patel (226)
CPS216: Advanced Database Systems Notes 03:Query Processing (Overview, contd.) Shivnath Babu.
Query Processing Presented by Aung S. Win.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
CSCE Database Systems Chapter 15: Query Execution 1.
Advanced Database Systems Notes:Query Processing (Overview) Shivnath Babu.
Database Management 9. course. Execution of queries.
CPS216: Advanced Database Systems Notes 07:Query Execution Shivnath Babu.
©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.
Advanced Databases: Lecture 6 Query Optimization (I) 1 Introduction to query processing + Implementing Relational Algebra Advanced Databases By Dr. Akhtar.
Copyright © Curt Hill Query Evaluation Translating a query into action.
1-1 Homework 3 Practical Implementation of A Simple Rational Database Management System.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan Chapter 13: Query Processing.
CPS216: Data-Intensive Computing Systems Introduction to Query Processing Shivnath Babu.
From Relational Algebra to SQL CS 157B Enrique Tang.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Query Execution Section 15.1 Shweta Athalye CS257: Database Systems ID: 118 Section 1.
CS 257 Chapter – 15.9 Summary of Query Execution Database Systems: The Complete Book Krishna Vellanki 124.
16.7 Completing the Physical- Query-Plan By Aniket Mulye CS257 Prof: Dr. T. Y. Lin.
CS4432: Database Systems II Query Processing- Part 2.
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
CS 440 Database Management Systems Lecture 5: Query Processing 1.
CS 540 Database Management Systems
Chapter 13: Query Processing
1 Minggu 6, Pertemuan 12 Query Processing Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
CS4432: Database Systems II Query Processing- Part 1 1.
Database Applications (15-415) DBMS Internals- Part VIII Lecture 19, March 29, 2016 Mohammad Hammoud.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
Query Execution Chapter 15 Section 15.1 Presented by Khadke, Suvarna CS 257 (Section II) Id
15.1 – Introduction to physical-Query-plan operators
Prepared by : Ankit Patel (226)
Overview of Query Optimization
Chapter 15 QUERY EXECUTION.
Query Execution Presented by Khadke, Suvarna CS 257
Database Management Systems (CS 564)
Introduction to Database Systems
Examples of Physical Query Plan Alternatives
Query Optimization CS 157B Ch. 14 Mien Siao.
Query Execution Presented by Jiten Oswal CS 257 Chapter 15
CPS216: Advanced Database Systems Notes 03:Query Processing (Overview, contd.) Shivnath Babu.
Presentation transcript:

Query Optimization CS 157B Ch. 14 Mien Siao

Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction in DBMS Cost-based query Optimization: Algebraic Expressions

Introduction What is Query Optimization? Suppose you were given a chance to visit 15 pre-selected different cities in Europe. The only constraint would be ‘Time’ -> Would you have a plan to visit the cities in any order?

Europe

Plan: -> Place the 15 cities in different groups based on their proximity to each other. -> Start with one group and move on to the next group. Important point made over here is that you would have visited the cities in a more organized manner, and the ‘Time’ constraint mentioned earlier would have been dealt with efficiently.

Query Optimization works in a similar way: There can be many different ways to get an answer from a given query. The result would be same in all scenarios. DBMS strive to process the query in the most efficient way (in terms of ‘Time’) to produce the answer. Cost = Time needed to get all answers

Starting with System-R, most of the commercial DBMSs use cost-based optimizers. The estimation should be accurate and easy. Another important point is the need for being logically consistent because the least cost plan will always be consistently low.

Steps in a Cost-based query optimization 1. Parsing 2. Transformation 3. Implementation 4. Plan selection based on cost estimates

Query Flow Parser Optimizer Code Generator/ Interpreter Processor SQL

Query Parser – Verify validity of the SQL statement. Translate query into an internal structure using relational calculus. Query Optimizer – Find the best expression from various different algebraic expressions. Criteria used is ‘Cheapness’ Code Generator/Interpreter – Make calls for the Query processor as a result of the work done by the optimizer. Query Processor – Execute the calls obtained from the code generator.

Cost of physical plans includes processor time and communication time. The most important factor to consider is disk I/Os because it is the most time consuming action. Some other costs associated are: - Operations (joins, unions, intersections). - The order of operations. Why?

Joins, unions, and intersections are associative and commutative. - Management of storage of arguments and passing of it. Factors mentioned above should be limited and minimized when creating the best physical plan.

Projection Example: Projections produce a result tuple for every argument tuple. What is the change? Change in the output size is the change in the length of tuples Let’s take a relation ‘R’ Relation (20,000 tuples): R(a, b, c) Each Tuple (190 bytes): header = 24 bytes, a = 8 bytes, b = 8 bytes, c = 150 bytes Each Block (1024): header = 24 bytes

We can fit 5 tuples into 1 block - 5 tuples * 190 bytes/tuple = 950 bytes can fit into 1 block - For 20,000 tuples, we would require 4,000 blocks (20,000 / 5 tuples per block = 4,000 With a projection resulting in elimination of column c (150 bytes), we could estimate that each tuple would decrease to 40 bytes (190 – 150 bytes)

Now, the new estimate will be 25 tuples in 1 block tuples * 40 bytes/tuple = 1000 bytes will be able to fit into 1 block - With 20,000 tuples, the new estimate is 800 blocks (20,000 tuples / 25 tuples per block = 800 blocks) Result is reduction by a factor of 5

Query interaction in DBMS How does a query interact with a DBMS? - Interactive users - Embedded queries in programs written in C, C++, etc. What is the difference between these two ?

Interactive Users: - When there is an interactive user query, the query goes through the Query Parser, Query Optimizer, Code Generator, and Query Processor each time.

Embedded Query: - When there is an embedded query, the query does not have to through the Query Parser, Query Optimizer, Code Generator, and the Query Processor each time.

- In an embedded query, the calls generated by the code generator are stored in the database. Each time the query is reached within the program at run-time, the Query Processor invokes the stored calls in the database. - Optimization is independent in embedded queries.

Cost-based query Optimization: Algebraic Expressions If we had the following query- SELECT p.pname, d.dname FROM Patients p, Doctors d WHERE p.doctor = d.dname AND d.dgender = ‘M’

projection filter join Scan (Patients)Scan (Doctors)

Cost-based query Optimization : Transformation projectionprojection filter join join Filter Scan (Patients) Scan (Doctors) Scan(Patients) Scan(Doctors)

Cost-based query Optimization: Implementation projection projection filter hash join natural join filter Scan(Patients) Scan(Doctors)

Cost-based query Optimization: Plan selection based on costs projection projection filter hash join natural join filter Scan(Patients) Scan(Doctors) Estimated Costs = 100ms Estimated Costs = 50ms