Sub topic 15
How EXPLAIN differs in MySQL, SQL Server and others
This chapter assumes you can read a PostgreSQL plan. That means you know what a node is, what estimated versus actual rows mean, and how scans and joins appear in the tree. The goal is to carry those skills to other databases. The ideas transfer well. The syntax, the output layout and some node names do not.
The same ideas under different names
Every mainstream relational database has an optimizer. It builds a tree of physical operators, estimates costs from statistics, and can show you the result. What differs is how you ask for the plan, how it is displayed, and whether it comes with real measurements.
| Concept | PostgreSQL | MySQL 8 | SQL Server | Oracle |
|---|---|---|---|---|
| Estimated plan only | `EXPLAIN` | `EXPLAIN` | `SET SHOWPLAN_XML ON` | `EXPLAIN PLAN FOR` then `DBMS_XPLAN.DISPLAY` |
| Run and measure | `EXPLAIN ANALYZE` | `EXPLAIN ANALYZE` | `SET STATISTICS XML ON` | `GATHER_PLAN_STATISTICS` hint, then `DISPLAY_CURSOR` |
| I/O counts | `BUFFERS` | not in EXPLAIN | `SET STATISTICS IO ON` | `ALLSTATS` format shows buffers |
| Full table scan | Seq Scan | `type: ALL` | Table Scan or Clustered Index Scan | TABLE ACCESS FULL |
| Index lookup | Index Scan | `type: ref`, `eq_ref`, `range` | Index Seek | INDEX RANGE SCAN, INDEX UNIQUE SCAN |
| Hash join | Hash Join | hash join (8.0.18 and later) | Hash Match | HASH JOIN |
| Merge join | Merge Join | not available | Merge Join | MERGE JOIN |
| Sort | Sort | Using filesort | Sort | SORT ORDER BY |
MySQL
MySQL (InnoDB) accepts EXPLAIN in front of a statement, as PostgreSQL does. The default output is a table, not a tree.
EXPLAIN
SELECT c.name, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= '2026-01-01';You get one row per table, with these columns: id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered and Extra. Four of them matter most.
typeis the access method. From best to worst, the common values areconst,eq_ref,ref,range,indexandALL.ALLis a full table scan.indexis a full scan of an index, which is cheaper thanALLonly because an index is usually smaller.keyis the index MySQL chose, andpossible_keyslists the candidates. ANULLkey means no index is used.rowsis the estimated number of rows read from that table.filteredis the estimated percentage of those rows that survive the remaining conditions. Multiply them to estimate rows passed to the next step.Extraholds notes such asUsing where,Using index(a covering index, similar to an index-only scan),Using temporaryandUsing filesort.Using filesortmeans a sort that is not satisfied by an index. It does not necessarily mean the sort goes to disk.
The tabular form is not read inside out. Rows appear in join order, so the first row is the first table accessed, and later rows are joined to it with nested loops.
Tree format and EXPLAIN ANALYZE
Since MySQL 8.0.16 you can ask for a tree that looks much closer to what you know from PostgreSQL. Since 8.0.18, EXPLAIN ANALYZE runs the query and reports actual numbers in that tree format.
EXPLAIN ANALYZE
SELECT c.name, count(*) AS order_count
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= '2026-01-01'
GROUP BY c.name
ORDER BY order_count DESC
LIMIT 10;Each node shows two estimates and two actuals, for example (cost=1042 rows=9800) (actual time=0.05..12.4 rows=10211 loops=1). The two numbers in actual time are the time to the first row and to the last row, in milliseconds, per loop. This matches PostgreSQL, so you multiply by loops to get total time for a node.
Differences that trip people up
- There is no
BUFFERSoption. To see I/O you need other tools, such as theperformance_schematables or theHandler_read%status counters. - There is no merge join. MySQL uses nested loops and, since 8.0.18, hash joins for joins without a usable index.
EXPLAIN ANALYZEactually executes the statement, so the PostgreSQL rule applies: wrap anything that changes data in a transaction you can roll back, or avoid it. Check your version's documentation for which statement types it accepts.- Cost values are in MySQL's own units. They cannot be compared with PostgreSQL costs.
MariaDB differs from MySQL here. It uses ANALYZE SELECT ... for measured plans, and it reports r_rows (actual rows) next to rows (estimated rows).
SQL Server
SQL Server has no EXPLAIN keyword. Plans are usually viewed graphically in SQL Server Management Studio (SSMS) with "Display Estimated Execution Plan" (Ctrl+L) or "Include Actual Execution Plan" (Ctrl+M). In scripts you use SET options. Each SHOWPLAN option must be the only statement in its batch, which is why GO appears below.
SET SHOWPLAN_XML ON;
GO
SELECT c.name, count(*) AS order_count
FROM dbo.customers AS c
JOIN dbo.orders AS o ON o.customer_id = c.id
GROUP BY c.name;
GO
SET SHOWPLAN_XML OFF;
GOWith SHOWPLAN_XML on, the query is not executed. It returns the plan as XML, which SSMS opens as a diagram. SET SHOWPLAN_TEXT ON and SET SHOWPLAN_ALL ON give text forms. To get a plan with actual rows, use SET STATISTICS XML ON, which runs the query.
For I/O and time, use these two options together with the query:
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SELECT c.name, count(*) AS order_count
FROM dbo.customers AS c
JOIN dbo.orders AS o ON o.customer_id = c.id
GROUP BY c.name;The messages tab then shows lines such as Table 'orders'. Scan count 1, logical reads 5120, physical reads 3. Logical reads count pages read from the buffer cache, and physical reads count pages fetched from disk. This is the SQL Server counterpart of shared hit and shared read in PostgreSQL.
Reading differences
- Graphical plans flow right to left. Data starts at the leaf operators on the right and moves to the
SELECToperator on the left. This is the same inside-out order as PostgreSQL, drawn sideways. - Operator names differ. Seq Scan corresponds to Table Scan or Clustered Index Scan. An Index Scan corresponds to an Index Seek when SQL Server can jump to a key range. A Key Lookup is an extra trip to the clustered index for columns the nonclustered index lacks. A Hash Match is a hash join or hash aggregate.
- Table storage is different. Most SQL Server tables are clustered on a key, so the table itself is a B-tree.
- Each operator shows "Estimated Cost %" relative to the whole batch. In an actual plan these percentages are still estimates. Compare "Estimated Number of Rows" with "Actual Number of Rows" in the operator properties, as you would with PostgreSQL's
rowsandactual rows.
Oracle and SQLite
Oracle writes the plan into a table and you display it afterward:
EXPLAIN PLAN FOR
SELECT * FROM orders WHERE customer_id = 42;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The output is an indented operation list with an Id column, such as TABLE ACCESS FULL or INDEX RANGE SCAN. Child operations are indented under their parent, so it reads like PostgreSQL's tree. EXPLAIN PLAN does not run the query, and its plan can differ from the plan used at run time. For real numbers, add the GATHER_PLAN_STATISTICS hint, run the query, then display the plan from the cursor cache:
SELECT /*+ GATHER_PLAN_STATISTICS */ count(*) FROM orders WHERE customer_id = 42;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST'));The columns E-Rows and A-Rows hold estimated and actual rows, which makes misestimates easy to find.
SQLite is much simpler. Plain EXPLAIN there prints the virtual machine's bytecode, which is rarely what you want. Use EXPLAIN QUERY PLAN instead.
EXPLAIN QUERY PLAN
SELECT * FROM orders WHERE customer_id = 42;The output is a short tree with lines such as SEARCH orders USING INDEX orders_customer_id_idx (customer_id=?) or SCAN orders. It has no costs and no row estimates. It tells you only which access path was chosen.
A portable method
Whatever the database, the diagnosis steps from the earlier chapters still apply.
- Get the estimated plan and identify the access method for each table.
- Get an actual plan if you can run the query safely, and compare estimated rows with actual rows.
- Find the operator with the largest share of time or reads.
- Check whether statistics are current, then consider an index or a rewrite.
Statistics maintenance is named differently. PostgreSQL uses ANALYZE. MySQL uses ANALYZE TABLE. SQL Server uses UPDATE STATISTICS. Oracle uses the DBMS_STATS package.
Exercise
A colleague sends you a MySQL EXPLAIN row with type: ALL, key: NULL and rows: 480000 for the table orders. What does it tell you, and what is the PostgreSQL equivalent?
Show answer
type: ALL with no key means MySQL will do a full table scan of orders and expects to read about 480,000 rows. In PostgreSQL this is a Seq Scan. The fix to consider is an index on the filtered or joined column, after checking that the query has a selective condition.
Exercise
In SQL Server you need the actual row counts for a query, and you must not accidentally run a data-modifying batch. Which option shows the plan without running the query, and which one runs it?
Show answer
SET SHOWPLAN_XML ON returns the estimated plan without executing the statements. SET STATISTICS XML ON executes the statements and returns the plan with actual row counts, so only use it on a data-modifying batch inside a transaction you roll back.