The document discusses query optimization techniques used by database systems. It describes how a database catalog is used to estimate query execution costs. Alternative execution plans are enumerated using heuristic rules and the lowest cost plan is selected. Examples are provided of applying translation rules to optimize a query tree by pushing down select and project operations, avoiding Cartesian products, and reordering joins and selections. Cost estimation considers factors like I/O, memory usage, and computation to select a suboptimal but efficient execution plan.
SQL Database Design For Developers at php[tek] 2024
2 optimization
1. 5. Query Optimization
5. Query Optimization
▪ The cost is estimated using statistical information from the database
catalog (number of tuples in each relation,size of tuples, etc.)
▪ Enumerating alternative execution plans based on heuristic rules.
- Order operations using translation rules.
- Work well in most cases but are not guaranteed to work well
in all cases.
▪ Estimate cost of each enumerated execution plan:
- Cost of different strategies.
- Chose execution plan with lower cost.
2. 5. Query Optimization
▪ Example
Heuristic rules
Select lname
from employee, works_on, project
where pname = ‘aquarius’ and
pnumber = pno and
essn=ssn and
bdate > ‘31-12-1957’;
3. 5. Query Optimization
▪ Example
Initial (canonical) query tree
Moving select operations down the tree
4. 5. Query Optimization
▪ Example
Applying the more restrictive select operation first
Replacing Cartesian product by join
6. 5. Query Optimization
♦ Semantic query optimisation
Use constraints to guide the query processing:
E.g. Given the constraint “no employee earns more than their supervisor”
Select e.name
from employee e m
where e.salary > m.salary
and m.ni# = e.supervisor
The above query is obviously empty.
7. 6. Translation Rules
▪ Rules
Apply first operations that reduce size of intermediary results,
Then
1. Perform as early as possible select and project (move down the tree
the select and project)
2. Execute first most restrictive join and select
Then
reorder leaf nodes
avoid Cartesian product
adjust rest of tree accordingly
8. 6. Translation Rules
▪ Examples:
Cascade of select
Select (r, c1 and c2 and c3) select (select (select (r,c1), c2), c3)
Commutatively of select
Select (select (r, c1), c2) select (select (r, c2), c1)
Cascade of project
Project (project (project (r, a3), a2), a1) project (r, a1) where a3
Commuting select with join
Select (join (r,s), c) join (select (r,c), s)
When c applies to r only
Select (join (r,s), c1 and c2) join (select (r,c1), select (s,c2))
When c1 applies to r
When c2 applies to s
Commuting select with , , and Select (r s, c) select (r,c) select (s,c)
a2
a1
9. 7. Cost Estimations
How to Estimation
▪ Estimate and compare costs of executing query using different execution strategies.
Choose strategy with lower cost
Cost estimates
Limit number of strategies
▪ Compiled vs. Interpreted queries
▪ Traditional optimisation techniques,
- Search solution space to a problem for a solution
- Minimise cost function
▪ Selection of a strategy (not always optimal)
10. 7. Cost Estimations
Cost components for query execution
▪ Access to secondary storage (searching, reading, writing blocks)
▪ Storage cost (intermediate files)
▪ Computational cost (searching, sorting, merging in main memory)
▪ Memory usage cost (memory buffers)
▪ Communication cost (distributed dbs)