CSC475 ยท 2081 ยท 10 marks
Why do we need query optimization in databases? Explain heuristic query optimization with examples.
Why do we need query optimization in databases? Explain heuristic query optimization with examples.[10]
Quick answer
Query optimization is essential in database systems for the following reasons: Perform SELECT (selection) operations as early as possible; Selection reduces the number of tuples in intermediate results; Applying it before JOIN or other binary operations reduces the size of files being joined.
Query Optimization in Databases
Why Do We Need Query Optimization?
Query optimization is essential in database systems for the following reasons:
| Reason | Explanation |
|---|---|
| Improved Performance | Reduces execution time of queries, making data retrieval faster |
| Resource Utilization | Minimizes CPU usage, disk I/O, and network traffic |
| Cost Reduction | Minimizes resource usage and improves overall efficiency of query execution |
| Scalability | Techniques like indexing, caching, and parallel processing help handle growing workloads |
| Complex Query Support | Considers query rewriting, join ordering, and index selection for complex queries |
| Application Responsiveness | Provides faster responses, leading to better user experiences |
Heuristic Query Optimization
Definition
Heuristic query optimization transforms an initial query tree into a final (optimized) query tree using a set of equivalence transformation rules (heuristic rules). These rules typically improve execution performance without computing the actual cost of each alternative plan.
The internal representation of the query is usually a query tree or query graph data structure, and heuristic rules are applied to modify this structure to improve performance.
Key Heuristic Rules
The following are the most commonly applied heuristic rules:
-
Perform SELECT (selection) operations as early as possible
- Selection reduces the number of tuples in intermediate results.
- Applying it before JOIN or other binary operations reduces the size of files being joined.
-
Perform PROJECT (projection) operations as early as possible
- Projection reduces the number of attributes (columns) in intermediate results.
- This reduces the size of tuples flowing through the query tree.
-
Perform the most restrictive selection and join operations first
- Operations that eliminate the most data should be applied before less restrictive ones.
-
Apply SELECT before JOIN or other binary operations
- This is the main heuristic rule: since SELECT and PROJECT reduce file size, they should always precede JOIN operations.
How Heuristic Query Optimizer Works
Initial Query Tree
|
v
Apply Equivalence Transformation Rules (Heuristics)
|
v
Final Optimized Query Tree
The optimizer:
- Parses the SQL query into an initial query tree (usually based on relational algebra).
- Applies heuristic rules to push selections and projections down the tree (closer to the leaf nodes/base relations).
- Produces a final query tree that is expected to execute more efficiently.
Example
Consider the following query:
SELECT e.ename, d.dname
FROM EMPLOYEE e, DEPARTMENT d
WHERE e.dno = d.dnumber
AND e.salary > 50000;
Step 1: Initial Query Tree (Naive Plan)
The naive approach applies operations in the order they appear:
PROJECT (e.ename, d.dname)
|
SELECT (e.salary > 50000)
|
JOIN (e.dno = d.dnumber)
/ \
EMPLOYEE DEPARTMENT
In the naive plan, the JOIN is performed first on the full EMPLOYEE and DEPARTMENT tables, producing a large intermediate result. Then the SELECT filter is applied.
Step 2: Apply Heuristic Rules
Rule Applied: Push the SELECT operation below the JOIN (apply it early, before the JOIN).
Step 3: Final Optimized Query Tree
PROJECT (e.ename, d.dname)
|
JOIN (e.dno = d.dnumber)
/ \
SELECT DEPARTMENT
(e.salary > 50000)
|
EMPLOYEE
What changed:
- The selection
e.salary > 50000is now applied directly on EMPLOYEE before the JOIN. - This filters out employees with salary <= 50000 first, producing a much smaller intermediate relation.
- The JOIN is then performed on this smaller EMPLOYEE result and the DEPARTMENT table.
- Finally, PROJECT reduces the attributes to only
enameanddname.
Benefits of This Optimization
| Step | Without Heuristic | With Heuristic |
|---|---|---|
| JOIN input size | Full EMPLOYEE table (e.g., 10,000 rows) | Filtered EMPLOYEE (e.g., 500 rows) |
| Intermediate result | Very large | Much smaller |
| Execution time | Slow | Significantly faster |
Summary
Heuristic query optimization is a rule-based approach that improves query performance by transforming the query tree. The core principle is:
"Apply SELECT and PROJECT early to reduce intermediate result sizes before performing expensive JOIN operations."
Unlike cost-based optimization (which uses statistics and computes actual costs), heuristic optimization applies general rules that are known to improve performance in most cases, making it simpler and faster to apply.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.