CSC 742 Database Management Systems

Similar documents
Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Chapter 3. Algorithms for Query Processing and Optimization

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Database Systems External Sorting and Query Optimization. A.R. Hurson 323 CS Building

ECE 650 Systems Programming & Engineering. Spring 2018

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Outline. Query Processing Overview Algorithms for basic operations. Query optimization. Sorting Selection Join Projection

Chapter 19 Query Optimization

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

Simple SQL Queries (contd.)

Relational Calculus: 1

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Relational Algebra 1

Chapter 8: Relational Algebra

Relational Algebra. Ron McFadyen ACS

Query Processing & Optimization. CS 377: Database Systems

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

ECE 650 Systems Programming & Engineering. Spring 2018

Slides by: Ms. Shree Jaswal

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

CS 348 Introduction to Database Management Assignment 2

Advanced Databases. Winter Term 2012/13. Prof. Dr. Dietmar Seipel University of Würzburg. Advanced Databases Winter Term 2012/13

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Database design process

Part 1 on Table Function

Mobile and Heterogeneous databases Distributed Database System Query Processing. A.R. Hurson Computer Science Missouri Science & Technology

Ch 5 : Query Processing & Optimization

A taxonomy of SQL queries Learning Plan

SQL STRUCTURED QUERY LANGUAGE

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

Query Processing and Optimization *

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Some different database system architectures. (a) Shared nothing architecture.

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Query 2: Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry, Bellaire, TX Wallace 291 Berry, Bellaire, TX

Components of a DMBS

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

Outline. Note 1. CSIE30600 Database Systems More SQL 2

Chapter 8: The Relational Algebra and The Relational Calculus

Chapter 6 Part I The Relational Algebra and Calculus

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

CIS611 Lab Assignment 1 SS Chung

Overview Relational data model

The Relational Algebra

CS 377 Database Systems

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

RELATIONAL DATA MODEL: Relational Algebra

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Course Notes on Relational Algebra

DC62 DATABASE MANAGEMENT SYSTEMS DEC 2015

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Chapter 6 The Relational Algebra and Calculus

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

CS5300 Database Systems

CS 338 Nested SQL Queries

More SQL: Complex Queries, Triggers, Views, and Schema Modification

DBMS LAB SESSION PAVANKUMAR MP

Session Active Databases (2+3 of 3)

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

SQL Introduction. CS 377: Database Systems

Database Technology. Topic 3: SQL. Olaf Hartig.

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

CSIE30600 Database Systems Basic SQL 2. Outline

PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore ) Department of MCA. Solution Set - Test-II

Data Definition Language (DDL)

Querying a Relational Database COMPANY database For Lab4, you use the Company database that you built in Lab2 and used for Lab3

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe Slide 25-1

Information Systems Development 37C Lecture: Final notes. 30 th March 2017 Dr. Riitta Hekkala

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Relational Model. CS 377: Database Systems

Government of Karnataka Department of Technical Education Bengaluru

Applying evolutionary algorithms to materialized view selection in a data warehouse

QUERY PROCESSING & OPTIMIZATION CHAPTER 19 (6/E) CHAPTER 15 (5/E)

Integrity Coded Relational Databases (ICRDB) - Protecting Data Integrity in Clouds

DATABASE CONCEPTS. Dr. Awad Khalil Computer Science & Engineering Department AUC

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Improving Query Plans. CS157B Chris Pollett Mar. 21, 2005.

2.2.2.Relational Database concept

SQL- Updates, Asser0ons and Views

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

UNIVERSITY OF TORONTO MIDTERM TEST SUMMER 2017 CSC343H Introduction to Databases Instructor Tamanna Chhabra Duration 120 min No aids allowed

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

This chapter discusses how to design a relational

CSC 261/461 Database Systems Lecture 19

CMP-3440 Database Systems

Translation ER/EER to relational

Mapping ER Diagrams to. Relations (Cont d) Mapping ER Diagrams to. Exercise. Relations. Mapping ER Diagrams to Relations (Cont d) Exercise

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Announcement5 SQL5. Create%and%drop%table5. Basic%SFW%query5. Reading%a%table5. TDDD37%% Database%technology% SQL5

Transcription:

CSC 742 Database Management Systems Topic #16: Query Optimization Spring 2002 CSC 742: DBMS by Dr. Peng Ning 1

Agenda Typical steps of query processing Two main techniques for query optimization Heuristics based query optimization Cost based query optimization Translating SQL queries into relational algebra Heuristics based query optimization Spring 2002 CSC 742: DBMS by Dr. Peng Ning 2

Typical Steps when Processing A Highlevel Query Query Scanning, Parsing, and Validating Intermediate form of query Query Optimizer Execution plan Query Code Generator Code to execute the query Runtime database processor Query result Spring 2002 CSC 742: DBMS by Dr. Peng Ning 3

Questions Difference between high-level query languages and highlevel programming languages in terms of queries? SQL v.s. C Can we find the best execution plan? Do we always use the best execution plan? Spring 2002 CSC 742: DBMS by Dr. Peng Ning 4

Two Main Techniques for Query Optimization Heuristic Rules A heuristic is a rule that works well in most of cases, but not always. Cost based query optimization Estimate the cost for each execution plan, and choose the one with the lowest cost. Can we get the best execution plan? Spring 2002 CSC 742: DBMS by Dr. Peng Ning 5

