Docsity

Query Processing and Query Optimization - Database System Implement | CS 4420, Study notes of Computer Science

Partial preview of the text

Download Query Processing and Query Optimization - Database System Implement | CS 4420 and more Study notes Computer Science in PDF only on Docsity!

1

CS 4420 Database System Implementation

Query Processing

and Query Optimization

Ling Liu Associate Professor College of Computing, Georgia Tech

Lecture Outline

Query Processing Phases

„ Relational algebra level (last lecture)

z Algebraic transformations z good transformations

„ Detailed query plan level (today’s lecture)

z estimate costs (intermediate results size, I/O blocks) z generate and compare plans

3

Query Processing Methodology

High-level Calculus-based Query

Query Preprocessing

Query Optimization

Algebraic Query (a tree structure) LOGICAL SCHEMA

INTERNAL SCHEMA Execution Schedule (file access plan)

EXTERNAL SCHEMA

Relational Query Optimization

„ Relational algebra level z Using Heuristics for good transformations to reduce the search space of local query plans z Algebraic Rewriting z Algebraic Transformation Æ Typical Heuristics ) Push Selection and Projection closer to the base relations in a SQL query tree. ) Reduce intermediate result size as much as possible

„ Physical (Executable) query plan level z Consider various indexes and their performance characteristics when estimating costs of alternative query plans z Pick the most economical physical query plan for query execution

7

Algebraic Transformation

πENAME

σW.RESP = "Manager“

E.ENO = W.ENO

E

Moving selection downward (using Cascade of selections, Commutativity of selections, and Commuting selection with binary operations)

W

How to decide which is the best plan?

πENAME

σW.RESP = "Manager“

E.ENO = W.ENO

E

W

πENAME

σW.RESP = "Manager“

E.ENO = W.ENO

E

W

Plan 1 Do early selection Plan 2

9

How to decide which is the best plan?

πENAME

σW.RESP = "Manager“

E.ENO = W.ENO

E

W

πENAME

σW.RESP = "Manager“

E.ENO = W.ENO

E

W

Plan 3 Plan 4

πENAME, ENO πENO πENO πENAME, ENO

Do early selection and early projection

Identify subtrees that can be implemented in one algorithm

Heuristics Query Optimization: Summary

„ First apply operations that reduce the size of

intermediate results.

z Move selections and projections down the tree as far as possible Æ early selections reduce the number of tuples Æ early projections reduce the number of attributes z The selection and join operations are the most restrictive, thus, they 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.

13

Algebraic Transformation Q/A

„ State if the following transformation is correct or not and explain why z σp 1 and p 2 and p3 (R) ≡ σp 1 (σp2and p3 (R)) ≡ σp1and p 2 σ (^) p3(R))

z πA1 and A2 (R) ≡ πA1 (πA2(R)) ≡ πA2(πA1 (R)) ≡ πA1(R)

z σp 1 (R) ∪ σp 2 (R) ≡ σp 1 or p 2 (R )

z π (^) A1,A2 (σp(R)) ≡ σp(πA1,A2 (R)) ≡ σp (πA1(R) )

Q/A

z σp(A)(R × S) ≡ (σp(A)(R)) × S

z σp(Ai)(R (^) q(Ai,Bk) S) ≡ (σp(Ai)(R)) (^) q(Ai,Bk) S

z σp(Ai)(R ∪ T) ≡ σp(Ai)(R) ∪ σp(Ai)(T)

z σp(Ai)(R ∩ T) ≡ σp(Ai)(R) ∩ σp(Ai)(T)

z πC (R × S) ≡ πA(R) × πB (S) z πC (R ∪ S) ≡ πA (R) ∪ πB(S)

C = A' ∪ B' where A' ⊆ R, B’ ⊆ S

Ai is defined on both R and T

A is defined on R only

Ai is defined on R and Bk is defined on S

15

Rules: π,σ combined

Let x = subset of R attributes

z = attributes in predicate P

(subset of R attributes)

πx [σp ( R) ] = πx{σp [ πx ( R) ] }

πxz

Today’s lecture

Query Processing Phases

„ Relational algebra level (last lecture)

„ Detailed query plan level (today’s lecture)

z estimate costs Æ intermediate results size Æ I/O blocks z generate and compare plans

General Tactics ƒ Reducing the intermediate result size as much as possible ƒ Reducing I/O cost as much as possible