Upload
niyishaka-patrick
View
64
Download
1
Embed Size (px)
Citation preview
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.
5. Query Optimization
Heuristic rules
Select lname from employee, works_on, project where pname = ‘aquarius’ and
pnumber = pno and essn=ssn and bdate > ‘31-12-1957’;
5. Query Optimization
▪ Example
5. Query Optimization
▪ Example
Initial (canonical) query tree Moving select operations down the tree
5. Query Optimization
▪ Example
Applying the more restrictive select operation first Replacing Cartesian product by join
5. Query Optimization
▪ Example
Moving project down the tree
♦ 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.
5. Query Optimization
6. Translation Rules
▪ Rules
Apply first operations that reduce size of intermediary results,
Then1. Perform as early as possible select and project (move down the tree
the select and project)2. Execute first most restrictive join and select
Thenreorder leaf nodesavoid Cartesian productadjust rest of tree accordingly
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 a2 a1
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)
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)
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)