Redshift 中的資料表

Redshift 入門

Jason Myers

Principal Engineer

建立資料表

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
);
Redshift 入門

資料分配

Redshift 叢集

  • 分散於運算節點
  • 使用 Redshift 內部 row id、DISTKEYPRIMARY KEY
  • 多種分配樣式
Redshift 入門

分配樣式

Name Description Usage
ALL 每個節點各有完整資料表 小型查詢對應的事實/對照表,常用於聯結
KEY DISTKEY 欄位值分配 DISTKEY 彙總或聯結時
EVEN 逐列平均分配到各節點 大型且無明確鍵的資料表
AUTO 小表用 ALL。成長後若有合適的 DISTKEY 轉為 KEY,否則回退為 EVEN default
Redshift 入門

DISTKEY

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
)
-- Sets the data distribution key 
-- to organization_id
DISTKEY(organization_id);
CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    -- Sets organization_id as the data 
    -- distribution key 
    'organization_id' VARCHAR(31) DISTKEY,
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
);
Redshift 入門

設定分配樣式

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
)
DISTKEY(organization_id)
-- Sets the distribution style to key
DISTSTYLE KEY;
Redshift 入門

SORTKEY

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
)
DISTKEY(organization_id)
DISTSTYLE KEY
-- Sets the data sort key 
-- to fk_monitoringlocation
SORTKEY(fk_monitoringlocation);
  • 控制磁碟上的儲存順序
  • 強化條件下推的效益
  • 可設定多個
Redshift 入門

定義多個 SORTKEY

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
)
-- Sets fk_monitoringlocation, organization_id as compound sort keys
COMPOUND SORTKEY(fk_monitoringlocation, organization_id);
Redshift 入門

檢視欄位的 DISTKEY 與 SORTKEY 狀態

-- View the dist and sortkey
-- status of a column
SELECT column_name, 
       distkey, 
       sortkey
  FROM SVV_REDSHIFT_COLUMNS
 -- Only in the spectrumdb schema
 WHERE schema_name = 'spectrumdb'
   -- For the ecommerce_sales table
   AND table_name = 'ecommerce_sales';
  • 結果

       column   | distkey | sortkey
    ============|=========|========
    year_qtr    | t       | 1
    total_sales | f       | 2
    ecom_sales  | f       | 0
    
  • Distkey 欄位

    • t 代表 True,f 代表 False
  • SORTKEY 欄位
    • # 為在 sort key 中的位置,0 表示不屬於 sortkey
Redshift 入門

檢視分配樣式

  • SVV_TABLE_INFO
  • 影響查詢效能的資料表細節
    • distribution style
    • distribution skew
    • table size
    • sortkeys
    • sortkey skew
table          | encoded | diststyle       | sortkey1     | skew_sortkey1 | skew_rows
===============|=========|=================|==============|===============|===========
ecommerce_sales| N       | KEY(year_qtr)   | year_qtr     |               |          
date           | N       | ALL             | dateid       |          1.00 |
Redshift 入門

持續檢視分配樣式

SELECT table
       diststyle 
  FROM SVV_TABLE_INFO
 WHERE schema like 'spectrumdb';
table           | diststyle 
================|============== 
ecommerce_sales | KEY(year_qtr)
Redshift 入門

一起來練習吧!

Redshift 入門

Preparing Video For Download...