Translating SQL queries into extended relational algebra SQL queries are decomposed into query blocks Each query block contains a single SELECT FROM WHERE expression as well as GROUP BY and HAVING clauses. Nested queries within a query are identified as separate query blocks. Query optimization Choose an execution plan for each block Spring 2002 CSC 742: DBMS by Dr. Peng Ning 6

SELECT LNAME, FNAME FROM EMPLOYE WHERE SALARY > (SELECT MAX(SALARY) FROM EMPLOYEE WHERE DNO=5); SELECT LNAME, FNAME FROM EMPLOYE WHERE SALARY >C SELECT MAX(SALARY) FROM EMPLOYEE WHERE DNO=5 Spring 2002 CSC 742: DBMS by Dr. Peng Ning 7

SELECT DNAME, MAX(SALARY) AS MAX_SALARY FROM EMPLOYE GROUP BY DNO, DNAME HAVING DNO > 3 Spring 2002 CSC 742: DBMS by Dr. Peng Ning 8

Basic Operations DBMS implements basic algorithms for certain operations Select Project Join Cartesian Product Set operations These operations are combined to form the execution plans. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 9

Query Tree A query tree is a data structure that corresponds to a relational algebra expression. Input relations are leaf nodes of the tree Relational operations are internal nodes. An execution of the query tree consists of executing an internal node operation whenever its operands are available, and then replacing the node by the relation that results from executing the operation. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 10

For every project located in Stafford, retrieve the project Number, the controlling department number, and the department Manager s last name, address, and birthdate. π PNUMBER, DNUM, LNAME, ADDRESS, BDATE (((σ PLOCATION= Stafford (PROJECT)) >< DNUM=DNUMBER (DEPARTMENT)) >< MGRSSN=SSN (EMPLOYEE)) Employee Department Project Spring 2002 CSC 742: DBMS by Dr. Peng Ning 11

Using Heuristics in Query Optimization An example of a heuristic Apply SELECT before join. Questions: Does it work in most of cases? Exception? Spring 2002 CSC 742: DBMS by Dr. Peng Ning 12

Using Heuristics in Query Optimization General idea Many different relational algebra expressions (and thus query trees) are equivalent. Transform the initial query tree of a query into an equivalent final query tree that is efficient to execute. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 13

Algorithm Break up any SELECT operations with conjunctive conditions into a cascade of SELECT operations. Push SELECT operations as far down the query tree as possible Rearrange binary operations so that the most restrictive SELECT operations are executed first Avoid CARTESIAN PRODUCT Try to combine a CARTESIAN PRODUCT with a SELECT operation into a join operation. Break up PROJECT operation and move lists of projection attributes as down the tree as possible by creating new project operations. Identify sub-trees that represent groups of operations that can be executed by a single algorithm. Spring 2002 CSC 742: DBMS by Dr. Peng Ning 14

SELECT LNAME FROM EMPLOYEE, WORKS_ON, PROJECT WHERE PNAME= Aquarius AND PNUMBER=PNO AND ESSN=SSN AND BDATE> 1957-12-31 π LNAME σ PNAME= Aquarius AND PNUMBER=PNO AND ESSN=SSN AND BDATE> 1957-12-31 x x PROJECT EMPLOYEE WORKS_ON Spring 2002 CSC 742: DBMS by Dr. Peng Ning 15

π LNAME Step 1: Break up SELECT operations σ PNAME= Aquarius σ PNUMBER=PNO σ ESSN=SSN σ BDATE> 1957-12-31 x x PROJECT EMPLOYEE WORKS_ON Spring 2002 CSC 742: DBMS by Dr. Peng Ning 16

Step 2: Push down SELECT operations π LNAME σ PNUMBER=PNO x σ ESSN=SSN σ PNAME= Aquarius x PROJECT σ BDATE> 1957-12-31 WORKS_ON EMPLOYEE Spring 2002 CSC 742: DBMS by Dr. Peng Ning 17

Step 3: Apply restrictive SELECT first, Avoid cartesian product π LNAME σ ESSN=SSN x σ PNUMBER=PNO σ BDATE> 1957-12-31 x EMPLOYEE σ PNAME= Aquarius WORKS_ON PROJECT Spring 2002 CSC 742: DBMS by Dr. Peng Ning 18

Step 4: Combine a Cartesian product With a subsequent SELECT into A join. π LNAME >< ESSN=SSN ESSN SSN, LNAME >< PNUMBER=PNO σ BDATE> 1957-12-31 PNUMBER σ PNAME= Aquarius ESSN, PNO WORKS_ON EMPLOYEE PROJECT Spring 2002 CSC 742: DBMS by Dr. Peng Ning 19

Step 5: Push down PROJECT Operations. π LNAME >< ESSN=SSN π ESSN π SSN, LNAME >< PNUMBER=PNO σ BDATE> 1957-12-31 EMPLOYEE π PNUMBER π ESSN, PNO σ PNAME= Aquarius WORKS_ON PROJECT Spring 2002 CSC 742: DBMS by Dr. Peng Ning 20

Step 6: Identify sub-trees π LNAME >< ESSN=SSN π ESSN π SSN, LNAME >< PNUMBER=PNO σ BDATE> 1957-12-31 EMPLOYEE π PNUMBER π ESSN, PNO σ PNAME= Aquarius WORKS_ON PROJECT Spring 2002 CSC 742: DBMS by Dr. Peng Ning 21