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

Similar documents
Query Processing and Optimization

Background material for Chapter 3 taken from CS 245

Query Processing: an Overview. Query Processing in a Nutshell. .. CSC 468 DBMS Implementation Alexander Dekhtyar.. QUERY. Parser.

Optimization of logical query plans Eliminating redundant joins

The query processor turns user queries and data modification commands into a query plan - a sequence of operations (or algorithm) on the database

Query Optimization Overview. COSC 404 Database System Implementation. Query Optimization. Query Processor Components The Parser

COSC 404 Database System Implementation. Query Optimization. Dr. Ramon Lawrence University of British Columbia Okanagan

Database System Concepts

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop

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

CSC 742 Database Management Systems

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

Joins, NULL, and Aggregation

Lecture Query evaluation. Combining operators. Logical query optimization. By Marina Barsky Winter 2016, University of Toronto

DBMS Query evaluation

Background material for Chapter 3 taken from CS 245

Relational Database: The Relational Data Model; Operations on Database Relations

Query Processing SL03

Query Processing & Optimization

Ch 5 : Query Processing & Optimization

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

Algebraic laws extensions to relational algebra

Analysis of Query Processing and Optimization

Query Optimization. Shuigeng Zhou. December 9, 2009 School of Computer Science Fudan University

Chapter 12: Query Processing

1 Algebraic Query Optimization

Relational Model, Relational Algebra, and SQL

Notes. Some of these slides are based on a slide set provided by Ulf Leser. CS 640 Query Processing Winter / 30. Notes

CS122 Lecture 4 Winter Term,

Chapter 14: Query Optimization

Introduction Alternative ways of evaluating a given query using

Advanced Databases. Lecture 4 - Query Optimization. Masood Niazi Torshiz Islamic Azad university- Mashhad Branch

Query Optimization. Query Optimization. Optimization considerations. Example. Interaction of algorithm choice and tree arrangement.

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

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

Query Processing and Optimization *

Database Management Systems Introduction to DBMS

Overview of Query Processing and Optimization

Optimization Overview

Query Processing & Optimization. CS 377: Database Systems

Relational Algebra and SQL

Chapter 13: Query Optimization. Chapter 13: Query Optimization

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

Relational Databases

Introduction to Database Systems CSE 444

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Query optimization. Elena Baralis, Silvia Chiusano Politecnico di Torino. DBMS Architecture D B M G. Database Management Systems. Pag.

Ian Kenny. November 28, 2017

Database Applications (15-415)

Database Systems Architecture. Stijn Vansummeren

CMP-3440 Database Systems

Plan for today. Query Processing/Optimization. Parsing. A query s trip through the DBMS. Validation. Logical plan

CS 377 Database Systems

Chapter 11: Query Optimization

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

Arbori Starter Manual Eugene Perkov

Relational Query Optimization. Highlights of System R Optimizer

1. Data Definition Language.

Query Evaluation and Optimization

Information Systems for Engineers. Exercise 10. ETH Zurich, Fall Semester Hand-out Due

Compiler Design Overview. Compiler Design 1

Principles of Data Management. Lecture #12 (Query Optimization I)

ScalableTrigger Processing

QUERY OPTIMIZATION. CS 564- Spring ACKs: Jeff Naughton, Jignesh Patel, AnHai Doan

Ian Kenny. December 1, 2017

Chapter 2: Intro to Relational Model

Query Decomposition and Data Localization

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

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Relational Query Optimization. Overview of Query Evaluation. SQL Refresher. Yanlei Diao UMass Amherst October 23 & 25, 2007

QUERY OPTIMIZATION E Jayant Haritsa Computer Science and Automation Indian Institute of Science. JAN 2014 Slide 1 QUERY OPTIMIZATION

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Relational Model and Relational Algebra A Short Review Class Notes - CS582-01

Chapter 13: Query Optimization

2.2.2.Relational Database concept

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Midterm 1 157A Fall /22

Database Modifications and Transactions

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

Query Optimization Overview

Database Tuning and Physical Design: Basics of Query Execution

Chapter 3B Objectives. Relational Set Operators. Relational Set Operators. Relational Algebra Operations

Relational Model and Relational Algebra

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

SQL Subqueries. T. M. Murali. September 2, T. M. Murali September 2, 2009 CS 4604: SQL Subqueries

DBMS Y3/S5. 1. OVERVIEW The steps involved in processing a query are: 1. Parsing and translation. 2. Optimization. 3. Evaluation.

