Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Query Tuning. Types of Nested Queries Uncorrelated subqueries with aggregates in the nested query SELECT ssnum FROM employee WHERE salary > (select avg(salary)"— Presentation transcript:

1 Query Tuning

2 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)

3 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

4 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).

5 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);

6 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

7 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);

8 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?

9 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.

10 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

11 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 2001. > 10000> 1000

12 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

13 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 > 40000 SELECT ssnum FROM Temp WHERE Temp.dept = ‘information systems’ SELECT ssnum FROM Employee WHERE Employee.dept = ‘information systems’ AND salary > 40000

14 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

15 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

16 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

17 summary


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

Similar presentations


Ads by Google