Docsity

Query Optimization – Lecture Slides | CS 587, Study notes of Computer Science

Partial preview of the text

Download Query Optimization – Lecture Slides | CS 587 and more Study notes Computer Science in PDF only on Docsity!

CSE 544

Principles of Database

Management Systems

Magdalena Balazinska

Fall 2007

Lecture 9 - Query optimization

References

  • Access path selection in a relational database

management system.

Selinger. et. al. SIMOD 1979

  • Database management systems.

Ramakrishnan and Gehrke.

Third Ed. Chapter 15.

Query Optimization Algorithm

  • For a query
    • There exists many physical query plans
    • Query optimizer needs to pick a good one
  • Basic query optimization algorithm
    • Enumerate alternative plans
    • Compute estimated cost of each plan
      • Compute number of I/Os
      • Optionally take into account other resources
    • Choose plan with lowest cost
    • This is called cost-based optimization

Outline

  • Basic query optimization algorithm
  • Typical query optimizer (based on System R)
    • Estimating the cost of a query plan
    • Search space
    • Algorithm for enumerating query plans
  • Other types of optimizers

Access Path

  • Access path : a way to retrieve tuples from a table
    • A file scan
    • An index plus a matching selection condition
  • Index matches selection condition if it can be used to

retrieve just tuples that satisfy the condition

  • Example: Supplier(sid,sname,scity,sstate)
  • B+-tree index on (scity,sstate)
    • matches scity=‘Seattle’
    • does not match sid=3, does not match sstate=‘WA’

Access Path Selection

  • Supplier(sid,sname,scity,sstate)
  • Selection condition: sid > 300 ∧ scity=‘Seattle’
  • Indexes: B+-tree on sid and B+-tree on scity
  • Which access path should we use?
  • We should pick the most selective access path

Selectivity for Range Predicates

Selection on range: σ

a>v

(R)

  • How to compute the selectivity?
  • Assume values are uniformly distributed
  • Reduction factor X
  • X = (Max(R,a) - v) / (Max(R,a) - Min(R,a))
  • Clustered index on a: cost B(R)*X
  • Unclustered index on a: cost T(R)*X

Back to Our Example

  • Selection condition: sid > 300 ∧ scity=‘Seattle’
    • Index I1: B+-tree on sid clustered
    • Index I2: B+-tree on scity unclustered
  • Let’s assume
    • V(Supplier,scity) = 20
    • Max(Supplier, sid) = 1000, Min(Supplier,sid)=
    • B(Supplier) = 100, T(Supplier) = 1000
  • Cost I1: B(R) * (Max-v)/(Max-Min) = 100700/999* ≈ 70
  • Cost I2: T(R) * 1/V(Supplier,scity) = 1000/20 = 50

Back to Estimating

Cost of a Query Plan

  • We already how to
    • Compute the cost of different operations
    • Compute cost of retrieving tuples from disk with different access paths (for more sophisticated predicates than equality)
  • We still need to
    • Compute cost of a complete plan

Computing the Cost of a Plan

  • Collect statistical summaries of stored data
  • Compute cost in a bottom-up fashion
  • For each operator compute
    • Estimate cost of executing the operation
    • Estimate statistical summary of the output data

Computing Cost of an Operator

  • The cost of executing an operator depends
    • On the operator implementation
    • On the input data
  • We learned how to compute this cost last two lectures

Statistics on the Output Data

  • Most important piece of information
    • Size of operator result
    • I.e., the number of output tuples
  • Projection: output size same as input size
  • Selection: multiply input size by reduction factor
    • Similar to what we did for estimating access path selectivity
    • Assume independence between conditions in the predicate
    • (use product of the reduction factors for the terms)

Our Example

  • Suppliers(sid,sname,scity,sstate)
  • Supplies(pno,sid,quantity)
  • Some statistics
    • T(Supplier) = 1000 records
    • B(Supplier) = 100 pages
    • T(Supplies) = 10,000 records
    • B(Supplies) = 100 pages
    • V(Supplier,scity) = 20, V(Supplier,state) = 10
    • V(Supplies,pno) = 3,
    • Both relations are clustered

Physical Query Plan 1

Suppliers Supplies

sno = sno

scity=‘Seattle’ ∧sstate=‘WA’ ∧ pno=

sname

(File scan) (File scan)

(Nested loop)

(On the fly)

(On the fly) Selection and project on-the-fly

-> No additional cost. Total cost of plan is thus cost of join: = B(Supplier)+B(Supplier)*B(Supplies) = 100 + 100 * 100 = 10,100 I/Os