• 검색 결과가 없습니다.

CHAPTER 14

N/A
N/A
Protected

Academic year: 2022

Share "CHAPTER 14"

Copied!
23
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

CHAPTER 14

Advanced DB

CHAPTER 14

QUERY OPTIMIZATION

QUERY OPTIMIZATION

(2)

Chapter 14: Query Optimization p Q y p

ƒ Overview

ƒ Estimating Statistics of Expression Results

ƒ Transformation of Relational Expressions

ƒ Transformation of Relational Expressions

ƒ Choice of Evaluation Plans

ƒ Materialized Views

(3)

Introduction

ƒ Generation of query-evaluation plans for an expression involves several steps:

1. Generating logically equivalent expressions

‚ Use equivalence rules to transform an expression into an equivalent one.

2 A t ti r lti pr i t t lt r ti r pl

2. Annotating resulting expressions to get alternative query plans

3. Choosing the cheapest plan based on estimated cost

ƒ The overall process is called cost based optimizationThe overall process is called cost based optimization.

(4)

Query Optimization

ƒ Equivalence of Expressions

Q y p

q p

Given a DB schema S, a query Q on S is equivalent to another query Qon S, if the answer sets of Q and Qare the same in any instances of the DB.

Πb_name, assetc_city=“PC”(customer depositor branch)) vs

Π (( ( )) d i b h)

Πb_name, asset((σc_city=“PC”(customer)) depositor branch)

ƒ Query optimization is the process of selecting the most efficient query

evaluation plan for a given query

(5)

Evaluation Plan

ƒ An evaluation plan defines exactly what algorithm is used for p y g

each operation, and how the execution of the operations is

coordinated.

(6)

Equivalence Rules q

1. Conjunctive selection operations can be deconstructed into a sequence of individual selections.

σθ1∧θ2(E) = σθ1θ2(E)

2. Selection operations are commutative.

σ (σ (E) = σ (σ (E) σθ1θ2(E) = σθ2θ1(E)

3. Only the last in a sequence of projection operations is needed, the others can 3. Only the last in a sequence of projection operations is needed, the others can

be omitted.

Πt1t2( … Πtn(E ) … )) = Πt1(E )

4. Selections can be combined with Cartesian products and theta joins.

(E X E ) E E

a. σθ(E1X E2) = E1 θ E2

b. σθ1(E1 θ2 E2) = E1 θ1∧ θ2E2

(7)

Equivalence Rules (cont.) q ( )

5. Theta-join operations (and natural joins) are commutative.j p ( j ) E1 θ E2 = E2 θ E1

6 (a) Natural join operations are associative:

6. (a) Natural join operations are associative:

(E1 E2) E3 = E1 (E2 E3)

(b) Theta joins are associative in the following manner:

(E1 θ1 E2) θ2∧θ3 E3 = E1 θ2∧θ3 (E2 θ2 E3) where θ2 involves attributes from only E2 and E3.

7. Selection operation distributes over theta join when all the attributes in θ0 involve only the attributes of one of the expressions (E1) being joined.

σθ0(E1 θ E2) = (σθ0(E1)) θ E2

(8)

Equivalence Rules (cont.) q ( )

(9)

Transformation – Example 1 p

ƒ Query: Find the names of all customers who have an account at Q y some branch located in Brooklyn.

Π

customer-name

branch-city = y Brooklyn”y

(branch (account depositor)))

ƒ Transformation using rule 7a. g Π

customer-name

