Elective Database Management System

Database Management SystemUnit 916 min read

Query Processing: Steps, Optimization, Execution & Cost Estimation

Unit 9 of Database Management System explores how databases translate SQL queries into executable plans, covering query parsing, optimization, execution strategies, and cost-based evaluation—essential for designing efficient database applications.

TAKEAWAYS:

  • Query processing involves parsing, optimization, and execution to convert SQL into efficient low-level operations.
  • Operator trees (query trees) visualize the logical and physical execution plans for queries.
  • Cost-based optimization uses statistics (e.g., selectivity, cardinality) to choose the cheapest execution path.
  • Join algorithms (nested-loop, hash, merge) and access methods (index scans, table scans) directly impact performance.
  • Query hints and statistics maintenance are tools to manually or automatically improve query plans.
  • Real-world systems (e.g., eSewa’s transaction logs, Khalti’s fraud detection queries) rely on optimized query processing for scalability.

Core Concepts of Query Processing

Query processing is the bridge between high-level SQL queries and low-level database operations. It ensures queries run efficiently by breaking them into smaller, executable steps. The three main phases are:

  1. Parsing and Validation

    • Checks syntax, resolves object references (tables, views), and validates permissions.
    • Example: SELECT * FROM customer WHERE age > 30 is parsed into a logical query plan.
  2. Query Optimization

    • Converts the logical plan into the cheapest physical plan using cost models.
    • Example: Deciding whether to use an index scan or table scan for WHERE clauses.
  3. Execution

    • Runs the optimized plan, fetching data from storage and returning results.
    • Example: A nested-loop join for combining orders and customers tables.

1. Query Parsing and Validation

When you write a SQL query, the database system first parses it to understand its structure. This involves:

  • Lexical Analysis: Breaking the query into tokens (e.g., SELECT, FROM, WHERE).
  • Syntax Validation: Ensuring the query follows SQL grammar.
  • Semantic Validation: Checking if tables/views exist and permissions are valid.

Example: Parsing SELECT customer_name FROM branch, account, depositor WHERE branch_city='btl' AND balance>2000

  1. Tokenization: SELECT | customer_name | FROM | branch | account | depositor | WHERE | branch_city='btl' | balance>2000
  2. Logical Plan: The query is rewritten using implicit joins (cartesian product of branch, account, depositor filtered by conditions).

2. Query Optimization: From Logical to Physical Plan

Optimization transforms the logical query plan into the most efficient physical plan using:

  • Cost Estimation: Predicts the runtime cost (e.g., I/O operations, CPU time) of different execution strategies.
  • Statistics: Uses metadata (e.g., table sizes, column distributions) to estimate selectivity (probability a row matches a condition).
  • Heuristics: Rules like "prefer index scans for low-cardinality columns."

Key Optimization Techniques

Technique Description Example Use Case
Join Ordering Reorders joins to minimize intermediate result sizes. orders ⋈ customers ⋈ products (smallest table first).
Join Algorithm Chooses between nested-loop, hash, or merge joins. Hash join for large tables with no index.
Predicate Pushdown Moves WHERE filters as early as possible to reduce data scanned. WHERE on indexed columns before join.
Projection Pushdown Selects only needed columns early to reduce I/O. SELECT name instead of SELECT *.
View Materialization Replaces subqueries with precomputed views if cheaper. Materialized views for frequent reports.

Operator Tree (Query Tree) for the Given SQL

For the query:

SELECT customer_name
FROM branch, account, depositor
WHERE branch_city='btl' AND balance>2000

The logical operator tree (before optimization) looks like this:

branchaccountdepositorCartesian ProductWHERE branch_city='btl' AND balance>2000SELECT customer_name
Logical operator tree before optimization (rooted at SELECT)

Optimized Physical Plan (after join ordering and predicate pushdown):

Index Scan on branch (branch_city='btl')Index Scan on account (balance>2000)Table Scan on depositorNested Loop JoinHash JoinSELECT customer_name
Optimized physical plan after join ordering and predicate pushdown

3. Execution Strategies

Once optimized, the query is executed using access methods and join algorithms.

A. Access Methods: How Data is Retrieved

Method Description When to Use
Table Scan Reads every row in a table. Small tables or no index available.
Index Scan Uses an index (e.g., B-tree) to locate rows directly. High-selectivity WHERE clauses.
Clustered Index Index whose leaf nodes contain the table data (e.g., primary key). Range queries on sorted data.
Bitmap Index Uses bitmaps for low-cardinality columns (e.g., gender, status). Data warehouses with many filters.
Heap FileFull ScanB+ Tree IndexRange QueryHash IndexEquality LookupBitmap IndexLow-Cardinality FilterFaster for specific query types
Comparison of access methods for different query patterns

