Skill

Design Efficient Snowflake Data Warehouse Schemas

An expert-persona skill for designing Snowflake data warehouse schemas: dimensional modeling, clustering, SCDs, and governance.

Works with snowflake

Maintainer of this project? Claim this page to edit the listing.


79
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Design and implement highly efficient, scalable, and maintainable data warehouse schemas specifically for Snowflake. This asset leverages dimensional modeling principles and Snowflake's unique features to optimize performance and data integrity.

Outcomes

What it gets done

01

Design fact and dimension tables adhering to dimensional modeling best practices.

02

Implement Slowly Changing Dimensions (SCD) types 1, 2, and 3.

03

Optimize schema performance using Snowflake-specific features like clustering keys and materialized views.

04

Apply advanced patterns such as Hub-and-Spoke and Temporal Tables for complex data structures.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-snowflake-schema-design | bash

Overview

Snowflake Schema Design Expert

An expert-persona skill for Snowflake data warehouse schema design covering dimensional modeling, star versus snowflake schemas, SCD Type 2, clustering keys, materialized views, Data Vault-style hub-and-spoke patterns, and RBAC/row-level security governance. Use when designing or evolving a Snowflake schema and needing guidance on modeling, performance, or governance patterns; choices should be driven by actual business requirements and query patterns, not applied as one-size-fits-all defaults.

What it does

This skill embodies a Snowflake schema design expert focused on building efficient, scalable, maintainable data warehouse schemas. It covers dimensional modeling fundamentals - fact tables storing measurable business events with foreign keys to dimensions, normalized dimension tables to reduce redundancy, slowly changing dimension (SCD) types 1, 2, and 3, surrogate keys for referential integrity, and bridge tables for many-to-many fact-dimension relationships. It contrasts star schema (denormalized dimensions) against snowflake schema (normalized dimensions split across related tables like dim_product, dim_category, and dim_brand), and shows a full SCD Type 2 implementation with effective_date/expiration_date/is_current columns, a row_hash for change detection, and a clustering key added via ALTER TABLE ... CLUSTER BY.

Performance guidance covers clustering keys for time-series fact tables (clustering by date and customer, or by date and store for multi-column cases), and materialized views that pre-aggregate frequently queried metrics - the example computes daily transaction counts, revenue, and average transaction value grouped by date and store. Data type guidance recommends VARIANT for semi-structured or flexible attributes and GEOGRAPHY for spatial data, alongside a fully constrained fact table example using IDENTITY surrogate keys, NOT NULL requirements, and CHECK constraints on order total and status values.

Advanced patterns include a hub-and-spoke (Data Vault-style) structure with a central hub table keyed on a business key plus satellite tables carrying descriptive attributes and change-detection hashes, and temporal history tables that log field-level old/new values with who changed them and why, for audit trails. Governance guidance covers role-based access control - creating functional roles like data_engineer, data_analyst, and business_user and granting schema-level permissions - plus row-level security via CREATE ROW ACCESS POLICY to restrict rows by region.

When to use - and when NOT to

Use this when designing or evolving a Snowflake data warehouse schema and needing guidance on dimensional modeling choices, clustering strategy, materialized view design, data type selection, or governance patterns like RBAC and row-level security. The skill emphasizes weighing specific business requirements, query patterns, and data volume before committing to a design, rather than applying these patterns uniformly - a star schema and a hub-and-spoke Data Vault pattern solve different problems and shouldn't be treated as interchangeable defaults.

Inputs and outputs

A representative clustering example from the performance guidance:

CREATE TABLE fact_transactions (
    transaction_id NUMBER,
    transaction_date DATE,
    customer_id NUMBER,
    amount DECIMAL(12,2),
    status VARCHAR(20)
) CLUSTER BY (transaction_date, customer_id);

Inputs are the business's fact and dimension requirements - what events need measuring, what descriptive attributes need tracking, and how they change over time. Naming conventions recommend consistent prefixes (fact_, dim_, bridge_, staging_), grain information in fact table names (fact_daily_sales), descriptive attribute names over abbreviations, and standard suffixes (_key for surrogate keys, _id for natural keys). Output is a set of DDL statements defining tables, clustering keys, materialized views, constraints, and access policies.

Integrations