((σ

branch-city =Brooklyn”

(branch)) ((

branch city Brooklyn

( ))

(account depositor))

ƒ Performing the selection as early as possible reduces the size of Performing the selection as early as possible reduces the size of

the relation to be joined.

(10)

Transformation – Example 2

ƒ Query: Find the names of all customers with an account at a Brooklyn

p

branch whose account balance is over $1000.

Πcustomer-name((

branch-city = y Brooklyn” ∧ balance > 1000y

(branch (account depositor)))

ƒ Using join associativity (Rule 6a):g j y ( )

Πcustomer-name

((σ

branch-city = Brooklyn” ∧ balance > 1000

(branch (account)) depositor) (branch (account)) depositor)

ƒ Push selection in (Rules 1 & 7):

Π (

Πcustomer-name(

branch-city = Brooklyn”(branch) σbalance > 1000 (account))

d p it ) depositor)

(11)

Transformation – Example 2 (cont.) p ( )

(12)

Join Ordering

ƒ (r

1

r

2

) r

3

= r

1

(r

2

r

3

) (Rule 6a)

J g

(

1 2

)

3 1

(

2 3

) ( )

ƒ Choose the expression that will yield smaller temporary result

ƒ Example

ƒ Example

Π

customer-name

((σ

branch-city = “Brooklyn”

(branch)) account depositor)

à

account depositor

à

σ

branch-city = “Brooklyn”branch city Brooklyn

(branch) account ( )

(13)

Cost Estimation

ƒ Cost of each operator computer as described in Chapter 13 p p p

à Need statistics of input relations

à E.g. number of tuples, sizes of tuples

ƒ Inputs can be results of sub-expressions

à Need to estimate size of expression resultsp

à To do so, we require additional statistics

‚ E.g. number of distinct values for an attribute

(14)

Statistics for Cost Estimation

ƒ n

rr

: number of tuples in a relation r p

ƒ b

r

: number of blocks containing tuples of r

ƒ s : size of a tuple of r

ƒ s

r

: size of a tuple of r

ƒ f

r

: blocking factor of r

i.e., the number of tuples of r that fit into one block

ƒ V(A, r): number of distinct values that appear in r for attribute A; ( ) pp same as the size of ∏

A

(r)

ƒ SC(A, r): selection cardinality of attribute A of relation r; average ( , ) y b ; v g number of records that satisfy equality on A

⎡ ⎤

ƒ b

r

= ⎡n

r

/f

r

⎤ if tuples of r are stored together physically in a file

(15)

Catalog Information about Indices g

ƒ f f

ii

: average fan-out of internal nodes of index i, g for tree-structured indices such as B+-trees

ƒ HT : number of levels in index i — i e the height of i

ƒ HT

i

: number of levels in index i i.e., the height of i

à For a balanced tree index (such as B+-tree) on attribute A of relation r, HTi = ⎡ logfi (V(A,r))⎤.

HTi ⎡ logfi (V(A,r))⎤.

à For a hash index, HTi is 1.

à LBii: number of lowest-level index blocks in i — i.e, the number of blocks , at the leaf level of the index.

(16)

Measures of Query Cost Q y

ƒ Recall that

à Typically disk access is the predominant cost, and is also relatively easy to estimate.

à The number of block transfers from/to disk is used as a measure of the actual cost of evaluation.

I i d h ll f f bl k h h

à It is assumed that all transfers of blocks have the same cost

ƒ We do not include cost of writing output to disk

ƒ We refer to the cost estimate of algorithm A as E

A

(17)

Selection Size Estimation

ƒ Equality selection σ q y

A=vA v

(r) ( )

à SC(A, r) : number of records that will satisfy the selection

à ⎡SC(A, r)/f) frr⎤ — number of blocks that these records will occupyp

à E.g. Binary search cost estimate becomes

⎡ ⎤ ⎡ ⎤

EA = ⎡ log2(br)⎤ + ⎡SC(A, r)/fr⎤ - 1

l d k b SC( )

à Equality condition on a key attribute: SC(A,r) = 1

(18)

Join Size Estimation

ƒ r s = r x s if R ∩ S = ∅

J

à r x s contains nr * ns tuples

à each tuple occupies sr + ss bytes

ƒ If R ∩ S is a key for R y

à then a tuple of s will join with at most one tuple from r

à therefore, the number of tuples in r s is no greater than the number of , p g tuples in s.

