EXPLAINとEXPLAIN ANALYZEの出力を読む
EXPLAINでPostgreSQLのクエリプランを読み解き、プランナーがクエリを実行する方法と、実際にどこで時間が費やされているのかを確認します。
「EXPLAINとEXPLAIN ANALYZEの出力を読む」はCoddyKit上の無料PostgreSQL Performance & Query Optimizationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPostgreSQL Performance & Query Optimization学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Read Query Plans
Before tuning a slow query, see what PostgreSQL actually does. EXPLAIN shows the planner's chosen execution plan as a tree of nodes.
EXPLAIN vs EXPLAIN ANALYZE
EXPLAIN shows estimates only; EXPLAIN ANALYZE actually runs the query and reports real timings and row counts — far more useful for diagnosis.
EXPLAIN ANALYZE
SELECT * FROM orders WHERE customer_id = 42;The Plan Tree
Read plan trees inside-out and bottom-up: leaf nodes scan data, parents join or aggregate. Each node reports its cost, rows, and width.
The cost Numbers
The cost reads as cost=0.00..18.45: first is startup cost, second is total, both in arbitrary planner units used to compare competing plans.
Sequential Scan
A Seq Scan reads the whole table — fine for small tables or when most rows match, but a red flag on a selective query over a large one.
Index Scan
An Index Scan uses an index to find matching rows, then fetches them from the table. It's the win for selective predicates.
Estimated vs Actual Rows
Compare estimated rows with actual rows. A big mismatch means stale statistics and often a bad plan — run ANALYZE to refresh them, as shown below.
ANALYZE orders;Spotting the Bottleneck
To find the bottleneck, hunt for the node with the largest actual time, or one whose loops multiply its cost. That's where to focus tuning.
BUFFERS for I/O Insight
Add BUFFERS to your EXPLAIN to see how many pages came from cache versus disk — the fast way to spot I/O-bound queries. See the example below.
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE total > 100;Join Strategies
Watch the join strategy: Nested Loop, Hash Join, or Merge Join. A Nested Loop over many rows without an index is a classic slow pattern.
Readable Formats
For big plans, switch to FORMAT JSON or a visual tool to explore them more easily. The query below shows the JSON option.
EXPLAIN (ANALYZE, FORMAT JSON)
SELECT * FROM orders;Quick Check
Test what you have learned.
Recap
Recap: read EXPLAIN trees, compare estimates with ANALYZE actuals, tell Seq from Index scans and join types, use BUFFERS, and find the bottleneck node.
よくある質問
「EXPLAINとEXPLAIN ANALYZEの出力を読む」レッスンは無料ですか?
はい。「EXPLAINとEXPLAIN ANALYZEの出力を読む」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、PostgreSQL Performance & Query Optimizationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。
「EXPLAINとEXPLAIN ANALYZEの出力を読む」で何を学びますか?
EXPLAINでPostgreSQLのクエリプランを読み解き、プランナーがクエリを実行する方法と、実際にどこで時間が費やされているのかを確認します。 ブラウザで直接実行するハンズオンコードでPostgreSQL Performance & Query Optimizationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
PostgreSQL Performance & Query Optimizationを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのPostgreSQL Performance & Query Optimizationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「EXPLAINとEXPLAIN ANALYZEの出力を読む」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このPostgreSQL Performance & Query Optimizationレッスンでコードを書いて実行できますか?
はい。すべてのPostgreSQL Performance & Query Optimizationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- データベースパフォーマンスの基礎
- PostgreSQLアーキテクチャの概要
- 基本的なクエリ実行の流れ
- EXPLAINとEXPLAIN ANALYZEの出力を読む