Tabel di Redshift

Pengantar Redshift

Jason Myers

Principal Engineer

Membuat tabel

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

Mendistribusikan data

Kluster Redshift

  • Didistribusikan ke node komputasi
  • Menggunakan id baris internal Redshift, DISTKEY, atau PRIMARY KEY
  • Beberapa gaya distribusi
Pengantar Redshift

Gaya distribusi

Name Description Usage
ALL Seluruh tabel di setiap node Tabel lookup kecil yang sering dipakai di join
KEY Didistribusikan menurut data pada kolom DISTKEY Saat agregasi atau join berdasarkan DISTKEY
EVEN Bergiliran antar node per baris Tabel besar tanpa key
AUTO Menggunakan ALL untuk tabel kecil. Menjadi Key saat membesar jika ada DISTKEY yang cocok, jika tidak kembali ke EVEN default
Pengantar 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)
)
-- Mengatur kunci distribusi data 
-- ke organization_id
DISTKEY(organization_id);
CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    -- Mengatur organization_id sebagai 
    -- kunci distribusi data 
    'organization_id' VARCHAR(31) DISTKEY,
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
);
Pengantar Redshift

Mengatur gaya distribusi

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)
-- Mengatur gaya distribusi ke key
DISTSTYLE KEY;
Pengantar 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
-- Mengatur sort key data 
-- ke fk_monitoringlocation
SORTKEY(fk_monitoringlocation);
  • Mengatur urutan penyimpanan di disk
  • Memperkuat predicate pushdown
  • Dapat lebih dari satu
Pengantar Redshift

Mendefinisikan beberapa SORTKEY

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
)
-- Mengatur fk_monitoringlocation, organization_id sebagai compound sort key
COMPOUND SORTKEY(fk_monitoringlocation, organization_id);
Pengantar Redshift

Melihat status DISTKEY dan SORTKEY kolom

-- Lihat status dist dan sortkey
-- suatu kolom
SELECT column_name, 
       distkey, 
       sortkey
  FROM SVV_REDSHIFT_COLUMNS
 -- Hanya di skema spectrumdb
 WHERE schema_name = 'spectrumdb'
   -- Untuk tabel ecommerce_sales
   AND table_name = 'ecommerce_sales';
  • Hasil

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

    • t untuk True - f untuk False
  • Kolom SORTKEY
    • # adalah posisi dalam sort key, 0 berarti bukan bagian dari sortkey
Pengantar Redshift

Melihat distribution style

  • SVV_TABLE_INFO
  • Detail tabel yang memengaruhi kinerja kueri
    • distribution style
    • distribution skew
    • ukuran tabel
    • 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 |
Pengantar Redshift

Melihat distribution style (lanjutan)

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

Ayo berlatih!

Pengantar Redshift

Preparing Video For Download...