10
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

2 optimization

Embed Size (px)

Citation preview

Page 1: 2 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.

5. Query Optimization

Page 2: 2 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

Page 3: 2 optimization

5. Query Optimization

▪ Example

Initial (canonical) query tree Moving select operations down the tree

Page 4: 2 optimization

5. Query Optimization

▪ Example

Applying the more restrictive select operation first Replacing Cartesian product by join

Page 5: 2 optimization

5. Query Optimization

▪ Example

Moving project down the tree

Page 6: 2 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.

5. Query Optimization

Page 7: 2 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

Page 8: 2 optimization

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)

Page 9: 2 optimization

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)

Page 10: 2 optimization

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)