Generated by GPT-5-mini| EXPLAIN (SQL) | |
|---|---|
| Name | EXPLAIN (SQL) |
| Type | SQL statement |
| Introduced | 1986 |
| Related | SELECT, INSERT, UPDATE, DELETE, JOIN, INDEX, OPTIMIZER |
EXPLAIN (SQL) EXPLAIN is a diagnostic SQL statement used to reveal the execution plan chosen by a database query optimizer. It helps database administrators and developers understand how a Structured Query Language statement is transformed into operations that access data stored in systems such as Oracle, Microsoft SQL Server, PostgreSQL, MySQL, SQLite, and MariaDB. EXPLAIN output is commonly used alongside tools and standards developed by organisations like the International Organization for Standardization and projects such as Apache initiatives.
EXPLAIN informs about cost estimates, access methods, join orders, and use of indexes for statements including SELECT (SQL), INSERT (SQL), UPDATE (SQL), and DELETE (SQL). It exposes optimizer decisions influenced by statistics, histograms, and hints provided by vendors such as Oracle and Microsoft. Practitioners at companies like Amazon, Facebook, Google, Netflix, and Uber rely on EXPLAIN to diagnose latency issues, plan schema changes, and validate indexes used by queries against engines like PostgreSQL and MySQL. Research groups at institutions such as Massachusetts Institute of Technology, Stanford University, and University of California, Berkeley have published analyses comparing optimizer behavior revealed via EXPLAIN across engines.
Syntax varies by vendor: typical forms are EXPLAIN SELECT, EXPLAIN ANALYZE, EXPLAIN PLAN FOR, EXPLAIN QUERY PLAN, and EXPLAIN VERBOSE. For example, PostgreSQL supports EXPLAIN and EXPLAIN ANALYZE with options such as BUFFERS and FORMAT; MySQL provides EXPLAIN EXTENDED and EXPLAIN FORMAT=JSON; Oracle uses EXPLAIN PLAN FOR plus DBMS_XPLAN.DISPLAY; SQLite includes EXPLAIN and EXPLAIN QUERY PLAN producing bytecode or human-readable descriptions. Tools from Red Hat, IBM and cloud providers like Microsoft Azure and Google Cloud Platform integrate EXPLAIN output into performance dashboards. Vendor-specific hints, such as those from Oracle or Microsoft, may alter optimizer choices and thus change EXPLAIN output.
EXPLAIN output typically lists nodes like sequential scan, index scan, index-only scan, bitmap index scan, nested loop join, hash join, merge join, sort, aggregate, and materialize. It reports estimated costs, cardinality, and sometimes actual runtimes when combined with ANALYZE. Columns or fields in output map to concepts used in academic work at Carnegie Mellon University and Princeton University: startup cost, total cost, rows, width, and actual time. Visualizers and IDEs from vendors such as JetBrains and projects like pgAdmin render trees and heatmaps derived from EXPLAIN to show operator pipelines and buffer usage. Advanced components include partition pruning, parallel workers, and remote scans for distributed databases like CockroachDB and Amazon Aurora.
Developers use EXPLAIN to detect missing indexes, inefficient join orders, and costly sorts. DBAs examine EXPLAIN output when tuning OLTP workloads at enterprises such as Goldman Sachs, Walmart, and Airbnb. Data engineers apply EXPLAIN before deploying schema migrations or query rewrites in environments run by Spotify or Dropbox. Interpreting EXPLAIN requires understanding optimizer statistics gathering methods used by Oracle, PostgreSQL and MySQL, and correlating output with real-world metrics from observability stacks like Prometheus, Grafana, and Elastic.
Each engine exposes different detail levels: PostgreSQL emphasizes readable tree output and JSON formats, MySQL historically showed simplified rows but added JSON and EXTENDED forms, Oracle stores plans in PLAN_TABLE and integrates with DBMS_XPLAN, while SQL Server offers graphical execution plans in Management Studio and XML plans via SET SHOWPLAN_XML. Distributed systems such as Google Spanner and CockroachDB include cost estimates for network I/O and remote execution. Embedded engines like SQLite produce VM bytecode visible under EXPLAIN, whereas analytical engines such as Apache Hadoop-based systems and Snowflake provide different optimizer diagnostics and execution profiles.
EXPLAIN reports are estimates produced by optimizers that may diverge from observed performance due to stale statistics, parameter sniffing, or non-deterministic factors like concurrent load and caching. EXPLAIN ANALYZE or actual execution profiling reduces this gap but can introduce side effects and longer runtimes; caution is advised in production environments managed by providers like Amazon Web Services, Microsoft Azure, and Google Cloud Platform. Some optimizers apply heuristics or rule-based transformations—documented in literature from ACM and IEEE conferences—that are opaque in EXPLAIN output. Effective tuning combines EXPLAIN with real metrics collected by APM vendors such as New Relic and Datadog and with work by database research groups at University of California, San Diego and ETH Zurich.