Skip to content

Query Processing

Query Processing

Query Processing refers to the entire sequence of activities (āϏāĻžāĻŽāĻ—ā§āϰāĻŋāĻ• āĻ•āĻ°ā§āĻŽāĻ•āĻžāĻŖā§āĻĄā§‡āϰ āϧāĻžāϰāĻžāĻŦāĻžāĻšāĻŋāĻ•āϤāĻž) involved in translating a high-level query (like an SQL statement) into low-level instructions that the physical database engine can execute to fetch the required data.

Key Steps in Query Processing (āϕ⧁āϝāĻŧ⧇āϰāĻŋ āĻĒā§āϰāĻ•ā§āϰāĻŋāϝāĻŧāĻžāĻ•āϰāϪ⧇āϰ āĻĒā§āϰāϧāĻžāύ āϧāĻžāĻĒāϏāĻŽā§‚āĻš)

When you write and run an SQL query, the database management system (DBMS) processes it through three core phases:

1. Parsing and Translation (āϏāĻŋāύāĻŸā§āϝāĻžāĻ•ā§āϏ āĻŦāĻŋāĻļā§āϞ⧇āώāĻŖ āĻ“ āϰ⧂āĻĒāĻžāĻ¨ā§āϤāϰ)

  • Syntax Checking (āĻŦā§āϝāĻžāĻ•āϰāĻŖāĻ—āϤ āĻ¤ā§āϰ⧁āϟāĻŋ āϝāĻžāϚāĻžāχ): The query parser (āĻĒāĻžāĻ°ā§āϏāĻžāϰ/āĻŦāĻŋāĻļā§āϞ⧇āώāĻ•) inspects the SQL query to ensure it follows the correct syntax rules (āϝ⧇āĻŽāύ: āĻŦāĻžāύāĻžāύ āϭ⧁āϞ āĻŦāĻž āĻŦā§āĻ°ā§āϝāĻžāϕ⧇āϟ āĻŽāĻŋāϏāĻŋāĻ‚ āφāϛ⧇ āĻ•āĻŋ āύāĻž).
  • Semantic Verification (āĻ…āĻ°ā§āĻĨāĻ—āϤ āϝāĻĨāĻžāĻ°ā§āĻĨāϤāĻž āϝāĻžāϚāĻžāχ): The system consults the Data Dictionary to verify whether the specified tables, attributes (columns), and access permissions actually exist.
  • Internal Representation (āĻ…āĻ­ā§āϝāĻ¨ā§āϤāϰ⧀āĻŖ āϰ⧂āĻĒāĻžāĻ¨ā§āϤāϰ): The parser converts the SQL text into an internal representation, typically a Relational Algebra Expression (āϰāĻŋāϞ⧇āĻļāύāĻžāϞ āĻ…ā§āϝāĻžāϞāĻœā§‡āĻŦā§āϰāĻž āϏāĻŽā§€āĻ•āϰāĻŖ āĻŦāĻž āĻŸā§āϰāĻŋ āĻ•āĻžāĻ āĻžāĻŽā§‹).

2. Query Optimization (āϕ⧁āϝāĻŧ⧇āϰāĻŋ āĻ…āĻĒā§āϟāĻŋāĻŽāĻžāχāĻœā§‡āĻļāύ / āϏāĻ°ā§āĻŦā§‹āĻ¤ā§āϤāĻŽ āĻ•āĻ°ā§āĻŽāĻĒāĻĻā§āϧāϤāĻŋ āύāĻŋāĻ°ā§āϧāĻžāϰāĻŖ)

A single relational algebra expression can be evaluated (āϏāĻŽā§āĻĒāĻžāĻĻāύ āĻ•āϰāĻž) using many mathematically equivalent (āϏāĻŽāϤ⧁āĻ˛ā§āϝ) plans. However, some execution paths take seconds while others take hours.

  • Generating Execution Plans (āϏāĻŽā§āĻĒāĻžāĻĻāύ āĻĒāϰāĻŋāĻ•āĻ˛ā§āĻĒāύāĻž āϤ⧈āϰāĻŋ): The query optimizer generates multiple alternative strategies, called Evaluation Plans or Query Execution Plans (āϕ⧁āϝāĻŧ⧇āϰāĻŋ āϏāĻŽā§āĻĒāĻžāĻĻāύ⧇āϰ āĻŦāĻŋāĻ•āĻ˛ā§āĻĒ āύāĻ•āĻļāĻž).
  • Cost Estimation (āϏāĻŽā§āĻ­āĻžāĻŦā§āϝ āĻ–āϰāϚ āĻŦāĻž āĻŦā§āϝāϝāĻŧ⧇āϰ āĻ…āύ⧁āĻŽāĻžāύ): The optimizer estimates the resource cost of each plan based on:
  • Number of disk block transfers (āĻĄāĻŋāĻ¸ā§āĻ• āĻĨ⧇āϕ⧇ āĻĄā§‡āϟāĻž āĻŦā§āϞāϕ⧇āϰ āφāĻĻāĻžāύ-āĻĒā§āϰāĻĻāĻžāύ).
  • Number of disk seeks (āĻšā§‡āĻĄ āĻŽā§āĻ­āĻŽā§‡āĻ¨ā§āĻŸā§‡āϰ āϏāĻŽāϝāĻŧ).
  • CPU processing time (āĻĒā§āϰāϏ⧇āϏāϰ⧇āϰ āĻ•āĻžāĻœā§‡āϰ āϏāĻŽāϝāĻŧ).

  • Selecting the Best Plan: It picks the plan with the lowest estimated cost (āϏāĻ°ā§āĻŦāύāĻŋāĻŽā§āύ āĻ–āϰāĻšā§‡āϰ āϏāĻ°ā§āĻŦā§‹āĻ¤ā§āϤāĻŽ āĻĒāϰāĻŋāĻ•āĻ˛ā§āĻĒāύāĻž) to execute.

