0Pricing
PostgreSQL Performance & Query Optimization · レッスン

EXPLAINのコスト推定と行数を読む

EXPLAINが各プランノードに付与するコスト、推定行数、幅の値を解釈し、プランナーの推定が間違っている場合を見抜く方法を学びます。

「EXPLAINのコスト推定と行数を読む」はCoddyKit上の無料PostgreSQL Performance & Query Optimizationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPostgreSQL Performance & Query Optimization学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「EXPLAINのコスト推定と行数を読む」レッスンは無料ですか?

はい。「EXPLAINのコスト推定と行数を読む」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、PostgreSQL Performance & Query Optimizationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。

「EXPLAINのコスト推定と行数を読む」で何を学びますか?

EXPLAINが各プランノードに付与するコスト、推定行数、幅の値を解釈し、プランナーの推定が間違っている場合を見抜く方法を学びます。 ブラウザで直接実行するハンズオンコードでPostgreSQL Performance & Query Optimizationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

PostgreSQL Performance & Query Optimizationを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのPostgreSQL Performance & Query Optimizationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「EXPLAINのコスト推定と行数を読む」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このPostgreSQL Performance & Query Optimizationレッスンでコードを書いて実行できますか?

はい。すべてのPostgreSQL Performance & Query Optimizationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. EXPLAINとANALYZE入門
  2. プランノードの読み解き方
  3. パフォーマンスボトルネックの特定
  4. EXPLAINのコスト推定と行数を読む
← PostgreSQL Performance & Query Optimizationに戻る