この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
データアナリティクス事業本部コンサルティングチームの石川です。Redshiftは、SHOW TABLE
及びSHOW VIEW
コマンドを使用してDDLを表示するユーティリティを標準でサポートするようになりました。早速、どんな感じで、どこまで使えるのか試してみます。
SHOW TABLE コマンド
構文
SHOW TABLE table_name
TABLE定義の表示
例えば、categoryテーブルのDDLは、以下のように表示できます。
cmdb=# show table category;
Show Table DDL statement
------------------------------------------------
CREATE TABLE tickit.category ( +
catid smallint NOT NULL distkey, +
catgroup character varying(10) ENCODE lzo,+
catname character varying(10) ENCODE lzo, +
catdesc character varying(50) ENCODE lzo +
) +
DISTSTYLE KEY +
SORTKEY ( catid );
(1 row)
なお、外部テーブルは対応していません。
cmdb=# show table tickitdb.category;
ERROR: Operation not supported on external tables
SHOW VIEW コマンド
構文
SHOW VIEW view_name
以降では、普通のVIEW、Late Binding VIEW、Materialized VIEWのDDLを確認します。
普通のVIEW定義の表示
cmdb=# create view tickit_sales_vw as
select sellerid, username, (firstname ||' '|| lastname) as name,
city, sum(qtysold)
from tickit.sales, tickit.date, tickit.users
where sales.sellerid = users.userid
and sales.dateid = date.dateid
and year = 2008
and city = 'San Diego'
group by 1,2,3,4;
CREATE VIEW
cmdb=# show view tickit_sales_vw;
Show View DDL statement
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT sales.sellerid, users.username, (((users.firstname)::text || ' '::text) || (users.lastname)::text) AS name, users.city, sum(sales.qtysold) AS sum FROM sales, date, users WHERE ((((sales.sellerid = users.userid) AND (sales.dateid = date.dateid)) AND (date."year" = 2008)) AND ((users.city)::text = 'San Diego'::text)) GROUP BY sales.sellerid, users.username, (((users.firstname)::text || ' '::text) || (users.lastname)::text), users.city;
(1 row)
Late Binding VIEW定義の表示
cmdb=# create view tickit_sales_lbvw as
select sellerid, username, (firstname ||' '|| lastname) as name,
city, sum(qtysold)
from tickit.sales, tickit.date, tickit.users
where sales.sellerid = users.userid
and sales.dateid = date.dateid
and year = 2008
and city = 'San Diego'
group by 1,2,3,4
with no schema binding;
CREATE VIEW
cmdb=# show view tickit_sales_lbvw;
Show View DDL statement
------------------------------------------------------------------
create view tickit_sales_lbvw as +
select sellerid, username, (firstname ||' '|| lastname) as name,+
city, sum(qtysold) +
from tickit.sales, tickit.date, tickit.users +
where sales.sellerid = users.userid +
and sales.dateid = date.dateid +
and year = 2008 +
and city = 'San Diego' +
group by 1,2,3,4 +
with no schema binding;
(1 row)
Materialized VIEW定義の表示
cmdb=# create materialized view tickit_sales_mvw as
select sellerid, username, (firstname ||' '|| lastname) as name, city, sum(qtysold)
from tickit.sales, tickit.date, tickit.users
where sales.sellerid = users.userid
and sales.dateid = date.dateid
and year = 2008
and city = 'San Diego'
group by 1,2,3,4;
CREATE MATERIALIZED VIEW
cmdb=# show view tickit_sales_mvw;
Show View DDL statement
-------------------------------------------------------------------------------------
create materialized view tickit_sales_mvw as +
select sellerid, username, (firstname ||' '|| lastname) as name, city, sum(qtysold)+
from tickit.sales, tickit.date, tickit.users +
where sales.sellerid = users.userid +
and sales.dateid = date.dateid +
and year = 2008 +
and city = 'San Diego' +
group by 1,2,3,4;
(1 row)
まとめ
テーブルは、普通のテーブルのみ表示でき、外部テーブルのDDLは未対応でした。ビューは全て表示できました。今後は、カジュアルにDDLを生成して確認、修正して検証が捗るでしょう。
従来、テーブル定義はv_generate_tbl_ddl.sql 、ビューの定義は、v_generate_view_ddl.sqlを用いて出力していました。まとめてDDL出力するには、これらのビューを用いるほうが便利かもしれません。なお、MySQLやAthenaは、SHOW CREATE TABLE
コマンドなので混同しないように気をつけましょう。
ついでにですが、show all
を実行すると、その接続のパラメタを確認できたりします。
cmdb=# show all;
name | setting
---------------------------+----------
analyze_threshold_percent | 10
datestyle | ISO, MDY
extra_float_digits | 0
query_group | default
search_path | tickit
statement_timeout | 3600000
wlm_query_slot_count | 1
(7 rows)