Warehouse cost optimization snowflake bq

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Warehouse cost optimization snowflake bq is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Snowflake QUERY_HISTORY: https://docs.snowflake.com/en/sql-reference/account-usage/query_history

How to use this skill

Skill file

<!--

Snowflake QUERY_HISTORY: https://docs.snowflake.com/en/sql-reference/account-usage/query_history

BigQuery INFORMATION_SCHEMA: https://cloud.google.com/bigquery/docs/information-schema-jobs

Databricks query history: https://docs.databricks.com/sql/admin/system-tables.html

Companion: role.md → "Warehouse cost playbook"

-->

Warehouse cost optimization — Snowflake credits, BigQuery slots, Databricks DBUs

Identify, attribute, and reduce warehouse costs. Build a credit/slot/DBU attribution dashboard from QUERY_HISTORY / INFORMATION_SCHEMA.JOBS_BY_PROJECT / system tables. Apply auto-suspend, multi-cluster, materialized views, partitioning + clustering, and dbt materialization audits.

When to use

Defer dialect-specific patterns to snowflake-bigquery-databricks-warehousing. Defer dbt structure to dbt-modeling-staging-marts. Defer pipeline-side cost (ELT) to dlt-fivetran-airbyte-elt.

Setup

pip install snowflake-connector-python google-cloud-bigquery databricks-sql-connector
pip install pandas plotly

Auth requirements:

Common recipes — Snowflake

Recipe 1 — Top 20 most-expensive queries (last 7 days)

USE WAREHOUSE COMPUTE_WH;
USE DATABASE SNOWFLAKE;
USE SCHEMA ACCOUNT_USAGE;

SELECT
    SUBSTR(query_text, 1, 200) AS query_text,
    warehouse_name,
    user_name,
    role_name,
    DATEDIFF('second', start_time, end_time) AS exec_seconds,
    bytes_scanned / 1e9 AS gb_scanned,
    rows_produced,
    credits_used_cloud_services + COALESCE(credits_used, 0) AS total_credits,
    execution_status,
    query_tag
FROM query_history
WHERE start_time > CURRENT_DATE - INTERVAL '7 days'
  AND execution_status = 'SUCCESS'
ORDER BY total_credits DESC
LIMIT 20;

Recipe 2 — Credit attribution by warehouse + user

SELECT
    warehouse_name,
    user_name,
    DATE_TRUNC('day', start_time) AS query_day,
    COUNT(*) AS query_count,
    SUM(credits_used_cloud_services + COALESCE(credits_used, 0)) AS total_credits,
    SUM(bytes_scanned) / 1e12 AS tb_scanned,
    AVG(DATEDIFF('second', start_time, end_time)) AS avg_exec_seconds
FROM snowflake.account_usage.query_history
WHERE start_time > CURRENT_DATE - INTERVAL '30 days'
GROUP BY 1, 2, 3
ORDER BY total_credits DESC;

Recipe 3 — Warehouse load + concurrency analysis

SELECT
    warehouse_name,
    DATE_TRUNC('hour', start_time) AS hour_bucket,
    AVG(avg_running) AS avg_concurrency,
    MAX(avg_running) AS max_concurrency,
    AVG(avg_queued_load) AS avg_queued
FROM snowflake.account_usage.warehouse_load_history
WHERE start_time > CURRENT_DATE - INTERVAL '7 days'
GROUP BY 1, 2
ORDER BY 1, 2;

-- If avg_queued > 0.5, consider multi-cluster
-- If max_concurrency < 1, the warehouse is oversized

Recipe 4 — Auto-suspend optimization