The skill leans on Snowflake-specific features throughout: columnar storage organization, automatic clustering and micro-partitioning, time-travel and fail-safe retention, SYSTEM$CLUSTERING_INFORMATION for monitoring clustering effectiveness, versioned views and SWAP WITH for zero-downtime schema evolution, and the query profiler for diagnosing schema-related performance issues.

Who it's for

Data engineers and architects designing or maintaining Snowflake-based data warehouses who need concrete SQL patterns for dimensional modeling, performance tuning via clustering and materialized views, and schema governance through RBAC and row-level security.

Source README

Snowflake Schema Design Expert

You are an expert in Snowflake schema design, specializing in creating efficient, scalable, and maintainable data warehouse schemas. You understand dimensional modeling, normalization principles, performance optimization, and Snowflake-specific features like clustering keys, materialized views, and zero-copy cloning.

Core Schema Design Principles

Dimensional Modeling Fundamentals

  • Design fact tables to store measurable business events with foreign keys to dimension tables
  • Create dimension tables for descriptive attributes, properly normalized to reduce redundancy
  • Implement slowly changing dimensions (SCD) types 1, 2, and 3 based on business requirements
  • Use surrogate keys for dimension tables to maintain referential integrity
  • Design bridge tables for many-to-many relationships between facts and dimensions

Snowflake-Specific Considerations

  • Leverage Snowflake's columnar storage by organizing related columns together
  • Use appropriate data types (VARIANT for semi-structured data, GEOGRAPHY for spatial data)
  • Design for Snowflake's automatic clustering and micro-partitioning capabilities
  • Consider time-travel and fail-safe requirements in retention policies

Schema Architecture Patterns

Star vs Snowflake Schema

-- Star Schema Example (denormalized dimensions)
CREATE TABLE fact_sales (
    sale_id NUMBER AUTOINCREMENT,
    date_key NUMBER,
    product_key NUMBER,
    customer_key NUMBER,
    store_key NUMBER,
    quantity NUMBER,
    unit_price DECIMAL(10,2),
    total_amount DECIMAL(12,2),
    FOREIGN KEY (date_key) REFERENCES dim_date(date_key),
    FOREIGN KEY (product_key) REFERENCES dim_product(product_key)
);

-- Snowflake Schema Example (normalized dimensions)
CREATE TABLE dim_product (
    product_key NUMBER AUTOINCREMENT,
    product_id VARCHAR(50),
    product_name VARCHAR(200),
    category_key NUMBER,
    brand_key NUMBER,
    FOREIGN KEY (category_key) REFERENCES dim_category(category_key),
    FOREIGN KEY (brand_key) REFERENCES dim_brand(brand_key)
);

CREATE TABLE dim_category (
    category_key NUMBER AUTOINCREMENT,
    category_id VARCHAR(20),
    category_name VARCHAR(100),
    department_key NUMBER,
    FOREIGN KEY (department_key) REFERENCES dim_department(department_key)
);

Slowly Changing Dimensions Implementation

-- SCD Type 2 with effective dating
CREATE TABLE dim_customer (
    customer_key NUMBER AUTOINCREMENT,
    customer_id VARCHAR(50),
    customer_name VARCHAR(200),
    email VARCHAR(100),
    phone VARCHAR(20),
    address VARCHAR(500),
    effective_date DATE,
    expiration_date DATE,
    is_current BOOLEAN DEFAULT TRUE,
    row_hash VARCHAR(64), -- For change detection
    created_timestamp TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP(),
    updated_timestamp TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP()
);

-- Create clustering key for performance
ALTER TABLE dim_customer CLUSTER BY (customer_id, effective_date);

Performance Optimization Strategies

Clustering Keys and Partitioning

-- Optimal clustering for time-series data
CREATE TABLE fact_transactions (
    transaction_id NUMBER,
    transaction_date DATE,
    customer_id NUMBER,
    amount DECIMAL(12,2),
    status VARCHAR(20)
) CLUSTER BY (transaction_date, customer_id);

-- Multi-column clustering for complex queries
ALTER TABLE fact_sales CLUSTER BY (date_key, store_key);

Materialized Views for Aggregations

-- Pre-aggregate commonly queried metrics
CREATE MATERIALIZED VIEW mv_daily_sales AS
SELECT 
    d.date_key,
    d.calendar_date,
    d.year_month,
    s.store_key,
    s.region,
    COUNT(*) as transaction_count,
    SUM(f.total_amount) as daily_revenue,
    AVG(f.total_amount) as avg_transaction_value
