πŸ“Š Data Engineering

Snowflake

Snowflake

A cloud-native data warehouse. Separates compute and storage.

Detailed Explanation

Snowflake is a cloud-native data warehouse developed by the company of the same name, founded in 2012. With a multi-cloud architecture that operates identically on AWS, Azure, and GCP, it completely separates compute and storage so that each scales independently.

The architecture consists of three layers: Cloud Services (optimization, security, metadata), Query Processing (virtual warehouses), and Database Storage (micro-partitions). Virtual warehouses scale according to the query workload using T-shirt sizing (XS to 6XL) and automatically suspend when not in use to reduce costs.

Data Sharing is Snowflake's differentiating feature. It shares data between accounts in real time without copying the data, and lets you subscribe to third-party data on the Snowflake Marketplace. With Zero-Copy Cloning, you can instantly clone tables, schemas, and databases to quickly set up development/test environments.

Snowpark is a framework for running data processing logic inside Snowflake using Python, Java, and Scala. It transforms data with the DataFrame API and lets you write UDFs (User-Defined Functions) and Stored Procedures. ML workloads can also be processed inside Snowflake.

Code Example

-- λ°μ΄ν„°λ² μ΄μŠ€ 및 μŠ€ν‚€λ§ˆ 생성
CREATE DATABASE analytics_db;
CREATE SCHEMA analytics_db.raw;

-- 가상 μ›¨μ–΄ν•˜μš°μŠ€ 생성
CREATE WAREHOUSE analytics_wh
  WAREHOUSE_SIZE = 'MEDIUM'
  AUTO_SUSPEND = 300       -- 5λΆ„ ν›„ μžλ™ 쀑지
  AUTO_RESUME = TRUE
  MIN_CLUSTER_COUNT = 1
  MAX_CLUSTER_COUNT = 3;   -- λ©€ν‹° ν΄λŸ¬μŠ€ν„° (μžλ™ ν™•μž₯)

-- ν…Œμ΄λΈ” 생성
CREATE TABLE raw.sales (
    sale_id NUMBER AUTOINCREMENT,
    product_id NUMBER,
    customer_id NUMBER,
    sale_date DATE,
    amount DECIMAL(10,2),
    region VARCHAR(50)
);

-- S3μ—μ„œ 데이터 λ‘œλ“œ
COPY INTO raw.sales (product_id, customer_id, sale_date, amount, region)
FROM @my_s3_stage/sales/
FILE_FORMAT = (TYPE = 'PARQUET')
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE;

-- 뢄석 쿼리
SELECT
    DATE_TRUNC('month', sale_date) AS month,
    region,
    SUM(amount) AS total_sales,
    COUNT(DISTINCT customer_id) AS unique_customers
FROM raw.sales
WHERE sale_date >= '2024-01-01'
GROUP BY 1, 2
ORDER BY 1, total_sales DESC;

-- Zero-Copy Cloning (μ¦‰μ‹œ 볡제)
CREATE TABLE raw.sales_backup CLONE raw.sales;
CREATE DATABASE analytics_dev CLONE analytics_db;

-- Time Travel (κ³Όκ±° 데이터 쑰회, κΈ°λ³Έ 1일)
SELECT * FROM raw.sales AT (OFFSET => -60*60);  -- 1μ‹œκ°„ μ „
SELECT * FROM raw.sales BEFORE (TIMESTAMP => '2024-01-15 10:00:00');

-- Data Sharing (읽기 μ „μš© 곡유)
CREATE SHARE sales_share;
GRANT USAGE ON DATABASE analytics_db TO SHARE sales_share;
GRANT SELECT ON TABLE raw.sales TO SHARE sales_share;
ALTER SHARE sales_share ADD ACCOUNTS = partner_account;

-- Snowpark (Python)
-- from snowflake.snowpark import Session
-- from snowflake.snowpark.functions import col, sum as sum_
--
-- session = Session.builder.configs(connection_params).create()
--
-- df = session.table("raw.sales")
-- result = (df
--     .filter(col("sale_date") >= "2024-01-01")
--     .group_by("region")
--     .agg(sum_("amount").alias("total_sales"))
--     .sort(col("total_sales").desc())
-- )
-- result.show()

-- Python UDF 생성
CREATE OR REPLACE FUNCTION calculate_tax(amount FLOAT)
RETURNS FLOAT
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
HANDLER = 'compute_tax'
AS $$
def compute_tax(amount):
    return amount * 0.1
$$;

SELECT sale_id, amount, calculate_tax(amount) AS tax FROM raw.sales;

-- Stream (CDC)
CREATE STREAM sales_changes ON TABLE raw.sales;
-- λ³€κ²½ 사항 쑰회
SELECT * FROM sales_changes;

How It's Said in Practice

Senior: "If you need production data in the test environment, use Zero-Copy Cloning. It's cloned in a few seconds and doesn't add any storage cost."

Junior: "How do I decide the warehouse size?"

Senior: "It depends on query complexity. Use XS for simple lookups and L or larger for bulk ETL, and manage costs with AUTO_SUSPEND."

Interviewer: "Please tell me about your experience with Snowflake."

Candidate: "I migrated from Redshift to Snowflake. Separating compute and storage cut costs by 40%, and Zero-Copy Cloning reduced the time to build a development environment from a day to a matter of minutes. With Snowpark, I ran Python ML models inside Snowflake and performed batch inference without moving data."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "Check the warehouse size before running a large query. If you run it on XS, it will take several hours."

Developer: "I'll switch it to L and set AUTO_SUSPEND so it shuts off right after completion."

Cautions

Learn More