Sub topic 15
How EXPLAIN Differs in MySQL, SQL Server and Others
Assume you can read a PostgreSQL plan and understand SELECT, joins, filters, grouping, and indexes. The same planning ideas exist in other relational databases, but the commands, node names, and cost units differ.
The same questions, different interfaces
Every optimizer answers similar questions:
- Which table access method should it use?
- Which join order and join algorithm should it choose?
- How many rows will each step produce?
- How much work will each plan require?
The output is not interchangeable. A cost of 100 in PostgreSQL does not mean the same amount of work as a cost of 100 in SQL Server. Compare costs only within the same database system and configuration.
flowchart LR
Q[SQL statement] --> P[Optimizer plan]
P --> N[Plan nodes]
N --> E[Estimated work]
N --> A[Actual execution details]MySQL
MySQL uses EXPLAIN before a query:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;Traditional output uses columns such as these:
| MySQL column | Meaning |
|---|---|
| `type` | Access method, such as `const`, `ref`, `range`, or `ALL` |
| `possible_keys` | Indexes that might help |
| `key` | Index actually chosen |
| `rows` | Estimated rows examined |
| `Extra` | Additional details, such as filtering or sorting |
A type of ALL usually means a full table scan. It may be reasonable for a small table or a query that returns most rows. A ref access commonly means an indexed equality lookup.
MySQL 8.0.18 and later also support an executing form:
EXPLAIN ANALYZE
SELECT *
FROM orders
WHERE customer_id = 42;This reports actual timing and row counts in a tree format. Unlike plain EXPLAIN, it runs the statement. Do not use it casually on INSERT, UPDATE, or DELETE statements.
EXPLAIN FORMAT=JSON provides more detail, including cost information and nested plan properties. MySQL names some operations differently from PostgreSQL. For example, it may report a table scan where PostgreSQL reports a sequential scan.
SQL Server
SQL Server exposes estimated plans through SET SHOWPLAN_TEXT ON, SET SHOWPLAN_XML ON, or the graphical estimated plan in SQL Server Management Studio. These modes compile the statement without executing it.
For runtime information, enable an actual execution plan in the client, or use runtime statistics such as:
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SELECT *
FROM dbo.Orders
WHERE customer_id = 42;
SET STATISTICS TIME OFF;
SET STATISTICS IO OFF;The graphical plan includes operators such as Index Seek, Index Scan, Table Scan, Nested Loops, Hash Match, and Merge Join.
An Index Seek usually targets a selective part of an index. An Index Scan reads a larger portion of it. A Key Lookup fetches additional columns from the base table after an index found matching rows. Many lookups can indicate that a covering index or a rewritten query would help.
SQL Server reports estimated and actual row counts, operator costs, logical reads, and sometimes warnings such as spills to temporary storage. Its percentage cost shown for an operator is relative to that plan, not a universal performance measurement.
Other databases
Oracle commonly uses EXPLAIN PLAN FOR followed by DBMS_XPLAN.DISPLAY. To inspect a plan from a statement that actually ran, Oracle can use cursor and runtime statistics.
SQLite provides EXPLAIN QUERY PLAN. Its output is intentionally compact and focuses on whether SQLite scans a table or searches an index. It does not provide the same optimizer tree and runtime detail as PostgreSQL.
The comparison is useful:
| Database | Estimated plan | Runtime plan or statistics |
|---|---|---|
| PostgreSQL | `EXPLAIN` | `EXPLAIN ANALYZE`, often with `BUFFERS` |
| MySQL | `EXPLAIN` | `EXPLAIN ANALYZE` in supported versions |
| SQL Server | Estimated execution plan | Actual plan, `STATISTICS IO`, and `STATISTICS TIME` |
| Oracle | `EXPLAIN PLAN` | Cursor plan and runtime statistics |
| SQLite | `EXPLAIN QUERY PLAN` | Limited built-in runtime detail |
When moving between systems, map “sequential scan,” “table scan,” and “scan” to the same broad idea: reading many rows without using a selective lookup. Then check the system’s documentation for the exact operator behavior.
Exercise
You move a query from PostgreSQL to SQL Server. Its PostgreSQL cost is 250, while its SQL Server cost is 40. Can you conclude that the SQL Server query is faster?
Show answer
No. Cost units are optimizer-specific. Compare actual elapsed time, row counts, reads, and execution conditions within each database.