Query Optimization Dr. Karen C. Davis Professor School of Electronic and Computing Systems School of Computing Sciences and Informatics.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Advertisements

The Relational Calculus
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Lecture 1 Relational Algebra and Relational Calculus.
1 CSE 480: Database Systems Lecture 22: Query Optimization Reference: Read Chapter 15.6 – 15.8 of the textbook.
The Relational Algebra
CS346: Advanced Databases
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Query processing and optimization. Advanced DatabasesQuery processing and optimization2 Definitions Query processing –translation of query into low-level.
CS 257, Spring’08 Presented By: Presented By: Gayatri Gopalakrishnan Gayatri Gopalakrishnan ID : 201.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
QUERY OPTIMIZATION AND QUERY PROCESSING.
Getting Started Writing a Thesis/Dissertation
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization.
Introduction to Query Processing and Query Optimization Techniques
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
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.
Algorithms for Query Processing and Optimization
Query Processing & Optimization
Chapter 19 Query Processing and Optimization
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Database Management 9. course. Execution of queries.
Department of Computer Science and Engineering, HKUST Slide Query Processing and Optimization Query Processing and Optimization.
Relational Algebra - Chapter (7th ed )
Query Optimization Chap. 19. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying where.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
Query Processing and Optimization
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
Algorithms for Query Processing and Optimization
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Lecture 11: Query processing and optimization Jose M. Peña
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
1 Chapter 18 Query Processing and Optimization Query Processing and Optimization Scanner: identify language components. keywords, attribute,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Advance Database Systems Query Optimization Ch 15 Department of Computer Science The University of Lahore.
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Introduction to Query Processing (1) Query optimization: The process of choosing a suitable execution.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Chapter 18 Query Processing and Optimization. Chapter Outline u Introduction. u Using Heuristics in Query Optimization –Query Trees and Query Graphs –Transformation.
Query Processing COMP3017 Advanced Databases Nicholas Gibbins
Copyright © 2011 Ramez Elmasri and Shamkant Navathe CPSC 8620: Database Management System Design Notes 7 Query Processing.
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.
Algorithms for Query Processing and Optimization
Query Processing and Optimization
Query Processing and Optimization, and Database Tuning
Database System Implementation CSE 507
6/22/2018.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
QUERY OPTIMIZATION.
1/3/2019.
Advance Database Systems
1. Explain the following concepts: (a) superkey (b) key
1. Explain the following concepts of the ER data model:
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Algorithms for Query Processing and Optimization
Presentation transcript:

Query Optimization Dr. Karen C. Davis Professor School of Electronic and Computing Systems School of Computing Sciences and Informatics

Outline overview of relational query optimization logical optimization –algebraic equivalences –transformation of trees physical optimization –selection algorithms –join algorithms cost-based optimization research example using relational algebra

Relational Query Optimization query optimizer logicalphysical SQL query relational algebra query tree access plan (executable)

Learning Outcomes translate basic SQL to RA query tree perform heuristic optimizations to tree use cost-based optimization to select algorithms for tree operators to generate an execution plan

SQL is declarative describes what data, not how to retrieve it select distinct … from … where … helpful for users, not necessarily good for efficient execution

Relational Algebra is procedural specifies operators and the order of evaluation steps for query evaluation: 1.translate SQL to RA operators (query tree) 2.perform heuristic optimizations: a.push RA select operators down the tree b.convert select and cross product to join c.others based on algebraic transformations

Relational Algebra Operators namesymbolicallyevaluation selectσcRσcR applies condition c to R project π l R keeps a list (l) of attributes of R cross product R X S all possible combinations of tuples of R are appended with tuples from S join R ⋈ c S π l (σ c (R X S)), where l is a list of attributes of R and S with duplicate columns removed and c is a join condition

SQL to RA select distinct …π l from …x where …σ c π l | σ c | X / \ R S π l | σ c | X / \ X S / \ R T π l | σ c | X / \ X S / \ X T / \ R U  two relations three relations  four relations ↓

SQL to RA Tree Example select A.x, A.y, B.z from A, B where A.a = B.z and A.x > 10 π A.x, A.y, B.z | σ A.a = B.z and A.z > 10 | X / \ A B evaluated bottom-up left to right; intermediate values are passed up the tree to the next operator

SQL to RA Tree Example select lname from employee, works_on, projects where pname = ‘Aquarius’ and pnumber = pno and essn = ssn and bdate = ‘ ’ π lname | σ pname = ‘Aquarius’ and pnumber = pno and essn = ssn and bdate = ‘ ’ | X / \ X projects / \ employee works_on

Simple Heuristic Optimization 1.cascade selects (split them up) π l | σ c 1 and c 2 and c 3 | X / \ R S π l | σ c 1 | σ c 2 | σ c 3 | X / \ R S

2.Push any single attribute selects down the tree to be just above their relation π l | σ c 1 | σ c 2 | σ c 3 | X / \ R S π l | σ c 2 | X / \ σ c 1 σ c 3 | R S

3.Convert 2-attribute select and cross product to join π l | σ c 2 | X / \ σ c 1 σ c 3 | R S π l | ⋈ c 2 / \ σ c 1 σ c 3 | R S smaller intermediate results efficient join algorithms

Practice problem: optimize RA tree select P.pnumber, P.dnum, E.lname, E.bdate from projects P, department D, employee E where D.dnumber = P.dnum and// c 1 D.mgrssn = E.ssn and// c 2 P.plocation = ‘Stafford’;// c 3

RA tree to RA expression π l | ⋈ c 2 / \ σ c 1 σ c 3 | R S σc1 Rσc1 R σc3 Sσc3 S ⋈c2⋈c2 πl(πl( )

Other Operators in Relational Algebra SQL: (select pnumber from projects, department, employee where dnum = dnumber and mgrssn = ssn and lname = 'Smith‘) union (select pnumber from projects, works_on, employee where pnumber = pno and essn = ssn and lname = 'Smith'); RA: π pnumber (σ lname = ‘Smith’ employee ⋈ ssn=mgrssn department ⋈ dnumber = dnum projects) ⋃ π pnumber (σ lname = ‘Smith’ employee ⋈ ssn=essn works_on ⋈ pnumber = pno projects)

Selection Algorithms linear search binary search primary index or hash for point query primary index for range query clustering index secondary index conjunctives –individual index –composite index or hash –intersection of record pointers for multiple indexes

Join Algorithms nested loop join single-scan join sort-merge join hash join sort-merge using indexes example execution plan

Multiple View Processing Plan (MVPP)  view chromosome:  index chromosome:  Fitness: sum of query processing costs of individual queries using the views and indexes selected ⋈ orderkey (v7) Customer (C)Orders (O)Lineitem (L)Nation (N)Part (P) Q1 Q2Q3 π O.orderkey, O.shippriority (v9) π C.custkey, C.name, C.acctbal, N.name, C.address, C.phone (v12) π P.type, L.extendedprice (v15) σ C.mktsegment = “building” and L.shipdate = “ ” (v8) σ O.orderdate = “ ” (v11) σ L.shipdate = “ ” (v14) ⋈ nationkey (v10) ⋈ custkey (v6) ⋈ partkey (v13) π name, address, phone, acctbal, nationkey, custkey, mktsegment (v1) π orderkey, orderdate, custkey, shippriority (v2) π partkey, orderkey, shipdate, extendedprice (v3) π nationkey, name (v4) π partkey, type (v5) thesis defense of Sirisha Machiraju: Space Allocation for Materialized Views and Indexes Using Genetic Algorithms, June 2002