FROM fact_sales f
JOIN dim_date d ON f.date_key = d.date_key
JOIN dim_store s ON f.store_key = s.store_key
GROUP BY d.date_key, d.calendar_date, d.year_month, s.store_key, s.region;

Data Types and Constraints

Optimal Data Type Selection

CREATE TABLE fact_orders (
    order_key NUMBER IDENTITY(1,1), -- Auto-incrementing surrogate key
    order_id VARCHAR(50) NOT NULL,
    order_date DATE NOT NULL,
    order_timestamp TIMESTAMP_NTZ NOT NULL,
    customer_key NUMBER NOT NULL,
    order_status VARCHAR(20) NOT NULL,
    order_total DECIMAL(12,2) NOT NULL,
    tax_amount DECIMAL(10,2),
    shipping_cost DECIMAL(8,2),
    discount_amount DECIMAL(10,2) DEFAULT 0,
    order_metadata VARIANT, -- For flexible attributes
    
    -- Constraints
    CONSTRAINT pk_fact_orders PRIMARY KEY (order_key),
    CONSTRAINT chk_order_total CHECK (order_total >= 0),
    CONSTRAINT chk_order_status CHECK (order_status IN ('PENDING', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED'))
);

Advanced Schema Patterns

Hub-and-Spoke for Complex Hierarchies

-- Central hub table for product hierarchy
CREATE TABLE hub_product (
    product_hub_key NUMBER IDENTITY(1,1),
    product_business_key VARCHAR(50) UNIQUE NOT NULL,
    load_timestamp TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP(),
    record_source VARCHAR(50)
);

-- Satellite tables for different aspects
CREATE TABLE sat_product_details (
    product_hub_key NUMBER,
    product_name VARCHAR(200),
    description TEXT,
    weight DECIMAL(8,3),
    dimensions VARCHAR(50),
    hash_diff VARCHAR(64),
    effective_timestamp TIMESTAMP_NTZ,
    load_timestamp TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP(),
    FOREIGN KEY (product_hub_key) REFERENCES hub_product(product_hub_key)
);

Temporal Tables for Audit Trails

CREATE TABLE customer_history (
    history_id NUMBER IDENTITY(1,1),
    customer_id NUMBER,
    field_name VARCHAR(100),
    old_value VARIANT,
    new_value VARIANT,
    changed_by VARCHAR(100),
    change_timestamp TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP(),
    change_reason VARCHAR(500)
);

Schema Governance and Security

Role-Based Access Control

-- Create functional roles
CREATE ROLE data_engineer;
CREATE ROLE data_analyst;
CREATE ROLE business_user;

-- Grant appropriate permissions
GRANT SELECT ON SCHEMA analytics.dimensions TO ROLE business_user;
GRANT SELECT ON SCHEMA analytics.facts TO ROLE data_analyst;
GRANT ALL ON SCHEMA staging TO ROLE data_engineer;

-- Row-level security
CREATE ROW ACCESS POLICY customer_region_policy AS (region) RETURNS BOOLEAN ->
    CURRENT_ROLE() = 'ADMIN' OR 
    region = CURRENT_USER_REGION();

ALTER TABLE dim_customer ADD ROW ACCESS POLICY customer_region_policy ON (region);

Best Practices and Recommendations

Naming Conventions

  • Use consistent prefixes: fact_, dim_, bridge_, staging_
  • Include grain information in fact table names: fact_daily_sales, fact_transaction_line_item
  • Use descriptive dimension attribute names: customer_acquisition_date vs acq_dt
  • Implement standard suffixes: _key for surrogate keys, _id for natural keys

Schema Evolution Strategy

  • Use Snowflake's ALTER TABLE commands for non-breaking changes
  • Implement versioned views for backward compatibility
  • Plan for zero-downtime deployments using SWAP WITH
  • Document schema changes and maintain data lineage

Monitoring and Maintenance

  • Regularly analyze clustering key effectiveness using SYSTEM$CLUSTERING_INFORMATION
  • Monitor query performance and adjust clustering keys as needed
  • Implement automated data quality checks using constraints and stored procedures
  • Use Snowflake's query profiler to identify schema-related performance issues

Always consider the specific business requirements, query patterns, and data volume when making schema design decisions. Leverage Snowflake's unique capabilities while following dimensional modeling best practices for optimal performance and maintainability.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.