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
Intro
SQL Execution Plans and Query Tuning
SQL states the result you need. A database engine still has to decide how to produce that result. It parses the statement, transforms it into relational operations, estimates how many rows each operation returns, compares possible physical strategies, and selects an execution plan. The executor then runs the plan as a tree of operators.
The optimizer's choice depends on the SQL expression, available indexes, table and column statistics, configuration, and engine capabilities. A plan is therefore evidence about one engine, schema, dataset, parameter set, and moment. It is not a permanent property of the SQL text.
From SQL text to rows
Query processing has four useful layers:
- Parse and bind. The engine checks syntax, resolves names, types expressions, and builds a logical representation.
- Rewrite. Rules may simplify predicates, expand views, flatten subqueries, or remove work that cannot affect the result.
- Optimize. The optimizer considers access paths, join orders, join algorithms, aggregation strategies, sorting, and parallelism. A cost model ranks candidate plans.
- Execute. Physical operators request or produce rows. Their measured row counts, timing, memory, and I/O reveal how well the estimates matched reality.
Most plans are trees. Leaf nodes read tables or indexes. Parent nodes filter, join, sort, aggregate, or limit their children's output. Read from the leaves toward the root to follow data flow. Also inspect the root because it states the final operation and total result shape.
Continue the course
This section is part of the paid course.
See pricing to subscribe, or log in if you already have access.
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
