Lecture: Lecture 15 - Query Optimization
Related: Physical Access Structures and Access Methods, Buffer Manager and Page Replacement

What the optimizer estimates

The optimizer uses table profiles stored in the data dictionary. A profile may include row count, tuple and attribute sizes, number of distinct values, minimum and maximum values, and estimates for intermediate expressions.

A simplified equality estimate is:

Card(A = v in T) ≈ Card(T) / number of distinct A values

This assumes values are distributed uniformly. It is an estimate, so stale statistics or skewed data can lead to a poor plan.

Scan versus index

A sequential scan reads every tuple. It is often best when a table is small or a predicate matches a large fraction of rows. An index is attractive when it can avoid most page reads.

  • Equality predicates can use hash, B+-Tree or bitmap indexes.
  • Range predicates naturally use B+-Trees.
  • Bitmap indexes are useful for combinations of predicates on low-cardinality attributes.

For several AND conditions, the optimizer can evaluate a selective condition first or intersect matching RIDs/bitmaps before reading the table. An index is not automatically faster when many rows match.

Cost model

The lecture expresses cost approximately as:

Ctotal = CIO × nIO + CCPU × nCPU

Disk I/O often dominates because pages are much slower to retrieve than data already in memory. The optimizer therefore considers how many pages each plan reads, as well as CPU work.

Retrieval questions

Why can a full scan beat an index? If most rows match, following index pointers may cause many random page reads, while a sequential scan reads pages efficiently.

What does cardinality mean? The number of tuples in a relation or intermediate result.

What assumption appears in the simple equality estimate? Uniform distribution of attribute values.