Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Query Optimization

Similar presentations


Presentation on theme: "Overview of Query Optimization"— Presentation transcript:

1 Overview of Query Optimization
Logical Optimization: Using some transformation rules and algebraic equivalences To choose different join orders R ⋈ S  S ⋈ R (Commutative) (R ⋈ S) ⋈ T  R ⋈ (S ⋈ T) (Associative) To push selections and projections bellow of joins (attr1 op value (R) ) ⋈ S  attr1 op value (R ⋈ S ) attr1, attr2, attr3 ((attr1, attr2 (R) ) ⋈ S )  attr1, attr2,attr3 (R ⋈ S ) Physical Optimization: For a given R.A expression There could be several different plans possible using different implementation of the R.A operators Calculate the cost of these different plans and choose the best Ideally, we want the best plan, but Practically, we should avoid worst plans Advanced Databases: Lecture # 8 Query Optimization (III)

2 Pushing Selection & Projection below Join
A join is quite expensive operation. The cost can be reduced by reducing the sizes of the input relations. The sizes of the Input Relations can be reduced by applying: Selection: restricting the input relation i.e. number of tuples Projection: reducing the number of columns. Usually Selection reduces the size of the input relation more than Projection. Projection before the Join should be done quite carefully as the cost of Projection could increase the overall cost if it does not reduce the size of the input by a good factor. Advanced Databases: Lecture # 8 Query Optimization (III)

3 Estimating the Evaluation Cost of a Plan
For each plan, we should be able to estimate the overall cost. For each node in the query tree, we estimate the cost of performing the corresponding operation; For each node in the query tree, we estimate the size of the result, which is used by the operation in the parent node; In order to correctly estimate the cost of each operation and the size of its result, the optimizer uses certain statistical information maintained by the DBMS. Advanced Databases: Lecture # 8 Query Optimization (III)

4 Statistics Maintained by a DBMS
Cardinality The number of tuples for each relation. Size The number of pages for each relation. Index Cardinality The number of distinct key values for each index. Index Size The number of pages for each index. Index Height The number of non-leaf levels for each tree index. Index Range The minimum present key value and the maximum present key value for each index. Advanced Databases: Lecture # 8 Query Optimization (III)

5 A motivating example SELECT S.sname FROM Reserves R, Sailors S Where R.sid = S.sid AND R.bid = AND S.rating > 5 Cost = * 1000 I/Os (Using nested loops – page-oriented) 500,500 I/Os This is not an efficient plan. We could have pushed selections down before the join, no index is used. Goal of Optimization: To find more efficient plans that compute the same answer. Advanced Databases: Lecture # 8 Query Optimization (III)

6 Optimization: Alternative 1 (no index)
Push Selects before join Assuming that there are 1000 tuples in Reserves with bid=100 and tuples in Sailors with rating > 5. So Cost of Selections: Scan Reserves (1000 pages) and write the selected 1000 tuples to temp relation T1 (10 pages), so in total 1010 I/Os Scan Sailors (500 pages) and write the selected tuples to temp relation T2 (250 pages), so in total 750 I/Os Total cost = = 1760 I/Os so far Using SNL the cost = * 250 = 2510 I/Os Total cost = = 4270 I/Os about 117 times less than the cost of the initial plan i.e. 500,500 I/Os Advanced Databases: Lecture # 8 Query Optimization (III)

7 Optimization: Alternative 2 (uses indexes)
The same RA as alternative 1 Using a clustered Hash index on Reserves: A hash index will take 1 plus 10 to retrieve the 1000 qualifying tuples Cost of Selection = = 11 I/Os Cost of Writing to T1 = 10 I/Os Sub-Total = = 21 The size of T1 = 10 pages Using a clustered B+ tree index on Sailors: Cost of Selection = 2 (the constant cost) = 252 I/Os, Cost of Writing to T2 = 250 I/Os, Sub-Total = = 502, and the size of T2 = 250 pages Using SNL the cost is: Cost = * 250 = 2510 I/Os Total cost = = 3033 I/Os, which is 165 times less than the cost of the initial plan i.e. 500,500 I/Os Advanced Databases: Lecture # 8 Query Optimization (III)

8 Optimization: Alternative 3(using pipelining i. e
Optimization: Alternative 3(using pipelining i.e. intermediate results are not written to disk) The same RA as alternative 1 Using a clustered Hash index on Reserves: A hash index will take 1 plus 10 to retrieve the 1000 qualifying tuples Cost of Selection = = 11 I/Os The size of T1 = 10 pages Using a clustered B+ tree index on Sailors: Cost of Selection = 2 (the constant cost) = 252 I/Os The size of T2 = 250 pages Using Block-Nested Loops join with 7 buffer pages: Cost = Total cost = = 773 I/Os, which is 647 times faster than the initial plan and about 4 times faster than the previous one. Advanced Databases: Lecture # 8 Query Optimization (III)


Download ppt "Overview of Query Optimization"

Similar presentations


Ads by Google