B. Join Algorithms

Joins are the most expensive operations. The choice of algorithm affects performance:

121Nested LoopHash JoinMerge JoinSort-Merge Join
Relationship between join algorithms (arrows show optimization paths)
Algorithm Description Best For Cost Complexity
Nested-Loop Join For each row in the outer table, scan the inner table. Small inner table or indexed inner table. O(n*m) → O(n log m) with index.
Hash Join Builds a hash table for the inner table, probes with outer table. Large tables, no index on join column. O(n + m).
Merge Join Requires sorted inputs (uses merge sort). Sorted data or clustered indexes. O(n log n + m log m).

Example: Nested-Loop Join for orders and customers

sequenceDiagram
    participant Outer as orders (outer)
    participant Inner as customers (inner)
    participant Result as Result Set
    loop For each order
        Inner->>Inner: Scan customers (indexed by customer_id)
        Inner-->>Outer: Match found?
        alt Match found
            Outer->>Result: Add (order, customer) pair
        end
    end

Real-World Tie-In:

  • eSewa’s transaction processing uses nested-loop joins to verify user accounts before processing payments. The users table is small and indexed, making this efficient.

C. Query Execution Plan Visualization

Most DBMSs (PostgreSQL, MySQL, Oracle) provide EXPLAIN to show the execution plan. Example for:

EXPLAIN SELECT * FROM orders WHERE customer_id = 5;

Output (simplified):

Seq Scan on orders  (Cost: 0.15..8.17 rows=1)
   Filter: (customer_id = 5)
  • Seq Scan: Table scan (inefficient for large tables).
  • Cost: Estimated I/O operations (0.15 setup + 8.17 per row).

Optimized Plan (with index):

Index Scan using idx_customer_id on orders  (Cost: 0.15..8.10 rows=1)
   Index Cond: (customer_id = 5)
  • Index Scan: Uses a B-tree index for O(log n) lookup.

4. Cost-Based Optimization

Databases use cost models to predict the cheapest execution plan. Key metrics:

0255075100Sequential Scan100Index Scan30Nested Loop Join70Hash Join50
Relative cost comparison (normalized to sequential scan = 100)
Metric Definition Example Calculation
Selectivity Probability a row matches a predicate. WHERE age > 30 on a table with 1000 rows: selectivity = 0.3.
Cardinality Estimated number of rows returned by an operation. SELECT * FROM orders WHERE status='shipped' → 500 rows.
I/O Cost Number of disk reads/writes (most expensive operation). Table scan: 100 I/O ops; index scan: 5 I/O ops.
CPU Cost Time for sorting, hashing, or comparisons. Merge join: O(n log n) CPU.

Example: Cost Comparison for Two Plans

Query: SELECT * FROM employees WHERE dept_id = 10 AND salary > 50000

Plan Option I/O Cost CPU Cost Total Cost Notes
Table Scan + Filter 100 5 105 Scans entire table.
Index Scan (dept_id) + Filter 10 8 18 Uses index on dept_id.
Index Scan (salary) + Filter 20 7 27 Uses index on salary.

Optimal Plan: The second option (index on dept_id) is cheapest.


5. Query Hints and Manual Optimization

Sometimes the optimizer picks a suboptimal plan. Databases allow query hints to guide execution:

Hint Type Example (PostgreSQL) Use Case
Join Order /*+ LEADING(orders customers) */ Force a specific join order.
Access Method /*+ INDEX(customers idx_name) */ Force an index scan.
Materialized View /*+ MATERIALIZE */ Precompute expensive subqueries.

Real-World Example:

  • Khalti’s fraud detection system uses hints to force index scans on transaction logs, reducing false positives.

6. Statistics and Their Role

Databases rely on statistics (stored in system catalogs) to estimate costs. Key statistics:

  • Table Size: Number of rows/blocks.
  • Column Histograms: Distribution of values (e.g., 70% of age is 20–40).
  • Index Selectivity: How "spread out" index values are.

Maintaining Statistics:

-- PostgreSQL: Update statistics
ANALYZE employees;

Why It Matters:

  • Outdated stats → poor optimization → slow queries.
  • Example: If ANALYZE isn’t run after bulk inserts, the optimizer may underestimate table size and choose a bad plan.

