The database receives a query, parses it, creates possible plans, estimates their costs, picks the cheapest plan, executes it, and returns results.
Execution Sample
DBMS Theory
SELECT * FROM Orders WHERE CustomerID = 5;
-- Optimizer chooses index scan if available
-- Else full table scan
-- Returns matching rows
This query fetches all orders for customer 5; optimizer decides best way to find these rows.
Analysis Table
Step
Action
Details
Result
1
Receive Query
SELECT * FROM Orders WHERE CustomerID = 5;
Query accepted
2
Parse Query
Check syntax and semantics
Query parsed successfully
3
Generate Plans
Plan A: Full table scan
Plan B: Index scan on CustomerID
Two plans created
4
Estimate Cost Plan A
Full scan cost estimated
Cost = 100 units
5
Estimate Cost Plan B
Index scan cost estimated
Cost = 10 units
6
Choose Plan
Compare costs 100 vs 10
Plan B chosen (index scan)
7
Execute Plan
Perform index scan to find CustomerID=5
Rows fetched efficiently
8
Return Results
Send rows to user
Query complete
9
Exit
Query execution finished
End
💡 Query execution ends after results are returned to user.
State Tracker
Variable
Start
After Step 3
After Step 5
Final
Query
Raw SQL string
Parsed query object
Plans with costs
Executed plan result
Plan A Cost
N/A
N/A
100 units
N/A
Plan B Cost
N/A
N/A
10 units
N/A
Chosen Plan
N/A
N/A
Plan B (index scan)
Plan B executed
Key Insights - 3 Insights
Why does the optimizer choose the index scan over the full table scan?
Because the estimated cost for the index scan (10 units) is much lower than the full table scan (100 units), making it faster and more efficient as shown in execution_table rows 4, 5, and 6.
What happens if no index exists on CustomerID?
The optimizer cannot create an index scan plan, so it defaults to the full table scan plan, which has a higher cost but is the only option, as implied in step 3 and 4.
How does cost estimation affect query execution?
Cost estimation helps the optimizer pick the fastest plan by comparing resource usage; incorrect estimates can lead to slower queries, as seen in steps 4, 5, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, which plan does the optimizer choose?
APlan B (Index scan)
BPlan A (Full table scan)
CBoth plans
DNo plan chosen yet
💡 Hint
Check the 'Choose Plan' action and result in execution_table row 6.
At which step does the optimizer estimate the cost of the full table scan?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for 'Estimate Cost Plan A' in execution_table.
If the index on CustomerID was missing, how would the chosen plan change?
ANo plan would be chosen
BPlan A (Full table scan) would be chosen
CPlan B would still be chosen
DA new plan would be created
💡 Hint
Refer to key_moments about what happens if no index exists.
Concept Snapshot
Query Optimization Strategies:
- Receive and parse SQL query
- Generate multiple query plans
- Estimate cost for each plan
- Choose plan with lowest cost
- Execute chosen plan
- Return results
Cost estimation guides efficient query execution.
Full Transcript
Query optimization strategies involve the database system receiving a SQL query, parsing it to understand its structure, and then generating multiple possible ways to execute it called query plans. Each plan is evaluated by estimating the cost, which measures how much time or resources it might take. The optimizer then selects the plan with the lowest estimated cost to run the query efficiently. For example, when querying orders for a specific customer, the optimizer may choose an index scan over a full table scan if it is cheaper. This process helps speed up data retrieval and reduce system load.