0
0
DBMS Theoryknowledge~10 mins

Cost-based optimization in DBMS Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cost-based optimization
Receive SQL Query
Parse Query
Generate Possible Execution Plans
Estimate Cost for Each Plan
Compare Costs
Choose Lowest Cost Plan
Execute Chosen Plan
The database receives a query, creates many ways to run it, estimates the cost for each, and picks the cheapest plan to run.
Execution Sample
DBMS Theory
SELECT * FROM Orders WHERE CustomerID = 5;
The database decides how to get orders for customer 5 with the least cost.
Analysis Table
StepActionDetailsCost EstimateDecision
1Parse QueryIdentify tables and conditions-Proceed
2Generate Plan AFull table scan of OrdersCost = 100Consider
3Generate Plan BUse index on CustomerIDCost = 10Consider
4Compare CostsPlan A vs Plan B-Plan B cheaper
5Choose PlanSelect Plan B-Execute Plan B
6Execute PlanRetrieve matching rows using index-Done
💡 Execution stops after choosing and running the lowest cost plan (Plan B).
State Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
QuerySELECT * FROM Orders WHERE CustomerID = 5;ParsedParsedParsedParsed
Plan A CostN/A100100100100
Plan B CostN/AN/A101010
Chosen PlanNoneNoneNonePlan BPlan B
Key Insights - 3 Insights
Why does the optimizer consider multiple plans instead of just one?
Because different plans can have very different costs. The optimizer compares them all (see execution_table steps 2 and 3) to pick the cheapest.
What does 'cost' mean in cost-based optimization?
Cost is an estimate of resources like time and CPU needed to run a plan. Lower cost means faster or cheaper execution (see execution_table cost estimates).
Why might a full table scan have a higher cost than using an index?
A full scan reads every row, which is slow for large tables. Using an index targets only needed rows, so it costs less (see Plan A vs Plan B costs).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cost estimate for Plan B?
A100
B10
C50
D0
💡 Hint
Check the 'Cost Estimate' column at Step 3 in the execution_table.
At which step does the optimizer decide which plan to execute?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look for the step labeled 'Choose Plan' in the execution_table.
If the index on CustomerID was missing, which plan would likely be chosen?
AA new plan with cost 5
BPlan B with cost 10
CPlan A with cost 100
DNo plan would be chosen
💡 Hint
Refer to the variable_tracker where Plan B cost is only available if index exists.
Concept Snapshot
Cost-based optimization chooses the best way to run a query by:
- Parsing the query
- Generating multiple execution plans
- Estimating the cost of each plan
- Selecting the plan with the lowest cost
- Executing that plan
Cost estimates reflect resource use like time and CPU.
Full Transcript
Cost-based optimization is a process used by databases to run queries efficiently. When a query is received, the database parses it to understand what data is needed. Then, it creates different possible ways to get that data, called execution plans. Each plan is given a cost estimate based on how much time and resources it might take. The optimizer compares these costs and picks the plan with the lowest cost to run. For example, it might choose to use an index instead of scanning the whole table because the index plan costs less. This process helps queries run faster and saves resources.