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

クエリプランナーのコスト定数を調整する

random_page_costやeffective_cache_sizeなどのプランナーのコスト定数がプランの選択に与える影響と、ハードウェアやデータに合わせて調整する方法を学びます。

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

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

The Planner Needs Hints About Hardware

The planner estimates the cost of each plan using constants that model your hardware. If those constants do not match reality, it may pick a sequential scan when an index scan would be far faster, or vice versa.

seq_page_cost vs random_page_cost

Two key constants:

  • seq_page_cost (default 1.0): cost to read a page sequentially
  • random_page_cost (default 4.0): cost to read a page at random

The 4:1 ratio assumes spinning disks where random reads are slow.

Tuning for SSDs

On SSDs random reads are nearly as cheap as sequential ones. Lowering random_page_cost makes the planner favor index scans appropriately.

SET random_page_cost = 1.1;

effective_cache_size

This tells the planner how much memory the OS and PostgreSQL together are likely to use for caching data. It does not allocate memory — it only influences cost estimates for index scans.

SET effective_cache_size = '12GB';

Why effective_cache_size Matters

A large value tells the planner that repeated index lookups will likely hit cache, making index scans look cheaper and more attractive for large tables. Set it to roughly 50-75% of total RAM.

CPU Cost Constants

Finer constants weigh CPU work:

  • cpu_tuple_cost: per-row processing
  • cpu_index_tuple_cost: per index entry
  • cpu_operator_cost: per operator/function call

These are rarely changed but matter for CPU-heavy expressions.

Testing a Change at Session Level

Test constants with SET in your session before making them permanent. Compare plans with EXPLAIN.

SET random_page_cost = 1.1;
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;

Making It Permanent

Once satisfied, set the value cluster-wide and reload, or edit postgresql.conf.

ALTER SYSTEM SET random_page_cost = 1.1;
SELECT pg_reload_conf();

Beware of Over-Tuning

Do not chase a single slow query by skewing constants globally — you may break thousands of other plans. Fix the root cause (missing index, stale stats) first; adjust constants only for genuine hardware mismatches.

Per-Tablespace Tuning

If some data sits on SSD and some on HDD, you can set random_page_cost per tablespace so the planner reasons about each storage medium correctly.

ALTER TABLESPACE fast_ssd
SET (random_page_cost = 1.1);

Verifying the Effect

After changing a constant, confirm the planner actually switched plans. Run EXPLAIN before and after and look for a Seq Scan turning into an Index Scan (or vice versa).

EXPLAIN SELECT * FROM orders WHERE customer_id = 42;
SET random_page_cost = 1.1;
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;

Quick Check

Test your planner-tuning knowledge.

Recap

You learned planner cost tuning:

  • Cost constants model your hardware for the planner
  • Lower random_page_cost on SSDs to favor index scans
  • Set effective_cache_size to ~50-75% of RAM
  • Test with SET and EXPLAIN before ALTER SYSTEM
  • Fix indexes/stats before skewing constants globally

よくある質問

「クエリプランナーのコスト定数を調整する」レッスンは無料ですか?

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

「クエリプランナーのコスト定数を調整する」で何を学びますか?

random_page_costやeffective_cache_sizeなどのプランナーのコスト定数がプランの選択に与える影響と、ハードウェアやデータに合わせて調整する方法を学びます。 ブラウザで直接実行するハンズオンコードでPostgreSQL Performance & Query Optimizationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「クエリプランナーのコスト定数を調整する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. postgresql.confの主要パラメーター
  2. メモリ(shared_buffers、work_mem)のチューニング
  3. ディスクI/Oとチェックポイントのチューニング
  4. クエリプランナーのコスト定数を調整する
← PostgreSQL Performance & Query Optimizationに戻る