SQL SquirrelでPostgreSQLテーブルのインデックスを操作する
PostgreSQL テーブルのインデックス一覧表示方法
\d+ コマンド
psql コマンドラインツールで \d+
コマンドを使用すると、テーブルに関する詳細情報を含む一覧が表示されます。この情報には、作成されたインデックスも含まれます。
postgres=# \d+ テーブル名
例:
postgres=# \d+ users
テーブル "public.users"
列名 | 型 | 修飾子 | 説明
-------+-----+---------+-----------
id | integer | not null |
name | text | |
email | text | |
インデックス:
名前 | 列名 | 順序 | ユニーク | フィルタ | 備考
-------+-------+-------+---------+---------+---------
users_pkey | id | ASC | t | | 主キー
pg_indexes
ビューは、データベース内のすべてのインデックスに関する情報を提供します。
SELECT * FROM pg_indexes WHERE tablename = 'テーブル名';
SELECT * FROM pg_indexes WHERE tablename = 'users';
indexname | tablename | schemaname | indexdef | indisunique | indisprimary | indisclustered | indisextends | indiximmediate | indixvacuum | fillfactor | tablespace | allow_system_table |
----------+-----------+-----------+----------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+-------------+
users_pkey | users | public | CREATE UNIQUE INDEX users_pkey ON users (id) | t | t | f | f | f | f | 90 | pg_default | f
情報スキーマ
PostgreSQLの情報スキーマには、データベース内のオブジェクトに関する情報が含まれています。
SELECT * FROM information_schema.indexes WHERE table_name = 'テーブル名';
SELECT * FROM information_schema.indexes WHERE table_name = 'users';
table_catalog | table_schema | table_name | index_name | non_unique | index_type | index_columns | column_name | ordinal_position | is_nullable |
--------------+--------------+------------+------------+------------+-------------+-------------+-------------+-------------+-------------+
postgresql | public | users | users_pkey | f | btree | (id) | id | 1 | NO |
その他のツール
テーブルのインデックスを一覧表示するツールは他にも多数あります。
- pgAdmin
- DBeaver
- SQL Squirrel
PostgreSQLテーブルのインデックス一覧表示には、いくつかの方法があります。どの方法を使用するかは、個人の好みや状況によって異なります。
psql コマンドラインツール
postgres=# \d+ テーブル名
postgres=# \d+ users
テーブル "public.users"
列名 | 型 | 修飾子 | 説明
-------+-----+---------+-----------
id | integer | not null |
name | text | |
email | text | |
インデックス:
名前 | 列名 | 順序 | ユニーク | フィルタ | 備考
-------+-------+-------+---------+---------+---------
users_pkey | id | ASC | t | | 主キー
pg_indexes ビュー
SELECT * FROM pg_indexes WHERE tablename = 'テーブル名';
SELECT * FROM pg_indexes WHERE tablename = 'users';
indexname | tablename | schemaname | indexdef | indisunique | indisprimary | indisclustered | indisextends | indiximmediate | indixvacuum | fillfactor | tablespace | allow_system_table |
----------+-----------+-----------+----------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+-------------+
users_pkey | users | public | CREATE UNIQUE INDEX users_pkey ON users (id) | t | t | f | f | f | f | 90 | pg_default | f
情報スキーマ
SELECT * FROM information_schema.indexes WHERE table_name = 'テーブル名';
SELECT * FROM information_schema.indexes WHERE table_name = 'users';
table_catalog | table_schema | table_name | index_name | non_unique | index_type | index_columns | column_name | ordinal_position | is_nullable |
--------------+--------------+------------+------------+------------+-------------+-------------+-------------+-------------+-------------+
postgresql | public | users | users_pkey | f | btree | (id) | id | 1 | NO |
PostgreSQLテーブルのインデックス一覧表示方法: 他の方法
\dt コマンド
postgres=# \dt テーブル名
テーブル "public.users"
列名 | 型 | 修飾子 | 説明
-------+-----+---------+-----------
id | integer | not null |
name | text | |
email | text | |
インデックス: 1
システムカタログ
SELECT * FROM pg_catalog.index i
JOIN pg_catalog.class t ON i.indrelid = t.oid
WHERE t.relname = 'テーブル名';
SELECT * FROM pg_catalog.index i
JOIN pg_catalog.class t ON i.indrelid = t.oid
WHERE t.relname = 'users';
indexrelid | indexname | indisunique | indisprimary | indisexclusion | indexcolname | indexprs | indexpred | indexorderby | indixtuple | indisvalid | indcheckxmin | indkeyopts | indnkeyattrs | indckcheck | indexspace |
------------+------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
2630 | users_pkey | t | t | f | id | | | | | t | f | (90,0) | 1 | f | pg_default |
GUIツール
pgAdmin、DBeaver、SQL SquirrelなどのGUIツールを使用して、テーブルのインデックスを一覧表示することもできます。これらのツールは、インデックスに関する詳細情報をわかりやすく表示する機能を提供しています。
postgresql indexing psql