Query Optimization
Query Optimization
Query Optimization
Query Plan 1
COST ESTIMATION 5
Statistics and Catalogs 5
Result Size Estimation 5
Result Size Estimation Calculations 5
SINGLE TABLE SELECTION 6
CALCULATING REDUCTION FACTORS 6
There are many ways of executing a given query which gives the same result
However, cost of alternative methods often varies enormously
Query optimization aims to find the execution strategy with the lowest cost
Query Plan
● Basically a tree with relational algebra operators as the nodes and access path as the leaf nodes
● Each operator are labeled with a choice of algorithm
CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically
deleted as well.
COST ESTIMATION
FOR EACH PLAN in TREE
1. EstimateSizeOfResult(PlanTree)
a. Tree is a sequence of operations
i. Output of the previous operation is the input to subsequent operation
ii. Need to know size of output for the size of input of subsequent operation
2. EstimateCost(PlanTree)
a. Depends on input cardinalities
b. SequentialScan, IndexScan, Joins
c. Cost of entire plan
● Maximum number of tuples in the result is the product of the cardinalities of relations in the FROM
clause
● REDUCTION FACTOR (RF) associated with each predicakes reflects the impact of the predicate in
reducing the result size. RF is also known as SELECTIVITY
If there are no selections (no predicates), reduction factors are ignored (==1).
EXAMPLE