Mod 7 - Query Optimization
Mod 7 - Query Optimization
Dr. Balasundaram A
VIT-Chennai
SWE1004 Syllabus
Module-7
Text Books
R. Elmasri & S. B. Navathe, Fundamentals of Database Systems, Addison Wesley, 7 th
Edition, 2015
Raghu Ramakrishnan,Database Management Systems,Mcgraw-Hill,4 th edition,2015
References
A. Silberschatz, H. F. Korth & S. Sudershan, Database System Concepts, McGraw Hill, 6
th Edition 2010
Thomas Connolly, Carolyn Begg, Database Systems : A Practical Approach to Design,
Implementation and Management,6 th Edition,2012
Pramod J. Sadalage and Marin Fowler, NoSQL Distilled: A brief guide to merging world of
Polyglot persistence, Addison Wesley, 2012.
Shashank Tiwari, Professional NoSql, Wiley, 2011.
Nested queries : are within a query are identified as separate query blocks.
Finally, The query optimizer will choose an execution plan for each query block.
The main heuristic is to apply first the operations that reduce the size of
intermediate results.
For Eg :
– For every project located in ‘Stafford’, retrieve the project number, department
number, and department manager’s last name, address, and birth-date.
SQL Query :
SELECT P.NUMBER,P.DNUM,E.LNAME, E.ADDRESS, E.BDATE
FROM PROJECT AS P, DEPARTMENT AS D, EMPLOYEE AS E
WHERE P.DNUM=D.DNUMBER AND D.MGRSSN=E.SSN AND
P.PLOCATION=‘STAFFORD’;
Relation Algebra :
πPnumber ,Dnum,Lname,Address,Bdate (((σPlocation=‘Stafford 0 (PROJECT))
./Dnum=Dnumber (DEPARTMET)) ./Mgrs sn=Ssn (EMPLOYEE))
Query Tree -1
Query Tree -3
Query Tree -3
Example:
SELECT LNAME
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE PNAME = ‘AQUARIUS’ AND PNUMBER=PNO AND
ESSN=SSN AND BDATE > ‘1957-12-31’;
Query Tree-1
An overall rule for heuristic query optimization is to perform as many select and
project operations as possible before doing any joins.
1 Cascade of σ : A conjunctive selection condition can be broken up into a
cascade (that is, a sequence) of individual σ operations:
σ c1 ,c2 ,c3 ....,cn (R) = σ c1 (σ c2 (σ c3 ....., (σ cn (R))))
2 Commutative of (σ) : The ’σ’ operation is commutative:
σ c1 (σ c2 (R)) = σ c2 (σ c1 (R))
3 Cascade of π : In a cascade (sequence) of π operations, all but the last one
can be ignored:
π List1 (π List2 (...., π Listn (R) ) ) = π List1 (R)
4 Commuting σ with π : If the selection condition ’c’ involves only those
attributes A1 , ... , An in the projection list, the two operations can be
commuted:
π A1 ,A2 ,A3 ...,An (σ c (R)) = σ c (π A1 ,A2 ,A3 ...,An ((R)))
The main heuristic is to apply first the operations that reduce the size of
intermediate results.
The select and join operations that are most restrictive should be executed
before other similar operations. (This is done by reordering the leaf nodes of
the tree among themselves and adjusting the rest of the tree appropriately.)