複合インデックスとカバリングインデックス
複合インデックスで複数列のクエリを高速化し、INCLUDEを使ったカバリングインデックスでテーブル参照を完全になくします。
「複合インデックスとカバリングインデックス」はCoddyKit上の無料PostgreSQL Performance & Query Optimizationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPostgreSQL Performance & Query Optimization学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond Single-Column Indexes
Many queries filter on more than one column. A composite index spans multiple columns and can serve such queries far better than separate single-column indexes.
Creating a Composite Index
List columns in the order you want them indexed.
CREATE INDEX idx_orders_cust_date
ON orders (customer_id, order_date);Column Order Matters
A composite index on (a, b) helps queries filtering on a alone or a AND b, but generally NOT queries filtering only on b.
The Leftmost Prefix Rule
PostgreSQL can use any leftmost prefix of the index. So (a, b, c) serves filters on a, on a+b, and on a+b+c.
Ordering by Selectivity
Put the most selective or most frequently filtered column first, often an equality column before a range column, to maximize how many rows the index eliminates early.
CREATE INDEX idx_orders_status_date
ON orders (status, order_date);The Heap Lookup Cost
A normal index scan finds matching rows then visits the table (the heap) to read the other columns. Those extra fetches cost I/O.
Covering Indexes
A covering index contains every column the query needs, so PostgreSQL answers from the index alone, an Index Only Scan, with no heap visit.
Using INCLUDE
The INCLUDE clause adds non-key columns to the index leaf pages so they are available without being part of the searchable key.
CREATE INDEX idx_orders_cust_incl
ON orders (customer_id)
INCLUDE (order_date, total);Confirming Index Only Scan
Check the plan; you want to see Index Only Scan rather than Index Scan plus heap fetches.
EXPLAIN ANALYZE
SELECT order_date, total
FROM orders WHERE customer_id = 42;The Visibility Map Caveat
Index Only Scans still need the heap when pages are not marked all-visible. Keeping tables well-vacuumed maintains the visibility map so scans stay index-only.
VACUUM ANALYZE orders;Costs of Wide Indexes
More columns mean larger indexes and slower writes. Add covering columns only for hot queries, not everywhere.
Quick Check
Test what you have learned.
Recap
You learned how composite indexes serve multi-column filters under the leftmost prefix rule, how column order and selectivity matter, and how covering indexes with INCLUDE enable Index Only Scans, balanced against larger size and write cost.
よくある質問
「複合インデックスとカバリングインデックス」レッスンは無料ですか?
はい。「複合インデックスとカバリングインデックス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、PostgreSQL Performance & Query Optimizationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。
「複合インデックスとカバリングインデックス」で何を学びますか?
複合インデックスで複数列のクエリを高速化し、INCLUDEを使ったカバリングインデックスでテーブル参照を完全になくします。 ブラウザで直接実行するハンズオンコードで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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- B-Treeインデックスの基礎
- インデックスの作成と利用
- インデックスを付けるタイミングと方法
- 複合インデックスとカバリングインデックス