Advanced Database · Unit 3 · 7 hrs
Query Processing and Optimization
Exam-focused notes for Query Processing and Optimization (Advanced Database, CSC475): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Query Processing and Optimization in short
This unit covers how a DBMS turns a high-level SQL query into an efficient execution plan: the stages of query processing, relational-algebra equivalence rules, heuristic and cost-based optimization, join algorithms and their I/O costs, and how an optimizer chooses among competing plans.
What this unit covers
- Concept of Query Processing
- Query Trees and Heuristics for Query Optimization
- Choice of Query Execution Plans
- Cost-Based Optimization
Query Trees and Heuristics for Query Optimization
2081 · 10 marks
Why do we need query optimization in databases? Explain heuristic query optimization with examples.[10]
Show solved answerHide answer
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.
2081 · 5 marks
What are the uses of query trees in query processing? Explain. [5]
Show solved answerHide answer
Uses of Query Trees in Query Processing
Definition
A query tree is a hierarchical (tree-based) representation of the steps and operations involved in executing a database query. It outlines the logical and physical operations required to retrieve the desired data from the database.
Uses of Query Trees in Query Processing
1. Visualization and Understanding
Query trees provide a visual representation of the query execution plan. This makes it easier for database administrators, developers, and optimizers to understand and analyze the sequence of steps involved in processing a query. Instead of reading complex procedural instructions, one can simply trace the tree from leaves (base relations) to the root (final result).
2. Query Optimization
By representing the query execution plan as a tree, the query optimizer can efficiently explore and evaluate various alternative plans. It can rearrange nodes, reorder operations (such as pushing selections down the tree), and compare costs of different plans to select the most efficient execution strategy. This directly helps in minimizing execution time and resource consumption.
3. Execution Order and Data Flow
Query trees define the order in which operations (such as selection, projection, join) are performed and how data flows between them. Each internal node represents an operation, and data flows upward from child nodes to parent nodes, making the execution sequence explicit and unambiguous.
4. Query Plan Caching and Reuse
Query trees can be cached and reused for similar queries that differ only in parameter values. When the same or a structurally similar query is submitted again, the stored query tree can be retrieved and reused, avoiding the overhead of re-parsing and re-optimizing. This significantly improves query performance.
5. Support for Complex Queries
Query trees help in managing and processing complex queries involving multiple joins, subqueries, and nested operations. By breaking down a complex query into a structured tree of simpler operations, the system can handle each operation systematically and efficiently.
Summary Table
| Use | Benefit |
|---|---|
| Visualization | Easier understanding of execution plan |
| Query Optimization | Finds the most efficient execution strategy |
| Execution Order & Data Flow | Defines clear operation sequence |
| Plan Caching & Reuse | Avoids redundant computation |
| Complex Query Support | Manages multi-step queries systematically |
In summary, query trees are a fundamental tool in query processing that bridge the gap between a high-level SQL query and its efficient execution by the DBMS.
2080 · 5 marks
Why query optimization is essential in databases? What is heuristic optimization? [5]
Concept of Query Processing
2079 · 5 marks
What is query processing? Differentiate query processing with query optimization. [5]
Choice of Query Execution Plans
2079 · 5 marks
Why query optimization is necessary? Illustrate on the choices of query execution plans. [5]
Make Unit 3 stick
Practice CSC475 with flashcards & quizzes