Title: Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS)
Open Graph Title: Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS)
X Title: Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS)
Description: Learn to read PostgreSQL query plans: EXPLAIN ANALYZE, Seq Scan vs Index Scan, estimated vs actual rows, and spotting a missing index.
Open Graph Description: Learn to read PostgreSQL query plans: EXPLAIN ANALYZE, Seq Scan vs Index Scan, estimated vs actual rows, and spotting a missing index.
X Description: Learn to read PostgreSQL query plans: EXPLAIN ANALYZE, Seq Scan vs Index Scan, estimated vs actual rows, and spotting a missing index.
Keywords:
Opengraph URL: https://sql.coderang.dev/en/blog/sql-explain
Domain: sql.coderang.dev
IPv4 address: 79.137.205.149
{"@context":"https://schema.org","@type":"Organization","@id":"https://sql.coderang.dev/#organization","name":"SQL Arena","url":"https://sql.coderang.dev","logo":{"@type":"ImageObject","url":"https://sql.coderang.dev/android-chrome-192x192.png","width":192,"height":192},"sameAs":["https://t.me/coderang"],"description":"SQL Arena is an interactive SQL training platform — a practice arena, duels, a reference handbook and tutorials for analysts and backend developers."}
{"@context":"https://schema.org","@type":"WebSite","name":"SQL Arena","url":"https://sql.coderang.dev","inLanguage":"en-US","publisher":{"@id":"https://sql.coderang.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":"https://sql.coderang.dev/trainer/tasks?search={search_term_string}","query-input":"required name=search_term_string"}}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS)","description":"Learn to read PostgreSQL query plans: EXPLAIN ANALYZE, Seq Scan vs Index Scan, estimated vs actual rows, and spotting a missing index.","image":{"@type":"ImageObject","url":"https://sql.coderang.dev/og?lang=en&title=Reading%20Query%20Plans%3A%20EXPLAIN%20and%20EXPLAIN%20(ANALYZE%2C%20BUFFERS)","width":1200,"height":630},"datePublished":"2026-06-19T02:55:51.876Z","dateModified":"2026-06-26T02:27:05.000Z","author":{"@type":"Organization","@id":"https://sql.coderang.dev/#organization","name":"SQL Arena"},"publisher":{"@type":"Organization","@id":"https://sql.coderang.dev/#organization","name":"SQL Arena","url":"https://sql.coderang.dev","logo":{"@type":"ImageObject","url":"https://sql.coderang.dev/android-chrome-192x192.png","width":192,"height":192},"sameAs":["https://t.me/coderang"],"description":"SQL Arena is an interactive SQL training platform — a practice arena, duels, a reference handbook and tutorials for analysts and backend developers."},"mainEntityOfPage":{"@type":"WebPage","@id":"https://sql.coderang.dev/en/blog/sql-explain"},"inLanguage":"en-US","wordCount":665,"timeRequired":"PT3M","articleSection":"sql"}
{"@context":"https://schema.org","@type":"BreadcrumbList","inLanguage":"en-US","itemListElement":[{"@type":"ListItem","position":1,"name":"SQL Arena","item":"https://sql.coderang.dev/en"},{"@type":"ListItem","position":2,"name":"Blog","item":"https://sql.coderang.dev/en/blog"},{"@type":"ListItem","position":3,"name":"Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS)","item":"https://sql.coderang.dev/en/blog/sql-explain"}]}
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en-US","mainEntity":[{"@type":"Question","name":"EXPLAIN vs EXPLAIN ANALYZE","acceptedAnswer":{"@type":"Answer","text":"EXPLAIN only estimates the plan without running the query. EXPLAIN ANALYZE actually executes it (careful with INSERT/UPDATE/DELETE!) and attaches real numbers. A typical top line of a plan: What matters here: - cost=0.00..1834.00 — estimated cost in arbitrary units: startup cost and total cost. These are not milliseconds. - rows=120 — how many rows the planner expects . - actual time=... and actual rows=118 — what really happened. - loops=1 — how many times the node ran (crucial inside nested loops). To run EXPLAIN ANALYZE on a mutating query without side effects, wrap it in a transaction:"}},{"@type":"Question","name":"Seq Scan vs Index Scan","acceptedAnswer":{"@type":"Answer","text":"A Seq Scan (sequential scan) reads the whole table row by row. That's not always bad : if you need most of the table, reading it straight through is cheaper than hopping around an index. An Index Scan uses an index to pinpoint matching rows. It wins when the predicate selects a small fraction of the table (a selective filter). There's also Bitmap Heap Scan — a middle ground: PostgreSQL collects index matches into a bitmap, then reads the table in physical-order batches. The planner picks it for \"medium\" selectivity — hundreds to thousands of rows. Resist the urge to \"fix\" the planner with hints — feed it fresh statistics instead: Gotcha: MySQL also has EXPLAIN ANALYZE (since 8.0), but the ou"}},{"@type":"Question","name":"Estimated vs actual rows","acceptedAnswer":{"@type":"Answer","text":"The single most valuable skill is comparing rows (estimate) with actual rows (reality). A large gap means the planner had bad statistics and may have chosen a poor plan. If you see something like: — that's a red flag. The planner expected 5 rows and picked a Nested Loop, but got 48000. At that volume a Hash Join would have been far cheaper. Common causes of the gap: - stale statistics — fixed by ANALYZE; - correlated columns (e.g. city and country) — helped by CREATE STATISTICS; - complex expressions in the filter that have no statistics. Read estimates bottom-up : an error in a leaf node balloons through the whole join tree."}}]}
| theme-color | #0a0a0a |
| application-name | SQL Arena |
| author | SQL Arena |
| creator | SQL Arena |
| publisher | SQL Arena |
| googlebot | index, follow, max-image-preview:large, max-snippet:-1 |
| google-site-verification | EaorZzgqTO4qTFsEEiRorcvcDZOqzyHpTwUJwsCjq_Q |
| og:site_name | SQL Arena |
| og:locale | en_US |
| og:image | https://sql.coderang.dev/og?lang=en&title=Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS) |
| og:image:width | 1200 |
| og:image:height | 630 |
| og:image:alt | Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS) |
| og:image:type | image/png |
| og:type | article |
| article:published_time | 2026-06-19T02:55:51.876Z |
| article:modified_time | 2026-06-26T02:27:05.000Z |
| article:tag | indexes |
| twitter:card | summary_large_image |
| twitter:image | https://sql.coderang.dev/og?lang=en&title=Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS) |
| twitter:image:alt | Reading Query Plans: EXPLAIN and EXPLAIN (ANALYZE, BUFFERS) |
Links:
| Server | nginx |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | s-maxage=300, stale-while-revalidate |
Viewport: width=device-width, initial-scale=1, maximum-scale=5, viewport-fit=cover
Robots: index, follow