Course: Data Science and Database Technologies
Source: Query optimization, local path 15-QueryOptimization.pdf, 78 PDF pages. The source credits Naeem Mahoto and presents DBMS material from the Data Base and Data Mining Group. Lecture number 15 follows the supplied filename identifier.

Lecture summary

The query optimizer converts SQL into an execution plan that produces the same result efficiently. It checks lexical, syntactic and semantic correctness, builds a relational-algebra representation, applies equivalence transformations, and then compares physical alternatives using data statistics and cost formulas.

Algebraic optimization reduces intermediate results. The most important rules push selections and projections toward the base tables, replace a filtered Cartesian product with a join, and exploit the commutativity and associativity of many operators. Cost-based optimization then chooses how to read tables, the order of joins, the algorithm for each operator and when sorting should happen.

The lecture introduces table profiles, cardinality estimates and query trees. It compares sequential scans, B+-Tree, hash and bitmap access, then develops nested-loop, merge, hash and bitmap join methods. Grouping can be sort-based or hash-based, and materialized views can provide precomputed aggregates.

Learning route

  1. Read Query Optimization Pipeline and Relational Algebra for the optimizer stages and safe rewrites.
  2. Read Query Cost Estimation and Access Selection for statistics, cardinality and scans versus indexes.
  3. Read Join Algorithms and Execution Plan Selection for join methods and plan search.
  4. Revisit Physical Access Structures and Access Methods and DBMS Architecture and Query Processing.

Source map

PDF pagesMaterial
1-15Optimizer role, SQL analysis, relational algebra, cost-based optimization and execution modes
16-38Algebraic equivalences and EMP/DEPT rewrite example
39-45Cost model, table profiles and cardinality estimates
46-55Query trees, scans, sorting, predicate evaluation and bitmap intersections
56-70Join algorithms, bitmap join indexes and grouping
71-78Alternative-plan search, cost selection and approximation

Worked example

For EMP(Emp#, Dept#, Salary) and DEPT(Dept#, DName), the query asking for department names of employees with Salary > 1000 can be improved by filtering EMP first, retaining only Dept#, retaining only Dept# and DName from DEPT, and joining the reduced inputs. The final result is unchanged, but fewer rows and columns flow through the join.

This is an added study explanation based on the source example. The original PDF remains local.

Revision prompts

  • Explain the difference between algebraic optimization and cost-based optimization.
  • Rewrite a filtered Cartesian product as a join and push a selection below that join when valid.
  • Decide when a sequential scan is preferable to an index.
  • Compare nested-loop, merge and hash joins for different table sizes and predicates.
  • Explain how stale statistics can lead to a poor plan.

Source and scope

Naeem Mahoto, Query optimization, local path 15-QueryOptimization.pdf, 78 PDF pages. This note paraphrases the source. The worked query explanation is an added study clarification.