3. Evaluation / Execution Engine (āĻŦāĻžāĻ¸ā§āϤāĻŦāĻžāϝāĻŧāύ āĻŦāĻž āϏāĻŽā§āĻĒāĻžāĻĻāύ āχāĻžā§āϜāĻŋāύ)

  • Plan Execution (āĻĒāϰāĻŋāĻ•āĻ˛ā§āĻĒāύāĻž āĻŦāĻžāĻ¸ā§āϤāĻŦāĻžāϝāĻŧāύ): The query execution engine takes the chosen optimal evaluation plan, executes the underlying low-level code, and interacts with the Storage Manager.
  • Returning Results (āĻĢāϞāĻžāĻĢāϞ āĻĒā§āϰāĻĻāĻžāύ): The required data records are fetched (āϤ⧁āϞ⧇ āφāύāĻž) from the physical storage disks into main memory (RAM) and returned to the user or client application.

Concrete Example (āĻŦāĻžāĻ¸ā§āϤāĻŦ āωāĻĻāĻžāĻšāϰāĻŖ)

Consider this SQL query:

SELECT name 
FROM instructor 
WHERE salary > 50000;
  1. Parsing & Translation:
  2. Checks if instructor, name, and salary are valid.
  3. Translates the query into the relational algebra expression:
    $\(\Pi_{\text{name}}(\sigma_{\text{salary} > 50000}(\text{instructor}))\)$

  4. Optimization:

  5. Plan A: Scan the entire table from top to bottom, row by row (Full Table Scan).
  6. Plan B: Use an existing B+ tree index on salary to jump straight to records where salary > 50000 (Index Scan).
  7. Decision: If the table has 1,000,000 records, Plan B requires significantly fewer disk I/O operations (āĻĄāĻŋāĻ¸ā§āĻ• āχāύāĻĒ⧁āϟ-āφāωāϟāĻĒ⧁āϟ āĻ•āĻžāĻ°ā§āϝāĻ•āϞāĻžāĻĒ). The optimizer selects Plan B.

  8. Evaluation:

  9. The execution engine uses the index to retrieve matching rows and displays the instructor names.

example.sql

B+ Tree

Measures of Query Cost (āϕ⧁āϝāĻŧ⧇āϰāĻŋ āĻŦā§āϝāϝāĻŧ⧇āϰ āĻĒāϰāĻŋāĻŽāĻžāĻĒāĻ•āϏāĻŽā§‚āĻš)

Database performance is primarily constrained by physical secondary storage rather than CPU speed. Therefore, query cost is dominated by:

  • Disk I/O Operations (āĻĄāĻŋāĻ¸ā§āϕ⧇āϰ āχāύāĻĒ⧁āϟ-āφāωāϟāĻĒ⧁āϟ): Reading and writing blocks on physical storage drives.
  • Disk Seek Time (āĻšā§‡āĻĄ āĻŽā§āĻ­āĻŽā§‡āĻ¨ā§āĻŸā§‡ āĻŦā§āϝāϝāĻŧāĻŋāϤ āϏāĻŽāϝāĻŧ): The mechanical delay involved in positioning the disk head on traditional HDDs.
  • Memory Constraints (āĻŽā§‡āĻŽā§‹āϰāĻŋāϰ āϏ⧀āĻŽāĻžāĻŦāĻĻā§āϧāϤāĻž): How much temporary buffer space (RAM) is available to hold intermediate results (āĻŽāĻ§ā§āϝāĻŦāĻ°ā§āϤ⧀ āĻĢāϞāĻžāĻĢāϞ).