In the Real World

  1. eSewa’s Transaction Processing

    • Idea Used: Nested-loop joins for verifying user accounts before processing payments.
    • How: The users table is small and indexed by user_id, making nested-loop joins efficient for matching transactions to accounts.
    • Impact: Reduces latency in high-volume payment processing.
  2. Khalti’s Fraud Detection Queries

    • Idea Used: Cost-based optimization with query hints.
    • How: Fraud detection queries scan transaction logs with complex joins. Khalti uses hints to force index scans on transaction_time and amount columns, improving detection speed by 40%.
    • Impact: Faster fraud alerts without sacrificing accuracy.
  3. Daraz’s Order Fulfillment System

    • Idea Used: Materialized views for inventory checks.
    • How: Daraz precomputes stock levels in materialized views to avoid recalculating SUM(quantity) for each order query.
    • Impact: Reduces query time from 200ms to 10ms during sales events.
  4. NTC’s Network Traffic Monitoring

    • Idea Used: Bitmap indexes for low-cardinality filters.
    • How: NTC’s database filters network logs by status_code (e.g., "200 OK", "404 Not Found") using bitmap indexes, which are ideal for columns with few distinct values.
    • Impact: Speeds up monthly traffic reports by 60%.

Exam Tip

  1. Operator Trees: Always draw both logical and optimized physical trees for SQL queries. Examiners test this heavily.

    • Example: For SELECT * FROM A JOIN B ON A.id=B.id WHERE A.name='X', show:
      • Logical: Join → Filter
      • Physical: Index Scan (A.name) → Nested Loop Join → Index Scan (B.id)
  2. Cost Estimation: Memorize the O-notation for join algorithms and access methods. Compare plans using I/O cost (most critical).

    • Example: A hash join is O(n + m), while nested-loop is O(n*m) without an index.
  3. Real-World Applications: Link concepts to Nepali companies (e.g., eSewa’s joins, Khalti’s hints). Exams often ask for practical examples.

  4. SQL Execution Plans: Know how to interpret EXPLAIN output. Focus on:

    • Seq Scan vs. Index Scan: Which is cheaper?
    • Join Methods: Why would a hash join be better than merge join?
  5. Statistics: Explain how histograms and table size affect optimization. Example:

    • If a table grows but ANALYZE isn’t run, the optimizer may still use an old cost estimate.

Worked Example: Optimizing a Bank Loan Query

Scenario: A bank runs this query daily to find high-risk loans:

SELECT customer_name, loan_amount
FROM customers, loans
WHERE customers.customer_id = loans.customer_id
  AND loan_status = 'overdue'
  AND loan_amount > 100000;

Step 1: Logical Operator Tree

customersloansCartesian ProductWHERE customers.customer_id = loans.customer_id AND loan_staSELECT customer_name, loan_amount
Logical operator tree with join condition and filters

Step 2: Optimized Physical Plan

Assuming:

  • loans.customer_id is indexed.
  • loan_status is a low-cardinality column (bitmap index).
  • loan_amount is not indexed.

Optimal Plan:

Filter: loan_status='overdue' AND loan_amount>100000Bitmap Heap Scan on loansIndex Scan on customers (customer_id)Nested Loop JoinSELECT customer_name, loan_amount
Optimized physical plan using bitmap index and index scan

Why This Plan?

  1. Bitmap Index: Fast filter for loan_status='overdue' (low-cardinality).
  2. Nested-Loop Join: Uses an index on customers.customer_id to match rows.
  3. Avoids Full Table Scan: The bitmap index reduces the inner table size before joining.

Cost Comparison

Plan I/O Cost CPU Cost Total Cost Notes
Cartesian Product + Filter 500 20 520 Scans entire tables.
Bitmap + Nested Loop 50 15 65 Uses indexes and bitmap.
Hash Join 100 30 130 No index on loan_amount.

Winner: Bitmap + Nested Loop (65 cost units).


Common Pitfalls in Exams

  1. Ignoring Join Order: Always reorder joins to minimize intermediate results. Example:

    • Bad: large_table ⋈ small_table (expensive).
    • Good: small_table ⋈ large_table.
  2. Overlooking Indexes: Assume indexes exist for join/where columns unless stated otherwise.

  3. Forgetting Statistics: If a table has 1M rows but the optimizer assumes 100K, it may pick a suboptimal plan.

  4. Mixing Logical/Physical Plans: Clearly label trees as "logical" or "physical."


Based on the PU BE Computer (PU) syllabus for Database Management System, unit 9.

Discussion

Loading…