EXPLAIN (Show the Query Plan)

Last updated: 2026-11-04

EXPLAIN (Show the Query Plan)

EXPLAIN shows the plan the planner picked for a statement: sequential scan or index scan, estimated row counts, and costs. It does not run the query, so it costs almost nothing, which makes it the first stop when investigating a slow query. Adding ANALYZE actually executes the statement and reports real timings.

Kindcommand
Example LanguageSQL

📝 Syntax

EXPLAIN [ ( <option> [, ...] ) ] <statement>

-- common options
-- ANALYZE   run the statement and report actual timings
-- BUFFERS   show shared buffer hit statistics
-- VERBOSE   print full output-column information
-- FORMAT TEXT | JSON | YAML

⚙️ Parameters

ANALYZEExecutes the statement and reports actual timings
BUFFERSShows buffer hit and read statistics
VERBOSEPrints the full output-column list
FORMATOutput format: TEXT, JSON, or YAML

Returns:No return value. The plan is printed as a text table.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

DROP TABLE
CREATE TABLE
INSERT 0 1000
ANALYZE
                       QUERY PLAN                       
--------------------------------------------------------
Seq Scan on orders  (cost=0.00..19.50 rows=667 width=17)
  Filter: (amount > '500'::numeric)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhat privileges does EXPLAIN need?
AYou need SELECT on the tables involved; EXPLAIN ANALYZE runs the statement for real and therefore needs the corresponding write privileges on writable tables.
QWhy do the EXPLAIN costs not match the real elapsed time?
ACosts are the planner's relative unit for comparing plans, not milliseconds. For real elapsed time, read the actual time reported by EXPLAIN ANALYZE.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