Transactions

Introduction to Redshift

Jason Myers

Principal Engineer

แรงจูงใจในการใช้ Transactions

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;
Introduction to Redshift

ตัวอย่างการจัดกลุ่มคำสั่ง

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';
Introduction to Redshift

ผลลัพธ์ของตารางที่เกิดข้อผิดพลาด

data_intake

name priority
idaho_monitoring_locations 1
idaho_samples 1
idaho_site_id 3
Introduction to Redshift

ข้อดีและข้อควรพิจารณาของ Transactions

  • ผลลัพธ์ของข้อมูลที่สอดคล้องกัน
  • กำหนดให้กลุ่มคิวรีสำเร็จหรือล้มเหลวพร้อมกัน
  • การทำงานพร้อมกัน

พฤติกรรมการดำเนินการเริ่มต้น

  • คำสั่ง SQL แต่ละคำสั่งคือ transaction!

Transactions ส่งผลต่อฟังก์ชันบางอย่าง

  • กำหนดค่าเมื่อเริ่ม transaction และคงที่ตลอด
    • SYSDATE, TIMESTAMP, CURRENT_DATE

ฟังก์ชันบางอย่างทำงานนอก Transactions

  • กำหนดค่าใหม่ทุกครั้งที่รันคำสั่ง
    • GETDATE, TIMEOFDAY
Introduction to Redshift

โครงสร้างของ Transactions

  • เปิดด้วย BEGIN; หรือ START TRANSACTION;
  • ประกอบด้วยคำสั่ง SQL ตั้งแต่หนึ่งคำสั่งขึ้นไป โดยต่อท้ายแต่ละคำสั่งด้วยเซมิโคลอน
  • ปิดด้วย END; หรือ COMMIT;
  • หมายเหตุ: เซมิโคลอนมีความสำคัญ
BEGIN;

query1; query2;
END;
Introduction to Redshift

การรับผลลัพธ์คิวรีที่สอดคล้องกัน

-- Start a transaction
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;
-- End a transaction
END;
Introduction to Redshift

พฤติกรรมของฟังก์ชันใน Transactions

-- Start a transaction
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();
-- End a transaction
END;
Introduction to Redshift

มาฝึกกันเถอะ!

Introduction to Redshift

Preparing Video For Download...