Query Optimisation

COMP9315 21T1 ♢ Query Optimisation ♢ [0/14]
❖ Query Optimisation

Query optimiser:   RA expression efficient evaluation plan

[Diagram:Pics/qproc/qproc2.png]

COMP9315 21T1 ♢ Query Optimisation ♢ [1/14]
❖ Query Optimisation (cont)

Query optimisation is a critical step in query evaluation.

The query optimiser

"Optimisation" is a misnomer since query optimisers
Observed Query Time = Planning time + Evaluation time
COMP9315 21T1 ♢ Query Optimisation ♢ [2/14]
❖ Query Optimisation (cont)

Why do we not generate optimal query execution plans?

Finding an optimal query plan ...

Even for relatively small query, search space is very large.

Compromise:

COMP9315 21T1 ♢ Query Optimisation ♢ [3/14]
❖ Approaches to Optimisation

Three main classes of techniques developed:

All driven by aim of minimising (or at least reducing) "cost".

Real query optimisers use a combination of algrebraic+physical.

Semantic QO is good idea, but expensive/difficult to implement.

COMP9315 21T1 ♢ Query Optimisation ♢ [4/14]
❖ Approaches to Optimisation (cont)

Example of optimisation transformations:

[Diagram:Pics/qproc/query-transform.png]


For join, may also consider sort/merge join and hash join.

COMP9315 21T1 ♢ Query Optimisation ♢ [5/14]
❖ Cost-based Query Optimiser

Approximate algorithm for cost-based optimisation:

translate SQL query to RAexp
for enough transformations RA' of RAexp {
   while (more choices for RelOps) {
      Plan = {};  i = 0;  cost = 0
      for each node e of RA' (recursively) {
         ROp = select RelOp method for e
         Plan = Plan ∪ ROp
         cost += Cost(ROp) // using child info
      }
      if (cost < MinCost)
         { MinCost = cost;  BestPlan = Plan }
   }
}

Heuristics: push selections down, consider only left-deep join trees.

COMP9315 21T1 ♢ Query Optimisation ♢ [6/14]
❖ Cost Models and Analysis

The cost of evaluating a query is determined by:

Analysis of costs involves estimating:
COMP9315 21T1 ♢ Query Optimisation ♢ [7/14]
❖ Choosing Access Methods (RelOps)

Performed for each node in RA expression tree ...

Inputs:

Output:
COMP9315 21T1 ♢ Query Optimisation ♢ [8/14]
❖ Choosing Access Methods (RelOps) (cont)

Example:

giving

tmp[i]   := BtreeSearch[name='John'](Student)
tmp[i+1] := LinearSearch[age>21](tmp[i])

Where possible, use pipelining to avoid storing tmp[i] on disk.

COMP9315 21T1 ♢ Query Optimisation ♢ [9/14]
❖ Choosing Access Methods (RelOps) (cont)

Rules for choosing σ access methods:

COMP9315 21T1 ♢ Query Optimisation ♢ [10/14]
❖ Choosing Access Methods (RelOps) (cont)

Rules for choosing access methods:

 
(bnl = block nested loop;   inl = index nested loop;   sm = sort merge)
COMP9315 21T1 ♢ Query Optimisation ♢ [11/14]
❖ PostgreSQL Query Optimization

Input: tree of Query nodes returned by parser

Output: tree of Plan nodes used by query executor

Intermediate data structures are trees of Path nodes All Node types are defined in include/nodes/*.h
COMP9315 21T1 ♢ Query Optimisation ♢ [12/14]
❖ PostgreSQL Query Optimization (cont)

Query optimisation proceeds in two stages (after parsing)...

Rewriting:

Planning and optimisation: Then produces a Plan tree from the selected path.
COMP9315 21T1 ♢ Query Optimisation ♢ [13/14]
❖ PostgreSQL Query Optimization (cont)

[Diagram:Pics/qproc/qopt-trees1.png]

COMP9315 21T1 ♢ Query Optimisation ♢ [14/14]


Produced: 5 Apr 2021