Top Banner
Query Optimization CS 157B Ch. 14 Mien Siao
24

Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Dec 23, 2015

Download

Documents

Maud Anis Small
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Query Optimization

CS 157B Ch. 14 Mien Siao

Page 2: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Outline Introduction Steps in Cost-based query optimization- Query

Flow Projection Example Query Interaction in DBMS Cost-based query Optimization: Algebraic

Expressions

Page 3: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Introduction What is Query Optimization? Suppose you were given a chance to

visit 15 pre-selected different cities in Europe. The only constraint would be ‘Time’ -> Would you have a plan to visit the cities in any order?

Page 4: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Europe

Page 5: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Plan: -> Place the 15 cities in different groups based on their proximity to each other. -> Start with one group and move on to the next group.

Important point made over here is that you would have visited the cities in a more organized manner, and the ‘Time’ constraint mentioned earlier would have been dealt with efficiently.

Page 6: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Query Optimization works in a similar way: There can be many different ways to get an answer from a given query. The result would be same in all scenarios.

DBMS strive to process the query in the most efficient way (in terms of ‘Time’) to produce the answer.

Cost = Time needed to get all answers

Page 7: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Starting with System-R, most of the commercial DBMSs use cost-based optimizers.

The estimation should be accurate and easy. Another important point is the need for being logically consistent because the least cost plan will always be consistently low.

Page 8: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Steps in a Cost-based query optimization

1. Parsing 2. Transformation3. Implementation4. Plan selection based on cost

estimates

Page 9: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Query Flow

Parser

Optimizer

Code Generator/Interpreter

Processor

SQL

Page 10: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Query Parser – Verify validity of the SQL statement. Translate query into an internal structure using relational calculus.

Query Optimizer – Find the best expression from various different algebraic expressions. Criteria used is ‘Cheapness’

Code Generator/Interpreter – Make calls for the Query processor as a result of the work done by the optimizer.

Query Processor – Execute the calls obtained from the code generator.

Page 11: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Cost of physical plans includes processor time and communication time. The most important factor to consider is disk I/Os because it is the most time consuming action.

Some other costs associated are:- Operations (joins, unions, intersections).- The order of operations.

Why?

Page 12: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Joins, unions, and intersections are associative and commutative. - Management of storage of arguments and passing of it.

Factors mentioned above should be limited and minimized when creating the best physical plan.

Page 13: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Projection Example: Projections produce a result tuple for every

argument tuple. What is the change? Change in the output size is the change in the

length of tuples

Let’s take a relation ‘R’Relation (20,000 tuples): R(a, b, c)Each Tuple (190 bytes): header = 24 bytes, a = 8

bytes, b = 8 bytes, c = 150 bytesEach Block (1024): header = 24 bytes

Page 14: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

We can fit 5 tuples into 1 block- 5 tuples * 190 bytes/tuple = 950 bytes

can fit into 1 block - For 20,000 tuples, we would require 4,000

blocks (20,000 / 5 tuples per block = 4,000

With a projection resulting in elimination of column c (150 bytes), we could estimate that each tuple would decrease to 40 bytes (190 – 150 bytes)

Page 15: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Now, the new estimate will be 25 tuples in 1 block.

- 25 tuples * 40 bytes/tuple = 1000 bytes will be able to fit into 1 block

- With 20,000 tuples, the new estimate is 800 blocks (20,000 tuples / 25 tuples per block = 800 blocks)

Result is reduction by a factor of 5

Page 16: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Query interaction in DBMS How does a query interact with a

DBMS?- Interactive users- Embedded queries in programs written in C, C++, etc.

What is the difference between these two ?

Page 17: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Interactive Users: - When there is an interactive user

query, the query goes through the Query Parser, Query Optimizer, Code Generator, and Query Processor each time.

Page 18: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Embedded Query:- When there is an embedded query,

the query does not have to through the Query Parser, Query Optimizer, Code Generator, and the Query Processor each time.

Page 19: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

- In an embedded query, the calls generated by the code generator are stored in the database. Each time the query is reached within the program at run-time, the Query Processor invokes the stored calls in the database.

- Optimization is independent in embedded queries.

Page 20: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Cost-based query Optimization: Algebraic Expressions

If we had the following query-

SELECT p.pname, d.dnameFROM Patients p, Doctors dWHERE p.doctor = d.dname

AND d.dgender = ‘M’

Page 21: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

projection

filter

join

Scan (Patients) Scan (Doctors)

Page 22: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Cost-based query Optimization : Transformation

projection projection filter join

join Filter

Scan (Patients) Scan (Doctors) Scan(Patients) Scan(Doctors)

Page 23: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Cost-based query Optimization: Implementation

projection projection

filter hash join

natural join filter

Scan(Patients) Scan(Doctors) Scan(Patients) Scan(Doctors)

Page 24: Query Optimization CS 157B Ch. 14 Mien Siao. Outline Introduction Steps in Cost-based query optimization- Query Flow Projection Example Query Interaction.

Cost-based query Optimization: Plan selection based on costs

projection projection

filter hash join

natural join filter

Scan(Patients) Scan(Doctors) Scan(Patients) Scan(Doctors)

Estimated Costs= 100ms

Estimated Costs= 50ms