à In the example query depositor customer

‚ customer-name in depositor is a foreign key of customer

‚ hence, the result has (exactly) ndepositortuples

(19)

Join Size Estimation (cont.)

ƒ If R ∩ S = {A} is not a key for R or S

J ( )

{ } y

à If every tuple t in R produces tuples in R S: ( nr * ns ) / V(A, s)

à If the reverse is true: ( nr * ns ) / V(A, r)

à The lower of these two estimates is probably the more accurate one.

à Compute the size estimates for depositor customer without using information about foreign keys:

‚ V(customer-name, depositor) = 2,500 V(customer-name, customer) = 10 000 V(customer name, customer) 10,000

‚ The two estimates are 5,000 * 10,000/2,500 = 20,000 and 5,000 * 10,000/10,000 = 5,000

(20)

Choice of Evaluation Plans

ƒ Must consider the interaction of evaluation techniques when q choosing evaluation plans

à choosing the cheapest algorithm for each operation independently may not yield best overall algorithm. E.g.

‚ merge-join may be costlier than hash-join, but may provide a sorted output which reduces the cost for an outer level aggregation

which reduces the cost for an outer level aggregation.

‚ nested-loop join may provide opportunity for pipelining

ƒ Practical query optimizers incorporate elements of the following

ƒ Practical query optimizers incorporate elements of the following two broad approaches:

1 Search all the plans and choose the best plan in a 1. Search all the plans and choose the best plan in a

cost-based fashion.

2. Uses heuristics to choose a plan.p

(21)

Cost-Based Optimization p

ƒ Consider finding the best join-order for r g j

11

r

22

. . . r

nn

.

ƒ There are (2(n – 1))!/(n – 1)! different join orders for above

expression. With n = 7, the number is 665280, with n = 10, the expression. With n 7, the number is 665280, with n 10, the number is greater than 176 billion!

ƒ No need to generate all the join orders Using dynamic

ƒ No need to generate all the join orders. Using dynamic programming, the least-cost join order for any subset of

{r

1

r

2

r } is computed only once and stored for future use

{r

1

, r

2

, . . . r

n

} is computed only once and stored for future use.

(22)

Heuristic Optimization p

ƒ Cost-based optimization is expensive, even with dynamic p p y programming

à Search space grows exponentially!

ƒ Heuristic optimization

à make transformations based on a set of rules that typically (but not in all yp y ( cases) improve execution performance:

‚ Perform selection early (reduces the number of tuples)

‚ Perform projection early (reduces the number of attributes)

‚ Perform most restrictive selection and join operations before other similar operations

operations

à Some systems use only heuristics, others combine heuristics with partial cost-based optimization.p

(23)

END OF CHAPTER 14

END OF CHAPTER 14

참조

관련 문서

This designation is without prejudice to positions on status, and is in line with United Nations Security Council Resolution 1244/99 and the Opinion of the International

웹 표준을 지원하는 플랫폼에서 큰 수정없이 실행 가능함 패키징을 통해 다양한 기기를 위한 앱을 작성할 수 있음 네이티브 앱과

_____ culture appears to be attractive (도시의) to the

【판결요지】[1] [다수의견] 동일인의 소유에 속하는 토지 및 그 지상 건물에 관하여 공동저 당권이 설정된 후 그 지상 건물이 철거되고 새로 건물이 신축된 경우에는

14 More specifically, the research looks at the impact of a variety of interventions, such as changes in transaction costs, tax rates, financial literacy interventions as well

1 John Owen, Justification by Faith Alone, in The Works of John Owen, ed. John Bolt, trans. Scott Clark, "Do This and Live: Christ's Active Obedience as the

– Treat wastewater before it is discharged to maintain the water quality of the natural waters for the benefit of humans and aquatic ecosystem. • Wastewater management goal

Protecting the aquatic ecosystem in the water body Recover water and resources from wastewater and. produce renewable energy using wastewater constituents.. • So, the goal