CTAS使用說明 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CTAS使用說明

限制

Amazon Redshift 會依節點類型強制執行每個叢集的資料表數量配額限制。

資料表名稱的字元數上限為 127 個。

單一資料表中可定義的資料欄數目上限為 1,600 個。

欄位和資料表屬性的繼承

CREATETABLEAS (CTAS) 資料表不會繼承條件約束、識別資料行、預設資料行值或從其建立的資料表中的主索引鍵。

您無法指定資料CTAS表的資料行壓縮編碼。Amazon Redshift 會自動指派壓縮編碼,如下所示:

  • 定義為排序索引鍵的欄會被指定RAW壓縮。

  • 定義為BOOLEAN、、REALDOUBLEPRECISION、GEOMETRY或GEOGRAPHY資料類型的資料行會被指派RAW壓縮。

  • 定義為SMALLINT、、、INTEGER、BIGINT、DECIMAL、DATETIMETIMETZTIMESTAMP、或已指定AZ64壓縮TIMESTAMPTZ的欄。

  • 定義為CHAR、VARCHAR或已指派LZO壓縮VARBYTE的資料行。

如需詳細資訊,請參閱 壓縮編碼資料類型

若要明確指派資料欄編碼,請使用 CREATE TABLE

CTAS根據SELECT子句的查詢計劃,決定新資料表的分佈樣式和排序索引鍵。

對於複雜的查詢,例如包含聯結、彙總、order by 子句或 limit 子句的查詢,會CTAS盡最大努力根據查詢計畫選擇最佳的發佈樣式和排序索引鍵。

注意

為了在大型資料集或複雜查詢上發揮最佳效能,建議您使用一般資料集進行測試。

您通常可以透過檢查查詢計畫來查看查詢最佳化工具CTAS選擇哪些資料行 (如果有的話),來預測哪些分發索引鍵和排序索引鍵會選擇排序和散佈資料。如果查詢計畫的頂端節點是來自單一資料表 (XN Seq Scan) 的簡單循序掃描,則CTAS通常會使用來源資料表的發佈樣式和排序索引鍵。如果查詢計劃的頂端節點是任何其他連續掃描 (例如 XN Limit、XN 排序、XN HashAggregate 等等),CTAS會盡最大努力根據查詢計劃選擇最佳的發佈樣式和排序索引鍵。

例如,假設您使用下列子SELECT句類型建立五個表格:

  • 簡單 select 陳述式

  • 限制子句

  • 通過使用條款的順序 LISTID

  • 通過使用條款的順序 QTYSOLD

  • 帶有 group by 子句的SUM聚合函數。

下列範例顯示每個CTAS陳述式的查詢計畫。

explain create table sales1_simple as select listid, dateid, qtysold from sales; QUERY PLAN ---------------------------------------------------------------- XN Seq Scan on sales (cost=0.00..1724.56 rows=172456 width=8) (1 row) explain create table sales2_limit as select listid, dateid, qtysold from sales limit 100; QUERY PLAN ---------------------------------------------------------------------- XN Limit (cost=0.00..1.00 rows=100 width=8) -> XN Seq Scan on sales (cost=0.00..1724.56 rows=172456 width=8) (2 rows) explain create table sales3_orderbylistid as select listid, dateid, qtysold from sales order by listid; QUERY PLAN ------------------------------------------------------------------------ XN Sort (cost=1000000016724.67..1000000017155.81 rows=172456 width=8) Sort Key: listid -> XN Seq Scan on sales (cost=0.00..1724.56 rows=172456 width=8) (3 rows) explain create table sales4_orderbyqty as select listid, dateid, qtysold from sales order by qtysold; QUERY PLAN ------------------------------------------------------------------------ XN Sort (cost=1000000016724.67..1000000017155.81 rows=172456 width=8) Sort Key: qtysold -> XN Seq Scan on sales (cost=0.00..1724.56 rows=172456 width=8) (3 rows) explain create table sales5_groupby as select listid, dateid, sum(qtysold) from sales group by listid, dateid; QUERY PLAN ---------------------------------------------------------------------- XN HashAggregate (cost=3017.98..3226.75 rows=83509 width=8) -> XN Seq Scan on sales (cost=0.00..1724.56 rows=172456 width=8) (2 rows)

若要檢視每個資料表的分配索引鍵和排序索引鍵,請查詢 PG TABLE _ DEF 系統目錄資料表,如下所示。

select * from pg_table_def where tablename like 'sales%'; tablename | column | distkey | sortkey ----------------------+------------+---------+--------- sales | salesid | f | 0 sales | listid | t | 0 sales | sellerid | f | 0 sales | buyerid | f | 0 sales | eventid | f | 0 sales | dateid | f | 1 sales | qtysold | f | 0 sales | pricepaid | f | 0 sales | commission | f | 0 sales | saletime | f | 0 sales1_simple | listid | t | 0 sales1_simple | dateid | f | 1 sales1_simple | qtysold | f | 0 sales2_limit | listid | f | 0 sales2_limit | dateid | f | 0 sales2_limit | qtysold | f | 0 sales3_orderbylistid | listid | t | 1 sales3_orderbylistid | dateid | f | 0 sales3_orderbylistid | qtysold | f | 0 sales4_orderbyqty | listid | t | 0 sales4_orderbyqty | dateid | f | 0 sales4_orderbyqty | qtysold | f | 1 sales5_groupby | listid | f | 0 sales5_groupby | dateid | f | 0 sales5_groupby | sum | f | 0

下表顯示結果摘要。為了簡化起見,我們省略了說明計畫中的成本、資料列和寬度詳細資訊。

資料表

CTASSELECT聲明

說明計劃頂端節點

分佈索引鍵

排序索引鍵

S1_ SIMPLE

select listid, dateid, qtysold from sales

XN Seq Scan on sales ...

LISTID DATEID
S2_ LIMIT

select listid, dateid, qtysold from sales limit 100

XN Limit ...

無(EVEN)
S3_ _ _ ORDER LISTID

select listid, dateid, qtysold from sales order by listid

XN Sort ...

Sort Key: listid

LISTID LISTID
S4_ _ _ ORDER QTY

select listid, dateid, qtysold from sales order by qtysold

XN Sort ...

Sort Key: qtysold

LISTID QTYSOLD
S5_ _ GROUP 由

select listid, dateid, sum(qtysold) from sales group by listid, dateid

XN HashAggregate ...

無(EVEN)

您可以在CTAS陳述式中明確指定散佈樣式和排序索引鍵。例如,下列陳述式會使用 EVEN distribution 建立資料表,並指定SALESID為排序索引鍵。

create table sales_disteven diststyle even sortkey (salesid) as select eventid, venueid, dateid, eventname from event;

壓縮編碼

ENCODEAUTO用作表格的預設值。Amazon Redshift 會自動管理資料表中所有資料欄的壓縮編碼。

傳入資料的分佈

當傳入資料的雜湊分佈機制與目標資料表的機制相同時,不需要在資料載入時實際分佈資料。例如,如果新資料表設定了分佈索引鍵,而且要從另一個分佈在相同索引鍵資料欄上的資料表插入資料,則會使用相同的節點和分割就地載入資料。但是,如果來源和目標資料表都設定為EVEN發佈,則資料會重新分配到目標資料表中。

自動ANALYZE操作

Amazon Redshift 會自動分析您使用CTAS命令建立的表格。第一次建立這些表格時,您不需要在這些表格上執行ANALYZE命令。若您修改這些資料表,則應依照與其他資料表相同的方式進行分析。