分佈範例 - Amazon Redshift

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

分佈範例

下列範例顯示如何根據您在 CREATE TABLE 陳述式中定義的選項來分佈資料。

DISTKEY 範例

查看 TICKIT 資料庫中 USERS 資料表的結構描述。USERID 會定義為 SORTKEY 資料欄和 DISTKEY 資料欄:

select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 'users'; column | type | encoding | distkey | sortkey ---------------+------------------------+----------+---------+--------- userid | integer | none | t | 1 username | character(8) | none | f | 0 firstname | character varying(30) | text32k | f | 0 ...

USERID 是此資料表上分佈資料欄不錯的選擇。如果查詢 SVV_DISKUSAGE 系統檢視,您可以看到資料表很均勻地分佈。資料欄號碼以零開始,所以 USERID 為資料欄 0。

select slice, col, num_values as rows, minvalue, maxvalue from svv_diskusage where name='users' and col=0 and rows>0 order by slice, col; slice| col | rows | minvalue | maxvalue -----+-----+-------+----------+---------- 0 | 0 | 12496 | 4 | 49987 1 | 0 | 12498 | 1 | 49988 2 | 0 | 12497 | 2 | 49989 3 | 0 | 12499 | 3 | 49990 (4 rows)

資料表包含 49,990 個資料列。資料列 (num_values) 資料欄顯示每個配量包含大約相同數目的資料列。minvalue 和 maxvalue 資料欄顯示每一個配量上的值範圍。每一個配量幾乎包含整個值範圍,所以每一個配量都有很好的機會參與執行一個篩選使用者 ID 範圍的查詢。

此範例示範小型測試系統上的分佈。配量總數通常高得多。

如果您最常使用 STATE 資料欄進行聯結或分組,則可以選擇在 STATE 資料欄上進行分佈。下列範例顯示,若您建立一個新資料表,其中資料與 USERS 資料表相同,但將 DISTKEY 設為 STATE 資料欄。在此情況下,分佈不是平均的。配量 0 (13,587 個資料列) 比配量 3 (10,150 個資料列) 多保留大約 30% 的資料列。在規模很大的資料表中,此分佈扭曲數量可能對查詢處理具有負面影響。

create table userskey distkey(state) as select * from users; select slice, col, num_values as rows, minvalue, maxvalue from svv_diskusage where name = 'userskey' and col=0 and rows>0 order by slice, col; slice | col | rows | minvalue | maxvalue ------+-----+-------+----------+---------- 0 | 0 | 13587 | 5 | 49989 1 | 0 | 11245 | 2 | 49990 2 | 0 | 15008 | 1 | 49976 3 | 0 | 10150 | 4 | 49986 (4 rows)

DISTSTYLE EVEN 範例

如果您建立一個新資料表,其中資料與 USERS 資料表相同,但將 DISTSTYLE 設為 EVEN,則資料列一律平均地分佈在配量上。

create table userseven diststyle even as select * from users; select slice, col, num_values as rows, minvalue, maxvalue from svv_diskusage where name = 'userseven' and col=0 and rows>0 order by slice, col; slice | col | rows | minvalue | maxvalue ------+-----+-------+----------+---------- 0 | 0 | 12497 | 4 | 49990 1 | 0 | 12498 | 8 | 49984 2 | 0 | 12498 | 2 | 49988 3 | 0 | 12497 | 1 | 49989 (4 rows)

不過,因為分佈不是根據特定資料欄,所以查詢處理能力可能降低,尤其在資料表聯結至其他資料表時更是如此。聯結資料欄若少了分佈,通常會影響可以有效地執行聯結操作的類型。當這兩個資料表在其各自聯結資料欄上進行分佈和排序時,會最佳化聯結、彙總和分組操作。

DISTSTYLE ALL 範例

如果您建立一個新資料表,其中資料與 USERS 資料表相同,但將 DISTSTYLE 設為 ALL,則所有資料列會分佈至每個節點的第一個配量。

select slice, col, num_values as rows, minvalue, maxvalue from svv_diskusage where name = 'usersall' and col=0 and rows > 0 order by slice, col; slice | col | rows | minvalue | maxvalue ------+-----+-------+----------+---------- 0 | 0 | 49990 | 4 | 49990 2 | 0 | 49990 | 2 | 49990 (4 rows)