Query Optimiation
Query Optimiation
Query Optimiation
There are at least three alternative ways of representing this query as a Relational Algebra expression.
(job = Manager) (name=Sales) (emp.deptno = dept.deptno) (EMP X DEPT) (job = Manager) (name=Sales) (EMP ((job = Manager) (EMP))
emp.deptno = dept.deptno
DEPT)
emp.deptno = dept.deptno
((name=Sales) (DEPT))
Unit_IV_Class_Lectures(Prof.B.saleena)
Selection on result of Cartesian product: (1000 * 50) record I/Os to read tuples and compare against predicate
Total cost of the query: (1000 + 50) + 2*(1000 * 50) = 101, 050 record I/Os. Unit_IV_Class_Lectures(Prof.B.saleena)
emp.deptno = dept.deptno
DEPT)
Join of EMP and DEPT over deptno: (1000 + 50) record I/Os to read the relations + (1000) record I/Os to create an intermediate relation to store join result
Selection on result of Join: (1000) record I/Os to read each tuple and compare against predicate
Total cost of the query: (1000 + 50) + 2*(1000) = 3, 050 record I/Os. Unit_IV_Class_Lectures(Prof.B.saleena)
emp.deptno = dept.deptno
((name=Sales) (DEPT))
Select Managers in EMP: (1000) record I/Os to read the relations + (50) record I/Os to create an intermediate relation to store select result
Select Sales in DEPT: (50) record I/Os to read the relations + (5) record I/Os to create an intermediate relation to store select result Join of previous two selections over deptno: (50 + 5) record I/Os to read the relations Total cost of the query: (1000 2*(50) + 5 +(50 +5)) = 1, 160 record I/Os.
Unit_IV_Class_Lectures(Prof.B.saleena)
This involves the conversion of the original (SQL) query into some internal representation more suitable for machine manipulation. The internal representation typically chosen is either some kind of abstract syntax tree, or a relational algebra query tree.
Unit_IV_Class_Lectures(Prof.B.saleena)
(job = Manager) (name=Sales) (emp.deptno = dept.deptno) (EMP X DEPT) (job = Manager) (name=Sales) (emp.deptno = dept.deptno)
Root Intermediate operations
X EMP DEPT
Unit_IV_Class_Lectures(Prof.B.saleena)
Leaves
(job = Manager) (name=Sales) (emp.deptno = dept.deptno) (EMP X DEPT) (job = Manager) (name=Sales)
(emp.deptno = dept.deptno)
X EMP DEPT
Unit_IV_Class_Lectures(Prof.B.saleena)
emp.deptno = dept.deptno
DEPT)
emp.deptno = dept.deptno
EMP
DEPT
Unit_IV_Class_Lectures(Prof.B.saleena)
emp.deptno = dept.deptno
((name=Sales) (DEPT))
emp.deptno = dept.deptno
(job = Manager)
(name=Sales)
EMP
DEPT
Unit_IV_Class_Lectures(Prof.B.saleena)
Find a more efficient representation of the query by converting the internal representation into some equivalent (canonical) form through the application of a set of well-defined transformation rules. The set of transformation rules to apply will generally be the result of the application of specific heuristic processing strategies associated with particular DBMSs.
Unit_IV_Class_Lectures(Prof.B.saleena)
Example:
deptno=10 sal>1000(Emp) = deptno=10(sal>1000(Emp))
Unit_IV_Class_Lectures(Prof.B.saleena)
Example:
sal>1000(deptno=10(Emp)) = deptno=10(sal>1000(Emp))
Unit_IV_Class_Lectures(Prof.B.saleena)
Example:
PdeptnoPname(Dept) = Pdeptno (Dept))
Unit_IV_Class_Lectures(Prof.B.saleena)
Example:
Unit_IV_Class_Lectures(Prof.B.saleena)
pS
=S
pR
R X S = S XR Example:
EMP
emp.deptno = dept.deptno
DEPT EMP
= DEPT
emp.deptno = dept.deptno
Unit_IV_Class_Lectures(Prof.B.saleena)
= p( R
r S)
Example:
(emp.deptno=10 (EMP))
emp.deptno = dept.deptno
DEPT DEPT)
= emp.deptno=10 (EMP
emp.deptno = dept.deptno
Unit_IV_Class_Lectures(Prof.B.saleena)
= (PL1(R))
(PL2(S))
Project attributes L = L1 L2, where L1 are attributes of R, and L2 are attributes of S. L will also contain the join attributes
Example:
P job, location, deptno (EMP = (P job, deptno (EMP))
emp.deptno = dept.deptno
DEPT)
Unit_IV_Class_Lectures(Prof.B.saleena)
Unit_IV_Class_Lectures(Prof.B.saleena)
Unit_IV_Class_Lectures(Prof.B.saleena)
Cartesian Product
(R X S) X T = R X (S X T)
Unit_IV_Class_Lectures(Prof.B.saleena)
Unit_IV_Class_Lectures(Prof.B.saleena)
leaf node
:relations
Internal node :relational algebra operations Execution of query trees: post order traversal of tree Using Heuristics in Query Optimization Apply SELECT and PROJECT before applying JOIN or other binary operations.
Unit_IV_Class_Lectures(Prof.B.saleena)
emp.deptno = dept.deptno
DEPT
emp.deptno = dept.deptno
(job = Manager )
(name=Sales)
EMP
DEPT
Unit_IV_Class_Lectures(Prof.B.saleena)
Consider the (optimised canonical) query as a series of low-level operations (join, restrict, etc). For each of these operations generate alternative execution strategies and calculate the cost of such strategies on the basis of statistical information held about the database tables (files).
Unit_IV_Class_Lectures(Prof.B.saleena)
Construct a set of candidate Query Execution Plans (QEPs). Each QEP is constructed by selecting a candidate implementation procedure for each operation in the canonical query and then combining them to form a string of associated operations. Each QEP will have an (estimated) cost associated with it the sum of the cost of each of its operations.
Unit_IV_Class_Lectures(Prof.B.saleena)
A good declarative query optimiser does not rely solely on heuristic processing strategies. It chooses the QEP with the lowest estimated cost. After heuristic rules are applied to a query, there still remains a number of alternative ways to execute it . The Query Optimiser estimates the cost of executing each one (or at least a number) of these alternatives, and selects the cheapest one.
Unit_IV_Class_Lectures(Prof.B.saleena)
Storage costs
Cost of storing intermediate (temp) files
Computation costs
Cost of CPU usage
Communication costs
Cost of moving data across
Unit_IV_Class_Lectures(Prof.B.saleena)
number of blocks blocking factor primary access method primary access attributes secondary indexes secondary indexing attributes number of levels for each index number of distinct values of each attribute
Unit_IV_Class_Lectures(Prof.B.saleena)
Example:
Q: SELECT LNAME FROM EMPLOYEE, WORKS_ON, PROJECT WHERE PNAME = AQUARIUS AND PNMUBER=PNO AND ESSN=SSN AND BDATE > 1957-12-31;
Unit_IV_Class_Lectures(Prof.B.saleena)
Query: Find the last names of Employees born after 1957 who work on the Project named Aquarius SQL Query:
SELECT FROM WHERE LNAME EMPLOYEE, WORKS_ON, PROJECT PNAME = AQUARIUS AND PNMUBER=PNO AND ESSN=SSN AND BDATE > 1957-12-31;
Unit_IV_Class_Lectures(Prof.B.saleena)
SELECT LNAME FROM EMPLOYEE, WORKS_ON, PROJECT WHERE PNAME=Aquarius AND PNUMBER=PNO AND ESSN=SSN AND BDATE > DEC-31-1957
Unit_IV_Class_Lectures(Prof.B.saleena)
Unit_IV_Class_Lectures(Prof.B.saleena)
SELECT LNAME FROM EMPOYEE, WORKS_ON, PROJECT WHERE PNAME=Aquarius AND PUMBER=PNO AND ESSN=SSN AND BDATE > DEC-31-1987
Unit_IV_Class_Lectures(Prof.B.saleena)
Unit_IV_Class_Lectures(Prof.B.saleena)