Review. Relational Query Optimization. Query Optimization Overview (cont) Query Optimization Overview. Cost-based Query Sub-System

Module 4. Implementation of XQuery. Part 0: Background on relational query processing

Query Processing and Query Optimization. Prof Monika Shah

R & G Chapter 13. Implementation of single Relational Operations Choices depend on indexes, memory, stats, Joins Blocked nested loops:

Relational Query Optimization

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Optimizing Finite Automata

Chapter 3. Algorithms for Query Processing and Optimization

A simple syntax-directed

Transcription:

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

Outline Parse Trees and Grammars Algebraic Laws for Improving Query Plans From Parse Trees To Logical Query Plans

Syntax Analysis and Parse Trees The job of the parser is to take SQL and convert it to a parse tree. Nodes in this tree can be of the following types: Atoms -- keywords (like SELECT), names of attributes, constants, parentheses, operators. These are leaves in the tree Syntactic Categories - names for families of query subpart. For example, <Condition> might represent a possible condition part in the query.

A Grammar for a Simple Subset of SQL Here are some example rules a parser might use for SQL: <Query> ::= <SFW> <Query> ::= (<Query>) // ::= should be read as can be expressed as <SWF> ::= SELECT <SelList> FROM <FromList> WHERE <Condition> <SelList> ::= <Attribute>, <SelList> <SelList> ::= <Attribute> <FromList> ::= <Relation>, <FromList> <FromList> ::= <Relation> <Condition> ::= <Condition> AND <Condition> <Condition> ::= <Tuple> IN <Query> <Condition> ::= <Attribute> = <Attribute> <Condition> ::= <Attribute> LIKE <Pattern> //etc.

Example SELECT name from MovieStar WHERE birthdate LIKE %1960 Might yield parse tree: <Query> <SWF> SELECT <SelList> FROM <FromList> WHERE <Condition> <Attribute> <RelName> <Attribute>LIKE<Pattern> name MovieStar birthdate %1960

The Preprocessor The preprocessor does semantic checking: It expands views to their corresponding query It checks that the items in the FROM clause are actual tables in the DB It checks that the attributes in the SELECT clause belong to some table and matches them to the correct table. It checks that attributes in the WHERE clause are being put in conditions that are appropriate for their type.

Algebraic Laws For Improving Query Plans We are now going to consider different ways we can write the same query. The hope being some ways of writing are faster to process than others. For instance, it is probably faster to compute selects before taking a join, then vice versa. The optimizer s job will be to consider several plans and usually heuristically choose the best one.

Commutative and Associative Laws R x S = S x R; (R x S) x T = R x (S x T) R join S = S join R; (R join S) join T =R join (S join T) R S = S T; (R S) T = R (S T) R S = R S; (R S0 T = R (S T) Notice the intermediate tables in computing a sequence of join can be considerable smaller depending on the order of execution.

Laws Involving Selection σ C1 AND C2 (R) = σ C1 (σ C2 (R)) = σ C2 (σ C1 (R)) σ C1 OR C2 (R) = σ C1 (R) σ C2 (R) provided R is a set. σ C (R S) = σ C (R) σ C (S) σ C (R - S) =σ C (R) - S Similar rules hold for joins and cartesian products.

Pushing Selections To reduce the cost of computing joins and products as much as possible, a good heuristic is to try to push selects as far down all branches in the query tree as possible For instance, σ year=1996 (Movie join StarsIn) should be rewritten σ year=1996 (Movie) join σ year=1996 (StarsIn).

Laws Involving Projection π L (R join S) = π L (π M (R) join π N (S)) where M is those attributes in L that are from R; N is those attributes in L that are from S. This also works for joins with conditions on them. As with selections, it is often useful to try to push projections as far down the tree as possible.

Laws for Joins and Products We have already seen that it is possible to rewrite a join using: R join Condition S = σ Condition (R x S) A natural join can be written as R join Condition S = π L (σ C (R x S)) where L is the attributes from the natural join and C is the condition which matches like named attributed.

From Parse Trees to Logical Query Plans After calculating the parse tree for a query We convert the appropriate groups in the tree by relational operators. For example, we replace the <FromList> node in a <Query> tree with the cartesian product of the tables listed under it. Then try to optimize this relational algebra tree to create a physical query plan. Might apply heuristics like pushing selection and projections which we mentioned earlier.