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:
- Parsing & Translation:
- Checks if
instructor,name, andsalaryare valid. -
Translates the query into the relational algebra expression:
$\(\Pi_{\text{name}}(\sigma_{\text{salary} > 50000}(\text{instructor}))\)$ -
Optimization:
- Plan A: Scan the entire table from top to bottom, row by row (Full Table Scan).
- Plan B: Use an existing B+ tree index on
salaryto jump straight to records wheresalary > 50000(Index Scan). -
Decision: If the table has 1,000,000 records, Plan B requires significantly fewer disk I/O operations (āĻĄāĻŋāϏā§āĻ āĻāύāĻĒā§āĻ-āĻāĻāĻāĻĒā§āĻ āĻāĻžāϰā§āϝāĻāϞāĻžāĻĒ). The optimizer selects Plan B.
-
Evaluation:
- The execution engine uses the index to retrieve matching rows and displays the instructor names.
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 (āĻŽāϧā§āϝāĻŦāϰā§āϤ⧠āĻĢāϞāĻžāĻĢāϞ).
