Query Tuning. Types of Nested Queries Uncorrelated subqueries with aggregates in the nested query SELECT ssnum FROM employee WHERE salary > (select avg(salary)

Slides:



Advertisements
Similar presentations
Database Tuning. Overview v After ER design, schema refinement, and the definition of views, we have the conceptual and external schemas for our database.
Advertisements

Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
AOBD07/08H. Galhardas Query Tuning. H. Galhardas AOBD07/08 Query Tuning SELECT s.RESTAURANT_NAME, t.TABLE_SEATING, to_char(t.DATE_TIME,'Dy, Mon FMDD')
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
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.
DB performance tuning using indexes Section 8.5 and Chapters 20 (Raghu)
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
1 Physical Database Design and Tuning Module 5, Lecture 3.
Relational Query Optimization (this time we really mean it)
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization.
H.Lu/HKUST L05: Query Processing & Tuning Queries  Query Processing -- Principles  Tuning Queries.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Chapter 19 Query Processing and Optimization
Practical Database Design and Tuning. Outline  Practical Database Design and Tuning Physical Database Design in Relational Databases An Overview of Database.
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Nested Queries (Sub-Queries). Objectives Learn how to run a query as a nested sub-query Condition on nested query Application of nested query Restriction.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Relational Operator Evaluation. Overview Index Nested Loops Join If there is an index on the join column of one relation (say S), can make it the inner.
H. Pang / NUS Principles of Query Processing Pang Hwee Hwa School of Computing, NUS CS5226 Week 5.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Physical Database Design and Tuning Chapter 20.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Management COP4540, SCS, FIU Physical Database Design (2) (ch. 16 & ch. 6)
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Principles of Query Processing. Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g., SAP admin)
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Query Processing and Query Optimization CS 157B Dennis Le Weishan Wang.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
1 Chapter 4. Tuning a Relational Database System May 2002 Prof. Sang Ho Lee School of Computing, Soongsil University
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Query Tuning. overview Use of Views CREATE VIEW techlocation AS SELECT ssnum, tech.dept, location FROM employee, tech WHERE employee.dept = tech.dept;
1 CS122A: Introduction to Data Management Lecture #15: Physical DB Design Instructor: Chen Li.
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Using Subqueries to Solve Queries
Tuning Transact-SQL Queries
MySQL Subquery Source: Dev.MySql.com
6/22/2018.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Tuning Queries from (E&N)
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lecture 5- Query Optimization (continued)
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Query Tuning.
Presentation transcript:

Query Tuning

Types of Nested Queries Uncorrelated subqueries with aggregates in the nested query SELECT ssnum FROM employee WHERE salary > (select avg(salary) from employee) Uncorrelated subqueries without aggregate in the nested query SELECT ssnum FROM employee WHERE dept in (select dept from tech) Correlated subqueries with aggregates SELECT ssnum FROM employee e1 WHERE salary = (SELECT avg(e2.salary) FROM employee e2, tech WHERE e2.dept = e1.dept AND e2.dept = tech.dept) Correlated subqueries without aggregates (unusual)

Rewriting of Uncorrelated Subqueries without Aggregates 1.Combine the arguments of the two FROM clauses 2.AND together the where cluases, repacing in by = 3.Retain the SELECT clause from the outer block SELECT ssnum FROM employee WHERE dept in (select dept from tech) becomes SELECT ssnum FROM employee, tech WHERE employee.dept = tech.dept

Rewriting of Uncorrelated Subqueries without Aggregates Potential problem with duplicates –SELECT avg(salary) FROM employee WHERE manager in (select manager from tech) –SELECT avg(salary) FROM employee, tech WHERE employee.manager = tech.manager The rewritten query may include an employee record several times if that employee’s manager manages several departments. The solution is to create a temporary table (using DISTINCT to eliminate duplicates).

