2081 2

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]

Advanced DatabaseChapter 3: Query Processing and OptimizationQuery Trees and Heuristics for Query Optimization

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:

ReasonExplanation
Improved PerformanceReduces execution time of queries, making data retrieval faster
Resource UtilizationMinimizes CPU usage, disk I/O, and network traffic
Cost ReductionMinimizes resource usage and improves overall efficiency of query execution
ScalabilityTechniques like indexing, caching, and parallel processing help handle growing workloads
Complex Query SupportConsiders query rewriting, join ordering, and index selection for complex queries
Application ResponsivenessProvides 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:

  1. 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.
  2. 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.
  3. Perform the most restrictive selection and join operations first

    • Operations that eliminate the most data should be applied before less restrictive ones.
  4. 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:

  1. Parses the SQL query into an initial query tree (usually based on relational algebra).
  2. Applies heuristic rules to push selections and projections down the tree (closer to the leaf nodes/base relations).
  3. 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 > 50000 is 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 ename and dname.

Benefits of This Optimization

StepWithout HeuristicWith Heuristic
JOIN input sizeFull EMPLOYEE table (e.g., 10,000 rows)Filtered EMPLOYEE (e.g., 500 rows)
Intermediate resultVery largeMuch smaller
Execution timeSlowSignificantly 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.