Transacties

Introductie tot Redshift

Jason Myers

Principal Engineer

Waarom transacties gebruiken

SELECT name,
       priority,
  FROM data_log
       -- SYSDATE = 2024-02-07 00:17:24.259227
 WHERE intake_ts < SYSDATE;

SELECT name,
       data_size,
  FROM data_details
       -- SYSDATE = 2024-02-07 00:18:04.830527
 WHERE current_intake_date < SYSDATE;
Introductie tot Redshift

Voorbeeld: statements groeperen

data_intake

name priority
idaho_monitoring_locations 1
idaho_samples 2
idaho_site_id 3
UPDATE data_intake 
   SET priority=1 
 WHERE name='idaho_samples';

UPDATE data_intake 
   SET priority=2 
 WHERE name='idaho_monitoring_locations';
Introductie tot Redshift

Tabelresultaten met fout

data_intake

name priority
idaho_monitoring_locations 1
idaho_samples 1
idaho_site_id 3
Introductie tot Redshift

Voordelen en aandachtspunten van transacties

  • Consistente data-uitkomsten
  • Vereist slagen of falen voor een groep queries
  • Gelijktijdige operaties

Standaard uitvoergedrag

  • Elk SQL-statement is een transactie!

Transacties beïnvloeden sommige functies

  • Vastgezet bij start van de transactie en blijft gelijk
    • SYSDATE, TIMESTAMP, CURRENT_DATE

Sommige functies omzeilen transacties

  • Bepaald bij elke statement-uitvoering
    • GETDATE, TIMEOFDAY
Introductie tot Redshift

Structuur van transacties

  • Start met BEGIN; of START TRANSACTION;
  • Bevat één of meer SQL-statements, elk eindigend met een puntkomma
  • Eindigt met END; of COMMIT;
  • LET OP: puntkomma’s zijn belangrijk
BEGIN;

query1; query2;
END;
Introductie tot Redshift

Consistente queryresultaten krijgen

-- Start een transactie
BEGIN;
SELECT name,
       priority,
  FROM data_log
       -- SYSDATE = 2024-02-07 00:17:24.259227
 WHERE intake_ts < SYSDATE;

SELECT name,
       data_size,
  FROM data_details
       -- SYSDATE = 2024-02-07 00:17:24.259227
 WHERE current_intake_date < SYSDATE;
-- Einde van de transactie
END;
Introductie tot Redshift

Functiegedrag in transacties

-- Start een transactie
BEGIN;
SELECT name,
       priority,
  FROM data_intake
       -- GETDATE = 2024-02-07 00:17:24.259227
 WHERE data_intake_ts < GETDATE();

 SELECT name,
       data_size,
  FROM data_details
       -- GETDATE = 2024-02-07 00:18:44.830527
 WHERE current_intake_date < GETDATE();
-- Einde van de transactie
END;
Introductie tot Redshift

Laten we oefenen!

Introductie tot Redshift

Preparing Video For Download...