Rewriting of Correlated Subqueries Query: find the employees of tech departments who earn exactly the average salary in their department SELECT ssnum FROM employee e1 WHERE salary = (SELECT avg(e2.salary FROM employee e2, tech WHERE e2.dept = e1.dept AND e2.dept = tech.dept);

Rewriting of Correlated Subqueries INSERT INTO temp SELECT avg(salary) as avsalary, employee.dept FROM employee, tech WHERE employee.dept = tech.dept GROUP BY employee.dept; SELECT ssnum FROM employee, temp WHERE salary = avsalary AND employee.dept = temp.dept

Rewriting of Correlated Subqueries Query: Find employees of technical departments whose number of friends equals the number of employees in their department where their department is a technique one. SELECT ssnum FROM employee e1 WHERE numfriends = COUNT(SELECT e2.ssnum FROM employee e2, tech WHERE e2.dept = tech.dept AND e2.dept = e1.dept);

Rewriting of Correlated Subqueries INSERT INTO temp SELECT COUNT(ssnum) as numcolleagues, employee.dept FROM employee, tech WHERE employee.dept = tech.dept GROUP BY employee.dept; SELECT ssnum FROM employee, temp WHERE numfriends = numcolleagues AND employee.dept = temp.dept; Can you spot the infamous COUNT bug?

The Infamous COUNT Bug Let us consider Helene who is not in a technical department. In the original query, helene’s number of friends would be compared to the count of an empty set which is 0. In case helene has no friends she would survive the selection. In the transformed query, helene’s record would not appear in the temporary table because she does not work for a technical department. This is a limitation of the correlated subquery rewriting technique when COUNT is involved.

Avoid Complicated Correlation Subqueries Search all of e2 for each e1 record! SELECT ssnum FROM Employee e1 WHERE salary = (SELECT MAX(salary) FROM Employee e2 WHERE e2.dept = e1.dept SELECT MAX(salary) as bigsalary, dept INTO Temp FROM Employee GROUP BY dept SELECT ssnum FROM Employee, Temp WHERE salary = bigsalary AND Employee.dept = Temp.dept

Avoid Complicated Correlation Subqueries SQL Server 2000 does a good job at handling the correlated subqueries (a hash join is used as opposed to a nested loop between query blocks) –The techniques implemented in SQL Server 2000 are described in “Orthogonal Optimization of Subqueries and Aggregates” by C.Galindo- Legaria and M.Joshi, SIGMOD > 10000> 1000

Change Nested Queries to Join Might not use index on Employee.dept Need DISTINCT if an employee might belong to multiple departments SELECT ssnum FROM Employee WHERE dept IN (SELECT dept FROM Techdept) SELECT ssnum FROM Employee, Techdept WHERE Employee.dept = Techdept.dept

Avoid Unnecessary Temp Tables Creating temp table causes update to catalog Cannot use any index on original table, e.g. index on dep SELECT * INTO Temp FROM Employee WHERE salary > SELECT ssnum FROM Temp WHERE Temp.dept = ‘information systems’ SELECT ssnum FROM Employee WHERE Employee.dept = ‘information systems’ AND salary > 40000

Join Conditions It is a good idea to express join conditions on clustering indexes. –No sorting for sort-merge. –Speed up for multipoint access using an indexed nested loop. It is a good idea to express join conditions on numerical attributes rather than on string attributes. SELECT Employee.ssnum FROM Employee, Student WHERE Employee.name = Student.name SELECT Employee.ssnum FROM Employee, Student WHERE Employee.ssnum = Student.ssnum

Join on Clustering and Integer Attributes Employee is clustered on ssnum ssnum is an integer SELECT Employee.ssnum FROM Employee, Student WHERE Employee.name = Student.name SELECT Employee.ssnum FROM Employee, Student WHERE Employee.ssnum = Student.ssnum

Avoid HAVING when WHERE is enough May first perform grouping for all departments! Having should be reserved for aggregate properties of the groups. –SELECT avg(salary) as avgsalary, dept FROM employee GROUP BY dept HAVING count(ssnum) > 100; SELECT AVG(salary) as avgsalary, dept FROM Employee GROUP BY dept HAVING dept = ‘information systems’ SELECT AVG(salary) as avgsalary FROM Employee WHERE dept = ‘information systems’ GROUP BY dept

summary