Docsity

Cost-Based Query Optimization - Data Base Management System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Cost-Based Query Optimization - Data Base Management System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Outline

  • Introduction
  • Steps in Cost-based query optimization- Query Flow
  • Projection Example
  • Query Interaction in DBMS
  • Cost-based query Optimization: Algebraic Expressions

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?
  • 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

  • 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.

Query Flow

Parser

Optimizer

Code Generator/ Interpreter

Processor

SQL

  • 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.

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.

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 bytes Each Block (1024): header = 24 bytes

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

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?

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.
  • 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.

projection

filter

join

Scan (Patients) Scan (Doctors)

Cost-based query Optimization :

Transformation

projection projection filter join join Filter

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