0
0
SQLquery~10 mins

EXPLAIN plan for query analysis in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - EXPLAIN plan for query analysis
Start: Query Submitted
Parse Query
Generate Execution Plan
Show Plan Details
Analyze Plan for Optimization
The EXPLAIN command takes a SQL query, parses it, creates a plan for how the database will run it, then shows this plan so you can understand and improve the query.
Execution Sample
SQL
EXPLAIN SELECT * FROM employees WHERE department = 'Sales';
This command shows how the database plans to find employees in the Sales department.
Execution Table
StepActionDetailsResult
1Parse QueryCheck syntax and structureQuery is valid
2Generate PlanDecide scan type (e.g., full table scan or index scan)Plan created with full table scan
3Estimate CostCalculate cost and rows expectedCost=100, Rows=50
4Show PlanDisplay plan stepsOutput plan with scan type and filters
5EndPlan ready for reviewExecution plan displayed
💡 Plan generation completes after showing the execution steps and cost estimates.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
QueryRaw SQL textParsed query treeExecution plan structureCost and row estimates addedPlan ready for display
Plan DetailsNoneNoneScan type: full table scanCost=100, Rows=50Displayed to user
Key Moments - 2 Insights
Why does EXPLAIN show a full table scan instead of using an index?
Because the query condition or table statistics suggest scanning the whole table is cheaper; see execution_table step 2 where scan type is chosen.
What does the cost number in the plan mean?
It estimates how much work the database expects to do; lower cost means faster query. This is shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the action in step 3?
AEstimate Cost
BShow Plan
CParse Query
DGenerate Plan
💡 Hint
Check the 'Action' column in execution_table row with Step 3.
At which step does the plan get displayed to the user?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'Show Plan' action in execution_table.
If the query had an index on department, which step would most likely change?
AStep 1: Parse Query
BStep 2: Generate Plan
CStep 3: Estimate Cost
DStep 5: End
💡 Hint
Scan type choice happens in step 2, see execution_table.
Concept Snapshot
EXPLAIN shows how the database plans to run a query.
It parses the query, creates a plan, estimates cost, and displays steps.
Use it to find slow parts and improve queries.
Key parts: scan type, cost, rows estimated.
Run: EXPLAIN SELECT ...
Full Transcript
The EXPLAIN plan command helps you see how a database will run your SQL query. When you run EXPLAIN with a query, the database first checks the query's syntax and structure. Then it creates an execution plan, deciding how to find the data, like whether to scan the whole table or use an index. It estimates the cost and number of rows it expects to process. Finally, it shows you this plan so you can understand and optimize your query. For example, if EXPLAIN shows a full table scan, it means the database thinks scanning all rows is cheaper than using an index. The cost number tells you how much work the database expects to do; lower is better. By reading the EXPLAIN output, you can find slow parts and improve your SQL queries.