-- Find warehouses with high idle time
WITH suspend_intervals AS (
    SELECT warehouse_name, start_time, end_time, EVENT_NAME
    FROM snowflake.account_usage.warehouse_events_history
    WHERE start_time > CURRENT_DATE - INTERVAL '7 days'
      AND EVENT_NAME IN ('SUSPEND_WAREHOUSE', 'RESUME_WAREHOUSE')
)
-- Compute time spent running but idle
-- (complex — for production use Snowsight's warehouse insights instead)
SELECT warehouse_name, COUNT(*) AS toggle_count
FROM suspend_intervals GROUP BY 1 ORDER BY toggle_count DESC;

Tune auto-suspend:

-- Tight (default): suspend after 60s idle
ALTER WAREHOUSE compute_wh SET AUTO_SUSPEND = 60;
-- Loose (spiky workloads): suspend after 5 min idle
ALTER WAREHOUSE etl_wh SET AUTO_SUSPEND = 300;
-- Aggressive (interactive BI): suspend after 30s
ALTER WAREHOUSE bi_wh SET AUTO_SUSPEND = 30;

Recipe 5 — Query Acceleration Service eligibility

-- Find queries that would benefit from QAS
SELECT
    query_id,
    query_text,
    bytes_scanned / 1e9 AS gb_scanned,
    eligible_query_acceleration_time,
    upper_limit_scale_factor
FROM snowflake.account_usage.query_acceleration_eligible
WHERE start_time > CURRENT_DATE - INTERVAL '7 days'
ORDER BY eligible_query_acceleration_time DESC
LIMIT 20;

-- Enable QAS on the warehouse
ALTER WAREHOUSE compute_wh SET QUERY_ACCELERATION_MAX_SCALE_FACTOR = 8;

Common recipes — BigQuery

Recipe 6 — BigQuery slot + cost analysis

SELECT
    user_email,
    project_id,
    SUBSTR(query, 1, 200) AS query_text,
    creation_time,
    total_slot_ms / 1000.0 / 3600 AS slot_hours,
    total_bytes_processed / 1e9 AS gb_processed,
    total_bytes_billed / 1e9 AS gb_billed,
    -- On-demand pricing: $5/TB billed (2025)
    total_bytes_billed / 1e12 * 5 AS estimated_cost_usd,
    state
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
  AND job_type = 'QUERY'
  AND state = 'DONE'
ORDER BY total_slot_ms DESC
LIMIT 50;

Recipe 7 — BigQuery slot reservation usage

SELECT
    reservation_id,
    DATE(period_start) AS day,
    SUM(slots_assigned) AS total_slot_seconds_assigned,
    SUM(slots_used) AS total_slot_seconds_used,
    SAFE_DIVIDE(SUM(slots_used), SUM(slots_assigned)) AS utilization_pct
FROM `region-us`.INFORMATION_SCHEMA.RESERVATIONS_TIMELINE
WHERE period_start > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
GROUP BY 1, 2
ORDER BY 1, 2;

Recipe 8 — Partition pruning effectiveness

-- Find queries that did not benefit from partition pruning
SELECT
    SUBSTR(query, 1, 200) AS query,
    user_email,
    referenced_tables[OFFSET(0)].dataset_id AS dataset,
    referenced_tables[OFFSET(0)].table_id AS table,
    total_bytes_processed / 1e9 AS gb_processed,
    statement_type
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT,
UNNEST(referenced_tables) AS rt
WHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
  AND total_bytes_processed > 100e9            -- scanned > 100GB
  AND statement_type = 'SELECT'
ORDER BY total_bytes_processed DESC LIMIT 20;

For each result, check if a partition filter would help; add WHERE order_date BETWEEN ... clause.

Recipe 9 — Materialized view candidates

-- Find frequently-repeated queries — MV candidates
SELECT
    SUBSTR(query, 1, 100) AS query_pattern,
    COUNT(*) AS exec_count,
    SUM(total_bytes_processed) / 1e9 AS total_gb_processed,
    AVG(total_slot_ms) AS avg_slot_ms
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY'
GROUP BY 1
HAVING exec_count > 100
ORDER BY total_gb_processed DESC LIMIT 20;

Create MV:

CREATE MATERIALIZED VIEW my_dataset.mv_daily_revenue
PARTITION BY order_date
CLUSTER BY region AS
SELECT order_date, region, SUM(amount) AS revenue
FROM my_dataset.fct_orders
GROUP BY 1, 2;

Common recipes — Databricks

Recipe 10 — Databricks DBU consumption (system tables)

SELECT
    workspace_id,
    sku_name,
    DATE(usage_date) AS usage_day,
    SUM(usage_quantity) AS dbu_consumed,
    SUM(usage_quantity * list_prices.pricing.default) AS estimated_cost_usd
FROM system.billing.usage u
JOIN system.billing.list_prices ON u.sku_name = list_prices.sku_name AND CURRENT_DATE() BETWEEN list_prices.price_start_time AND COALESCE(list_prices.price_end_time, '2999-12-31')
WHERE usage_date >= current_date - INTERVAL 7 DAYS
GROUP BY 1, 2, 3
ORDER BY estimated_cost_usd DESC;
-- Most expensive queries
SELECT
    statement_id,
    executed_by,
    start_time,
    end_time,
    total_duration_ms / 1000 AS exec_seconds,
    total_task_duration_ms / 1000 AS task_seconds,
    read_bytes / 1e9 AS gb_read,
    statement_text
FROM system.query.history
WHERE start_time >= current_timestamp() - INTERVAL 7 DAYS
ORDER BY total_task_duration_ms DESC
LIMIT 20;

Recipe 11 — dbt materialization audit

import json
from pathlib import Path

# Read dbt manifest.json after `dbt parse`
manifest = json.loads(Path("target/manifest.json").read_text())

models = []
for node_id, node in manifest["nodes"].items():
    if node["resource_type"] == "model":
        models.append({
            "model": node["name"],
            "materialization": node["config"]["materialized"],
            "tags": node["tags"],
            "path": node["path"],
        })

import pandas as pd
df = pd.DataFrame(models)
print(df.materialization.value_counts())

# For "table" models that are large + append-only, consider incremental
# For "incremental" models with small dataset, downgrade to "table"
# For models referenced only once, consider "ephemeral"

Recipe 12 — Cost monitoring dashboard

-- Daily credit/slot/DBU dashboard query
SELECT
    DATE_TRUNC('day', start_time) AS day,
    warehouse_name,
    SUM(credits_used_cloud_services + COALESCE(credits_used, 0)) AS credits,
    COUNT(*) AS query_count,
    AVG(DATEDIFF('second', start_time, end_time)) AS avg_exec_seconds
FROM snowflake.account_usage.query_history
WHERE start_time > CURRENT_DATE - INTERVAL '30 days'
GROUP BY 1, 2
ORDER BY 1, 2;

Then dashboard via Metabase/Hex/Evidence with anomaly alerts (Recipe 13).

Recipe 13 — Cost anomaly alert

import pandas as pd

def cost_anomaly_check(daily_credits_series, k_sigma=2.5):
    """Alert when today's credits are >k_sigma above 28-day rolling mean."""
    rolling = daily_credits_series.rolling(28, min_periods=7)
    mean = rolling.mean()
    std = rolling.std()
    z = (daily_credits_series - mean) / std
    return daily_credits_series[z > k_sigma].index.tolist()

anomalies = cost_anomaly_check(df.set_index("day")["credits"])
if anomalies:
    print(f"COST SPIKE on: {anomalies}")

Example end-to-end

Goal: Snowflake bill jumped from $8k to $13k in May. Diagnose + fix.

  1. Run Recipe 1 → top-20 expensive queries; spot a 4× spike from MARKETING_WH.
  2. Recipe 2: drill into MARKETING_WH — most credits from bi_user@company.com.
  3. Inspect query: a Looker dashboard query scanning fct_events without partition filter; runs every 5 minutes.
  4. Recipe 3: MARKETING_WH has avg_concurrency = 0.3, max_queue = 0 → oversized at L; downsize to S.
  5. Add WHERE event_date >= CURRENT_DATE - 7 to Looker LookML; verify pruning via Snowsight Query Profile.
  6. Recipe 9: query is repeated → create materialized view mv_daily_event_summary.
  7. Recipe 11: audit dbt; fct_events is table but should be incremental on event_date.
  8. Save: Snowflake bill projected $7k for next month.
  9. Set up Recipe 12 + 13 dashboard + Slack alert for future spikes.

Edge cases / gotchas

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.