SQL Execution Plans and Query Tuning
An SQL execution plan is the database engine's chosen sequence of scans, joins, sorts, and other operations for a query. Query tuning compares the optimizer's estimates with measured work so you can change SQL, indexes, statistics, or data design for a faster and more stable plan.
itDatabases and data storage | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Don't Panic
Don't Panic: SQL Execution Plans and Query Tuning
An execution plan is the database engine's account of how it intends to get your SQL result: scans at the leaves, joins and sorts in the middle, and a result at the root. SQL asks for a result, which is admirably noncommittal. The engine must still move rows, and it must choose how without holding a committee meeting for every possible plan.
The useful idea is cardinality, the number of rows an operator expects to produce. That estimate helps choose an access path, join order, join type, and memory allocation. If the estimate goes wrong early, later choices inherit the bad news with the grim determination of office paperwork reproducing itself. Actual plans let you compare that prediction with rows, loops, timing, buffers, and spills observed during execution.
A scan is not guilty because it is a scan, and a nested loop is not guilty because it has a worrying name. A sequential scan can suit a query that needs much of a table. A nested loop can suit a small outer input and a selective inner probe. The surprise is that optimizer cost is not elapsed time: it is an engine-specific score for comparing candidates, not a stopwatch wearing a tiny database hat.
Start with the first serious difference between estimated and actual rows. Then look for repeated inner work, broad reads followed by selective filters, and sorts or hashes that spill. Those clues point back to statistics, predicates, indexes, data shape, or parameter values. Change one cause at a time, preserve the baseline, and measure again. Otherwise the plan changes, the query changes, the cache changes, and everyone receives a splendid mystery.
The Reference tab explains engine-specific plan formats and statistics. Slides give the row-flow map when a tree starts looking botanical. Cheatsheet is the compact list of operator signals, index checks, and safe experiment rules. The Quiz tests whether a plan observation supports a conclusion. For hands-on practice, use the Practice and Exercise tabs with a disposable PostgreSQL database, then return to the plan with evidence instead of optimism.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://www.postgresql.org/docs/current/using-explain.html
Supports
- Plan trees
- costs
- scans
- joins
- EXPLAIN ANALYZE execution
- actual rows
- loops
- buffers
- and data-modification caveats
- https://www.postgresql.org/docs/current/planner-stats-details.html
Supports
- Row estimation
- selectivity
- functional dependencies
- multivariate distinct counts
- and most-common-value lists
- https://www.postgresql.org/docs/current/indexes.html
Supports
- Index types
- multicolumn order
- expression and partial indexes
- covering scans
- and index overhead
- https://dev.mysql.com/doc/refman/8.4/en/explain.html
Supports
- MySQL EXPLAIN syntax and formats
- EXPLAIN ANALYZE execution
- iterator timing
- rows
- and loops
- https://dev.mysql.com/doc/refman/8.4/en/using-explain.html
Supports
- Join order inspection
- index diagnosis
- optimizer trace boundary
- and statistics refresh guidance
- https://dev.mysql.com/doc/refman/8.4/en/optimization-indexes.html
Supports
- Index lookup benefits and storage and data-change costs
- https://learn.microsoft.com/en-us/sql/relational-databases/query-processing-architecture-guide?view=sql-server-ver17
Supports
- Parsing
- optimization
- cost-based choices
- statistics
- physical operators
- estimated and actual plans
- live statistics
- and plan caching
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/display-the-estimated-execution-plan?view=sql-server-ver17
Supports
- Estimated plans do not execute and lack runtime measurements
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/display-an-actual-execution-plan?view=sql-server-ver17
Supports
- Actual plans execute and include runtime context
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/cardinality-estimation-sql-server?view=sql-server-ver17
Supports
- Cost-based optimization
- histograms
- cardinality errors
- correlation
- expressions
- unknown values
- and Query Store investigation
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/parameter-sensitive-plan-optimization?view=sql-server-ver17
Supports
- Parameter values
- plan variants
- caching
- and parameter-sensitive behavior
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/compare-execution-plans?view=sql-server-ver17
Supports
- Side-by-side plan comparison for query rewrites
- index changes
- and Query Store plans
- https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/EXPLAIN-PLAN.html
Supports
- Oracle EXPLAIN PLAN statement and plan-table behavior
- https://research.ibm.com/publications/access-path-selection-in-a-relational-database-management-system
Supports
- Research check for the early cost-based optimizer arc
- https://dev.mysql.com/doc/refman/8.0/en/explain.html
Supports
- MySQL 8.0.18 introduction of EXPLAIN ANALYZE
- https://learn.microsoft.com/en-us/shows/datadriven-sqlserver2016/querystore
Supports
- SQL Server 2016 introduction of Query Store for historical plans
- https://www.postgresql.org/docs/release/
Supports
- PostgreSQL release archive checked for EXPLAIN milestones
- https://github.com/sindresorhus/awesome
Supports
- Required discovery starting point and link to the PostgreSQL list
- https://github.com/dhamaniasad/awesome-postgres
Supports
- Discovery of PEV2
- pgMustard
- pg_flame
- PgHero
- HypoPG
- and pgplan
- https://github.com/dalibo/pev2
Supports
- PostgreSQL plan visualization
- hosted use
- local use
- and project licensing
- https://www.pgmustard.com/docs
Supports
- Plan submission
- timing bar
- tree
- rows
- operation details
- tips
- and supported EXPLAIN options
- https://github.com/mgartner/pg_flame
Supports
- EXPLAIN ANALYZE JSON input
- flame-graph HTML output
- relative node time
- and Apache licensing
- https://github.com/ankane/pghero
Supports
- PostgreSQL performance dashboard
- slow-query discovery
- EXPLAIN use
- and index suggestions
- https://hypopg.readthedocs.io/
Supports
- Hypothetical indexes
- supported access methods
- index creation
- hiding
- and planner experiments
- https://github.com/JacobArthurs/pgplan
Supports
- Command-line PostgreSQL plan comparison and analysis
- https://pganalyze.com/docs/explain
Supports
- Automatic plan collection
- tree and grid visualization
- plan comparison
- misestimate
- scan
- sort
- and hash insights
- https://docs.datadoghq.com/database_monitoring/
Supports
- Historical query metrics
- explain plans
- host metrics
- bottleneck inspection
- and plan changes
- https://dev.mysql.com/doc/workbench/en/wb-performance-explain.html
Supports
- MySQL extended JSON visual explain
- raw
- traditional
- and visual plan views
- https://dbeaver.com/docs/dbeaver/Query-Execution-Plan/
Supports
- Cross-database plan support
- graph and table views
- expensive path highlighting
- cost
- rows
- and execution flow
- https://www.jetbrains.com/help/datagrip/query-execution-plan.html
Supports
- Estimated and runtime plan actions
- supported engines
- raw copies
- tree diagrams
- and flame graphs
- https://learn.microsoft.com/en-us/ssms/sql-server-management-studio-ssms
Supports
- SQL Server Management Studio product identity and destination
- https://www.oracle.com/database/sqldeveloper/
Supports
- Oracle SQL Developer product identity and destination
- https://pganalyze.com/blog/introducing-postgres-query-tuning-workbooks
Supports
- Practitioner workflow for parameter-specific baselines, query variants, plan comparison, and validation before deployment
- https://pganalyze.com/blog/understanding-how-to-compare-postgres-explain-plans
Supports
- Practitioner account of parameter-driven plan variation and separate comparison of plan structure, runtime, and buffer metrics
- https://pganalyze.com/blog/5mins-postgres-create-statistics
Supports
- Practitioner example of PostgreSQL extended statistics and ANALYZE correcting estimates for dependent columns
