カタログクエリの例 - Amazon Redshift

カタログクエリの例

次のクエリは、カタログテーブルのクエリを実行して Amazon Redshift データベースに関する有益な情報を取得できるいくつかの方法を示しています。

テーブル ID、データベース名、スキーマ名、テーブル名の参照

次のビュー定義は、STV_TBL_PERM システムテーブルを PG_CLASS、PG_NAMESPACE、および PG_DATABASE システムカタログテーブルと統合し、テーブル ID、データベース名、スキーマ名、テーブル名を返します。

create view tables_vw as select distinct(id) table_id ,trim(datname) db_name ,trim(nspname) schema_name ,trim(relname) table_name from stv_tbl_perm join pg_class on pg_class.oid = stv_tbl_perm.id join pg_namespace on pg_namespace.oid = relnamespace join pg_database on pg_database.oid = stv_tbl_perm.db_id;

次の例は、テーブル ID 117855 の情報を返します。

select * from tables_vw where table_id = 117855;
table_id | db_name | schema_name | table_name ---------+-----------+-------------+----------- 117855 | dev | public | customer

Amazon Redshift テーブルごとの列数のリスト

次のクエリは、カタログテーブルを結合して、各 Amazon Redshift テーブルに含まれる列の数を調べます。Amazon Redshift テーブル名は、PG_TABLES と STV_TBL_PERM の両方に保存されます。可能であれば、PG_TABLES を使用して Amazon Redshift テーブル名を返します。

このクエリには Amazon Redshift テーブルは関連しません。

select nspname, relname, max(attnum) as num_cols from pg_attribute a, pg_namespace n, pg_class c where n.oid = c.relnamespace and a.attrelid = c.oid and c.relname not like '%pkey' and n.nspname not like 'pg%' and n.nspname not like 'information%' group by 1, 2 order by 1, 2; nspname | relname | num_cols --------+----------+---------- public | category | 4 public | date | 8 public | event | 6 public | listing | 8 public | sales | 10 public | users | 18 public | venue | 5 (7 rows)

データベースのスキーマとテーブルのリスト

次のクエリは STV_TBL_PERM をいくつかの PG テーブルに結合し、TICKIT データベースのテーブルのリストとそのスキーマ名 (NSPNAME 列) を返します。このクエリは、各テーブルの行の合計数も返します。(このクエリは、システムの複数のスキーマに同じテーブル名がある場合に役立ちます。)

select datname, nspname, relname, sum(rows) as rows from pg_class, pg_namespace, pg_database, stv_tbl_perm where pg_namespace.oid = relnamespace and pg_class.oid = stv_tbl_perm.id and pg_database.oid = stv_tbl_perm.db_id and datname ='tickit' group by datname, nspname, relname order by datname, nspname, relname; datname | nspname | relname | rows --------+---------+----------+-------- tickit | public | category | 11 tickit | public | date | 365 tickit | public | event | 8798 tickit | public | listing | 192497 tickit | public | sales | 172456 tickit | public | users | 49990 tickit | public | venue | 202 (7 rows)

テーブル ID、データ型、列名、およびテーブル名のリスト

次のクエリは、各ユーザーテーブルとその列に関するいくつかの情報 (各列のテーブル ID、テーブル名、列名、およびデータ型) をリストします。

select distinct attrelid, rtrim(name), attname, typname from pg_attribute a, pg_type t, stv_tbl_perm p where t.oid=a.atttypid and a.attrelid=p.id and a.attrelid between 100100 and 110000 and typname not in('oid','xid','tid','cid') order by a.attrelid asc, typname, attname; attrelid | rtrim | attname | typname ---------+----------+----------------+----------- 100133 | users | likebroadway | bool 100133 | users | likeclassical | bool 100133 | users | likeconcerts | bool ... 100137 | venue | venuestate | bpchar 100137 | venue | venueid | int2 100137 | venue | venueseats | int4 100137 | venue | venuecity | varchar ...

テーブルの各列のデータブロック数のカウント

次のクエリは、STV_BLOCKLIST テーブルを PG_CLASS に結合し、SALES テーブルの列のストレージ情報を返します。

select col, count(*) from stv_blocklist s, pg_class p where s.tbl=p.oid and relname='sales' group by col order by col; col | count ----+------- 0 | 4 1 | 4 2 | 4 3 | 4 4 | 4 5 | 4 6 | 4 7 | 4 8 | 4 9 | 8 10 | 4 12 | 4 13 | 8 (13 rows)