Tabellen in Redshift

Introductie tot Redshift

Jason Myers

Principal Engineer

Tabellen maken

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

Data distribueren

Redshift-cluster

  • Verdeeld over compute-nodes
  • Gebruikt interne Redshift-rij-id, DISTKEY of PRIMARY KEY
  • Meerdere distributiestijlen
Introductie tot Redshift

Distributiestijlen

Name Description Usage
ALL Hele tabel op elke node Kleine lookup-facttabellen, vaak nodig bij joins
KEY Verdeeld op data in de DISTKEY-kolom Als we aggregeren of joinen op DISTKEY
EVEN Om-en-om verdeling per rij over nodes Grote tabellen zonder geschikte keys
AUTO Gebruikt ALL voor kleine tabellen. Wordt KEY als de tabel groeit (met geschikte DISTKEYS), anders EVEN standaard
Introductie tot 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)
)
-- Stelt de distributiesleutel in 
-- op organization_id
DISTKEY(organization_id);
CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    -- Stelt organization_id in als 
    -- distributiesleutel 
    'organization_id' VARCHAR(31) DISTKEY,
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
);
Introductie tot Redshift

De distributiestijl instellen

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)
-- Stelt de distributiestijl in op key
DISTSTYLE KEY;
Introductie tot 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
-- Stelt de sorteersleutel in 
-- op fk_monitoringlocation
SORTKEY(fk_monitoringlocation);
  • Bepaalt de opslagvolgorde op schijf
  • Versterkt predicate pushdown
  • Kan meerdere hebben
Introductie tot Redshift

Meerdere SORTKEY's definiëren

CREATE TABLE IDAHO_SITE_ID
(
    'pk_siteid' INTEGER PRIMARY KEY,
    'fk_monitoringlocation' INTEGER,
    'organization_id' VARCHAR(31),
    'organizationformalname' VARCHAR(68),
    'organization' VARCHAR(16)
)
-- Stelt fk_monitoringlocation, organization_id in als samengestelde sorteersleutels
COMPOUND SORTKEY(fk_monitoringlocation, organization_id);
Introductie tot Redshift

DISTKEY- en SORTKEY-status van kolommen bekijken

-- Bekijk de dist- en sortkey-
-- status van een kolom
SELECT column_name, 
       distkey, 
       sortkey
  FROM SVV_REDSHIFT_COLUMNS
 -- Alleen in het schema spectrumdb
 WHERE schema_name = 'spectrumdb'
   -- Voor de tabel ecommerce_sales
   AND table_name = 'ecommerce_sales';
  • Resultaten

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

    • t voor True - f voor False
  • SORTKEY-kolom
    • is positie in de sortkey, 0 = niet in sortkey

Introductie tot Redshift

Distributiestijl bekijken

  • SVV_TABLE_INFO
  • Tabeldetails die queryprestaties beïnvloeden
    • distributiestijl
    • distributie-ongelijkheid
    • tabelgrootte
    • sorteersleutels
    • sortkey-ongelijkheid
table          | encoded | diststyle       | sortkey1     | skew_sortkey1 | skew_rows
===============|=========|=================|==============|===============|===========
ecommerce_sales| N       | KEY(year_qtr)   | year_qtr     |               |          
date           | N       | ALL             | dateid       |          1.00 |
Introductie tot Redshift

Distributiestijl (vervolg)

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

Laten we oefenen!

Introductie tot Redshift

Preparing Video For Download...