DML-satser (Data Manipulation Language)

Introduktion till BigQuery

Matt Forrest

Field CTO

Översikt över datamanipulering i BigQuery

  • INSERT: Lägg till nya rader.
  • UPDATE: Ändra befintliga värden i en rad.
  • DELETE: Ta bort oönskad data från en tabell.
  • MERGE: Kombinerar INSERT, UPDATE och DELETE i en enda sats.
  • CREATE TABLE AS: Skapar en ny tabell från ett frågeresultat.
Introduktion till BigQuery

Överväganden och prestanda

  • Gruppera DML-satser när möjligt i stället för att köra dem en och en
  • Använd alltid en WHERE-villkorssats vid UPDATE
  • Överväg att använda tabellpartitioner och kluster
1 https://cloud.google.com/bigquery/docs/reference/standard-sql/data-manipulation-language
Introduktion till BigQuery

INSERT

  • Lägg till poster i tabeller
-- Define the columns in the parentheses 
INSERT INTO customers (customer_id, name, email)

-- Each value is a row to be inserted
VALUES (1, "John Doe", "[email protected]"),
(2, "Jane Doe", "[email protected]"),
(3, "Alice Smith", "[email protected]");
Introduktion till BigQuery

UPDATE

  • Ändra data baserat på ett villkor
UPDATE customers
-- Set one column for each SET statement
SET email = "[email protected]"
-- Make sure to include where otherwise all
-- rows will be updated
WHERE customer_id = 1;
  • UPDATE tillsammans med underfrågor eller joins
    UPDATE customers c
    SET c.email = e.email
    FROM emails e
    WHERE c.customer_id = 1;
    
Introduktion till BigQuery

DELETE

  • DELETE tar permanent bort poster och kan inte ångras
DELETE FROM customers

-- Include WHERE to ensure only specific rows are deleted
WHERE customer_id = 3;
DELETE FROM customers c
JOIN emails e USING (customer_id)
WHERE email = '[email protected]'
Introduktion till BigQuery

MERGE

  • Kombinerar INSERT, UPDATE och DELETE i en enda operation
-- Sets the customers table as the target
MERGE customers AS target

-- The source is set to new_customers USING new_customers AS source
-- Matching condition ON target.customer_id = source.customer_id
-- If the emails do not match, update the email WHEN MATCHED AND target.email != source.email THEN UPDATE SET email = source.email
-- If the match is not met, insert the record WHEN NOT MATCHED THEN INSERT (customer_id, name, email) VALUES (source.customer_id, source.name, source.email);
Introduktion till BigQuery

CREATE TABLE

  • Skapa nya tabeller från frågor
CREATE TABLE active_customers AS
SELECT customer_id, name, email 
FROM customers
WHERE last_active_date > DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY);
Introduktion till BigQuery

Nu kör vi en övning!

Introduktion till BigQuery

Preparing Video For Download...