インデックスの作成と削除
インデックスの作成、確認、削除に使用するSQLコマンドと、管理におけるベストプラクティスを学びます。
「インデックスの作成と削除」はCoddyKit上の無料Advanced PostgreSQL: Indexing, Partitioning, Replicationレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAdvanced PostgreSQL: Indexing, Partitioning, Replication学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Index Management: The Basics
Time to get hands-on. This lesson covers the practical SQL to create, verify, and drop indexes — the everyday tools of index management.
Setting Up Our Table
First, let's build a small users table with a few rows to experiment on. Run the code to create and populate it.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
city VARCHAR(100)
);
INSERT INTO users (name, email, city) VALUES
('Alice Smith', 'alice@example.com', 'New York'),
('Bob Johnson', 'bob@example.com', 'Los Angeles'),
('Charlie Brown', 'charlie@example.com', 'New York'),
('Diana Prince', 'diana@example.com', 'London');The CREATE INDEX Command
CREATE INDEX name ON table (column) builds an index to speed up queries. PostgreSQL makes it a B-tree by default — no USING BTREE needed.
Creating Your First Index
Let's index the city column so filtering by city gets much faster as the table grows. Run the command below.
CREATE INDEX idx_users_city ON users (city);Verifying Existing Indexes
Did it work? PostgreSQL's pg_indexes system view lists every index in your database — query it to inspect what you just built.
Example: Listing Indexes
Query pg_indexes filtered to the users table to confirm your new index exists. Run it and check the output.
SELECT indexname, tablename, indexdef
FROM pg_indexes
WHERE tablename = 'users';
Composite Indexes
When queries filter on several columns, use a composite index spanning them. Column order matters — match your typical WHERE clause order.
Creating a Composite Index
Here we build a composite index on city and name — handy when you often search by both at once. Then re-run the pg_indexes query.
CREATE INDEX idx_users_city_name ON users (city, name);The DROP INDEX Command
Drop an index when it's unused, slowing writes, or being replaced. The syntax is simple: DROP INDEX index_name.
Example: Dropping an Index
Let's remove the idx_users_city index by name with DROP INDEX, then verify it's gone with the pg_indexes query.
DROP INDEX idx_users_city;Quick Check: Index Commands
Consider a table named products with columns product_id, category, and price.
Recap: Index Management
That's index management: CREATE INDEX to add, pg_indexes to verify, DROP INDEX to remove. Next up: more advanced index types.
よくある質問
「インデックスの作成と削除」レッスンは無料ですか?
はい。「インデックスの作成と削除」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Advanced PostgreSQL: Indexing, Partitioning, Replicationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。
「インデックスの作成と削除」で何を学びますか?
インデックスの作成、確認、削除に使用するSQLコマンドと、管理におけるベストプラクティスを学びます。 ブラウザで直接実行するハンズオンコードでAdvanced PostgreSQL: Indexing, Partitioning, Replicationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Advanced PostgreSQL: Indexing, Partitioning, Replicationを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのAdvanced PostgreSQL: Indexing, Partitioning, Replicationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「インデックスの作成と削除」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このAdvanced PostgreSQL: Indexing, Partitioning, Replicationレッスンでコードを書いて実行できますか?
はい。すべてのAdvanced PostgreSQL: Indexing, Partitioning, Replicationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- インデックスが重要な理由
- B-Treeインデックスの基礎
- インデックスの作成と削除
- 一意キーインデックスと主キーインデックス