Académique Documents
Professionnel Documents
Culture Documents
This lecture demonstrates the techniques of optimizing SQL query command for efficiency performance. The optimizer will transform SQL query into an equivalent SQL query in the form of relational algebra with less cost. It shows how to apply heuristics rules in reducing the cost of query.
11-2-2009 1
Query Optimizer
We can interpret an expression of relational algebra not only as the specification of the semantics of a query, but also as the specification of sequence of operations. From this viewpoint, two expressions with the same semantics can describe two different sequences of operations. Given: Relation EMP(Empnum, Name, Sal, Tax, Mgrnum, Deptnum) Relation DEPT(Deptnum, Name, Area, Mgrnum) Relation SUPPLIER(Snum, Name, City) Relation SUPPLY(Snum, Pnum, Deptnum, Quan)
PJ NAME, DEPTNUM SL DEPTNUM=15 EMP = SLDEPTNAM=15 PJ NAME, DEPTNUM EMP
condition SL DEPTNUM PJ DEPTNUMEMP (projected data must be in selected data) Are equivalent expressions but define two different sequences of operations.
11-2-2009
11-2-2009
The operator tree of an expression of relational algebra can be regarded as the parse tree of the expression itself, assuming the following grammar: R -> identifier R -> (R) R -> un_op R R -> R bin_op R Un_op -> SLF | PJA Bin_op -> CP | UN | DF | JNF | NJNF | SJF | NSJF Two relations are equivalent when their tuples represent the same mapping from attribute names to values, 11-2-2009 4 even if the order of attributes is different.
Commutativity of unary operations: U1U2R <->U2U1R Commutativity of operands of binary operations R B S <-> S B R Associativity of unary operations: U R <-> U1 U2 R Distributivity of unary operations with respect to binary operations: U (R B S) -> U(R) B U(S) Factorization of unary operations (this tranforsmation is the inverse distributivity): U (R) B U(S) -> U (R B S)
11-2-2009 5
SLF1 PJA2 R
Attr(F1)A2
PJA2 SLF1 R
SLF R
11-2-2009
F=F1 F2
SLF1 SLF2 R
6
Distributivity of unary operations SLF (R UN S) (SLF R) UN (SLF S) SLF (R DF S) SLF (R SJF3 S) PJA (R CP S) (SLF R) DF (SLF S) (SLF R) SJF3 (SLFS S) (PJAR R) CP (PJAS S)
AR=A-Attr(S)=R.A AS=A-Attr(R)=S.A
11-2-2009
11-2-2009
11-2-2009
11-2-2009
10
11-2-2009
11
11-2-2009
12
11-2-2009
13
11-2-2009
14
11-2-2009
15
S.B 1 1 3 3
16
Qualified Relations
A qualified relation is a pair [R: qR] where R is a relation called the body of the qualified relation and qR is a predicate called the qualification of the qualified relation, for example horizontal fragments are qualified relations in which the qualification corresponds to the partitioning predicate.
As a result, [R: qR] is an unary operation such as selection (horizontal fragmentation) and/or projection (vertical fragmentation) to relation R. 11-2-2009 17
produces a relation un_op R as its body and the predicate qR2 as its qualifications. On the left hand side, we apply qualification qR1 followed by un_ary operation. On the right hand side, we apply Un_ary operation first, followed by qualification qR2.
Rule 1: SLF[R: qR] [SLFR: F AND qR] F holds all the tuples as well as qR
SLF SLF UN R SLF1 R1
21-2-2009
(horizontal fragmentation)
SLF UN
SLF2 SLF3
R2
R3 R1 R2 R3
18
(vertical fragmentation)
PJA
PJA NJN
PJA NJN
21-2-2009
19
Horizontal Fragmentation
Vertical Fragmentation
11-2-2009
20
Rule 3: [R: qR] CP [S: qS] [R CP S: qR AND qS] Two qualifications apply to disjoint attributes of R CP S:
CP UN R S R1 R2 S1 S2 CP
UN
UN
CP
11-2-2009
CP
CP
CP
21
R1
S1
R1
S2
R2
S1
R2
S2
DF UN R S R1
DF
UN
R2
S1
S2
UN
DF
DF
DF
DF
22
11-2-2009
R1
S1
R1
S2
R2
S1
R2
S2
UN UN R S R1
UN
UN
R2
S1
S2
UN
UN
UN
UN
UN
21-2-2009
R 1 R 1
S1
R1
S2
R2
S1
R2
S2
23
11-2-2009
25
PJ EMPNUM, PNUM
PJ EMPNUM, PNUM
JN DEPTNUM = DEPTNUM
PJ DEPTNUM, EMPNUM
SUPPY
EMP
EMP
Q2: give the names of employees who work in a department whose manager has number 373 but who do not earn more than $35,000 PJEMP.NAME((EMP JN DEPTNUM=DEPTNUM SL MGRNUM=373 DEPT) DF (SLSAL>35000 EMP JN DEPTNUM=DEPTNUM SL MGRNUM=373 DEPT))
11-2-2009 28
Operator tree
PJ EMP.NAME
DF
JN DEPTNUM = DEPTNUM
JN DEPTNUM = DEPTNUM
EMP
SL MGRNUM=373
SL SAL>35000
SL MGRNUM=373
11-2-2009
DEPT
EMP
DEPT
29
Idempotence
PJ EMP.NAME
DF
EMP
SL MGRNUM=373
EMP
SL MGRNUM=373
21-2-2009
DEPT
DEPT
30
Factorization
PJ EMP.NAME
DF SL SAL>35000
DEPT
Simplication
PJ EMP.NAME
SL SAL<35000
JN DEPTNUM = DEPTNUM
SL MGRNUM=373 EMP
11-2-2009
DEPT
32
JN DEPTNUM = DEPTNUM
PJ NAME.DEPTNUM
PJ DEPTNUM
SL SAL<35000
SL MGRNUM=373
11-2-2009
EMP DEPT
33
Decompose query Q3 by criterion 3(eliminate empty set) Q3: SL DEPTNUM=1 DEPT Given:
UN [DEPT1: DEPTNUM<10]
[DEPT2: 10<DEPTNUM<20] [DEPT1: DEPTNUM<10] [DEPT3: DEPTNUM>20]
11-2-2009
35
Decompose query Q4 by criterion 4(eliminate irrelevant joins) and 5(push union up in the tree) Q4: PJ SMUM (SUPPLY NJN SUPPLIER)
Decompose query Q4 by criterion 4(eliminate irrelevant joins) and 5(push union up in the tree) Q4: PJ SMUM (SUPPLY NJN SUPPLIER)
PJ SNUM
NJN
Suppy
11-2-2009
Supplier
37
Decompose query Q4 by criterion 4(eliminate irrelevant joins) and 5(push union up in the tree) Q4: PJ SMUM (SUPPLY NJN SUPPLIER) PJ SNUM
NJN
UN
[SUPPLY1: SNAM=SUPPLIER.SNU M AND SUPPLIER. CITY=SF]
UN
[SUPPLY2: SNAM=SUPPLIER.SNU M AND SUPPLIER. CITY=LA] [SUPPLIER1: CITY=SF]
11-2-2009
[SUPPLIER2: CITY=LA]
38
PJ SNUM
PJ SNUM
PJ SNUM
PJ SNUM
NJN
NJN
NJN
NJN
[SUPPLIER2: CITY=LA]
[SUPPLIER1: CITY=SF]
11-2-2009
39
PJ SNUM
PJ SNUM
NJN
NJN
[SUPPLIER1: CITY=SF]
[SUPPLIER2: CITY=LA]
40
Lecture summary
Many heuristics rules can be applied to reduce the cost of SQL query as follows: Idempotence of select and project. Push select and project down in the operator tree. Qualify relation first for eliminating contradictory relation(s). Evaluate join operations with qualified relation(s). Push Union operation up in the operator tree.
11-2-2009 41
Review Question 5 (1) Discuss the reasons for converting SQL queries into relational algebra queries before optimization is done. (2) Show an example of qualifying following Relation R (Key, Attribute 1, Attribute 2) by Horizontal fragmentation Vertical fragmentation Into relations R1 and R2. (3) Show how to reconstruct relations R1 and R2 back into relation R from (a) Horizontal fragmentation (b) Vertical fragmentation
11-2-2009 42
(iv) Given its qualified relations (fragments) as follows: Relation PATIENT1 = SL Dept=surgery and Treat=intensive PATIENT Relation PATIENT2 = SL Dept=surgery and Treatintensive PATIENT Relation PATIENT3 = SL Deptsurgery PATIENT Relation CARE1 = CARE SJ Pnum=Pnum PATIENT1 Relation CARE2 = CARE SJ Pnum=Pnum PATIENT2 Relation CARE3 = CARE SJ Pnum=Pnum PATIENT3
Translate the query in into fragment queries (20%) and simplify them for optimization. (20%)
11-2-2009 43
Reading Assignment
Chapter 15 Algorithm for Query Processing and Optimization of Fundamentals of Database Systems by Elmasri and Navathe, 5th edition, Pearson, 2007.
11-2-2009
44