0Pricing
PostgreSQL Performance & Query Optimization · Lektion

EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen

Lernen Sie, die Kostenangaben, geschätzten Zeilen und Breitenwerte zu interpretieren, die EXPLAIN jedem Plan-Knoten zuordnet, und erkennen Sie, wann die Schätzungen des Planers falsch sind.

EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen ist eine kostenlose PostgreSQL Performance & Query Optimization-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des PostgreSQL Performance & Query Optimization-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der PostgreSQL Performance & Query Optimization-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What the Cost Numbers Mean

Every node in an EXPLAIN plan shows a cost=startup..total pair. These are arbitrary planner units, not milliseconds. The planner uses them only to compare alternative plans and pick the cheapest one.

Startup vs Total Cost

The two numbers tell different stories:

  • Startup cost: work before the first row is returned (e.g. building a hash table)
  • Total cost: work to return all rows

A node that must sort or aggregate everything has a high startup cost.

A Sample Plan

Run EXPLAIN on a simple query and read the cost on each line.

EXPLAIN SELECT * FROM orders WHERE total > 100;

The rows Estimate

Each node shows rows=N, the planner's estimated number of rows it will emit. The planner derives this from table statistics gathered by ANALYZE.

Bad estimates lead to bad plans, so this number matters a lot.

The width Value

The width=N field is the estimated average row size in bytes. Multiplied by rows it estimates memory and I/O needs, which influences whether sorts or hashes spill to disk.

Estimated vs Actual

EXPLAIN alone shows only estimates. Add ANALYZE to also run the query and see real timings and real row counts side by side.

EXPLAIN ANALYZE SELECT * FROM orders WHERE total > 100;

Spotting Estimate Errors

Compare rows (estimate) with actual rows in EXPLAIN ANALYZE output. A large gap — say estimate 10 but actual 100000 — signals stale or insufficient statistics.

Why Estimates Drift

Estimates go stale when:

  • Data changed a lot since the last ANALYZE
  • Columns are correlated and the planner assumes independence
  • The default statistics target is too low for skewed data

Refreshing Statistics

Run ANALYZE to recompute statistics for a table. Better stats produce better row estimates and therefore better plans.

ANALYZE orders;

Raising the Statistics Target

For columns with skewed distributions, increase how many distinct values are sampled. A higher target means more accurate estimates at the cost of slightly slower ANALYZE.

ALTER TABLE orders ALTER COLUMN total SET STATISTICS 500;
ANALYZE orders;

Cost Multipliers in postgresql.conf

Constants like seq_page_cost, random_page_cost, and cpu_tuple_cost scale how the planner weighs disk vs CPU. On SSDs, lowering random_page_cost makes index scans look cheaper.

Quick Check

Test your understanding of cost estimates.

Recap

You learned to read EXPLAIN's numbers:

  • Cost is in arbitrary units; startup vs total cost differ
  • rows and width are estimates from statistics
  • Compare estimated vs actual rows to spot bad stats
  • Fix drift with ANALYZE and a higher statistics target
  • Cost constants tune disk-vs-CPU weighting

Häufig gestellte Fragen

Ist die Lektion „EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen“ kostenlos?

Ja — der vollständige Text von „EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des PostgreSQL Performance & Query Optimization-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der PostgreSQL Performance & Query Optimization-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen“?

Lernen Sie, die Kostenangaben, geschätzten Zeilen und Breitenwerte zu interpretieren, die EXPLAIN jedem Plan-Knoten zuordnet, und erkennen Sie, wann die Schätzungen des Planers falsch sind. Du übst PostgreSQL Performance & Query Optimization mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um PostgreSQL Performance & Query Optimization zu starten?

Keine Vorkenntnisse erforderlich. PostgreSQL Performance & Query Optimization auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser PostgreSQL Performance & Query Optimization-Lektion Code schreiben und ausführen?

Ja. Jede PostgreSQL Performance & Query Optimization-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in EXPLAIN und ANALYZE
  2. Plan-Knoten interpretieren
  3. Leistungsengpässe identifizieren
  4. EXPLAIN-Kostenschätzungen und Zeilenanzahlen lesen
← Zurück zu PostgreSQL Performance & Query Optimization