dwhat

← Guides

BigQuery and Snowflake

Fixes for expensive BigQuery and Snowflake queries, and how to tell which applies

A fix is worth suggesting only when something in the query or the warehouse's own statistics shows it applies. Each section below is one fix the vendor documents, the evidence that shows it applies, and SQL that finds the queries with that evidence. They are the rules dwhat uses for its fix hints.

A large read with no filter (BigQuery)

Evidence: a query billed 10 GiB or more a run and has no WHERE clause.

On a table partitioned by date, a filter on the partitioning column is what reduces bytes billed; on a clustered table, a filter on a clustering column helps too.

Large reads without WHERE
-- Queries that read 10 GiB or more a run with no WHERE clause, the last 30 days.
SELECT
  query,
  COUNT(*) AS runs,
  ROUND(AVG(total_bytes_billed) / POW(1024, 3), 1) AS gib_billed_per_run,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY' AND state = 'DONE'
  AND IFNULL(statement_type, '') != 'SCRIPT'
  AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0)
  AND total_bytes_billed >= 10 * POW(1024, 3)
  AND NOT REGEXP_CONTAINS(LOWER(query), r'\bwhere\b')
GROUP BY query
ORDER BY on_demand_usd DESC
LIMIT 20

Documented in: BigQuery: querying partitioned tables · BigQuery: best practices for controlling costs

LIMIT does not reduce what BigQuery bills

Evidence: a query with LIMIT n that billed 1 GiB or more a run.

BigQuery reads the columns in full before it drops rows, so LIMIT changes the result, not the bytes billed. For a preview, filter on the partitioning column, or read a sample with TABLESAMPLE.

LIMIT on large reads
-- Queries with a LIMIT that still billed 1 GiB or more a run, the last 30 days.
SELECT
  query,
  COUNT(*) AS runs,
  ROUND(AVG(total_bytes_billed) / POW(1024, 3), 1) AS gib_billed_per_run,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY' AND state = 'DONE'
  AND IFNULL(statement_type, '') != 'SCRIPT'
  AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0)
  AND total_bytes_billed >= POW(1024, 3)
  AND REGEXP_CONTAINS(LOWER(query), r'\blimit\s+\d+')
GROUP BY query
ORDER BY on_demand_usd DESC
LIMIT 20

Documented in: BigQuery: best practices for controlling costs

SELECT * (both)

Evidence: the query selects every column.

BigQuery bills each column read in full, so naming only the columns used cuts the cost directly. Snowflake stores data by column too; reading fewer columns is less work, which can let a smaller warehouse do the job.

BigQuery: SELECT * by cost
-- The most expensive queries that select every column, the last 30 days.
SELECT
  query,
  COUNT(*) AS runs,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY' AND state = 'DONE'
  AND IFNULL(statement_type, '') != 'SCRIPT'
  AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0)
  AND REGEXP_CONTAINS(LOWER(query), r'\bselect\s+(distinct\s+)?([a-z_]\w*\.)?\*')
GROUP BY query
ORDER BY tib_billed DESC
LIMIT 20
Snowflake: SELECT * by credits
-- The most expensive queries that select every column, the last 30 days.
SELECT
  a.query_parameterized_hash,
  ANY_VALUE(LEFT(q.query_text, 2000)) AS example_query,
  COUNT(*) AS runs,
  ROUND(SUM(a.credits_attributed_compute), 2) AS credits
FROM snowflake.account_usage.query_attribution_history a
JOIN snowflake.account_usage.query_history q
  ON q.query_id = a.query_id
 AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP())
WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
  AND REGEXP_INSTR(q.query_text, 'select\\s+(distinct\\s+)?([a-z_][a-z0-9_]*\\.)?\\*', 1, 1, 0, 'i') > 0
GROUP BY a.query_parameterized_hash
ORDER BY credits DESC
LIMIT 20

Documented in: BigQuery: best practices for controlling costs · Snowflake: micro-partitions and data clustering

Spilling to remote storage (Snowflake)

Evidence: bytes_spilled_to_remote_storage above zero in QUERY_HISTORY.

The warehouse ran out of memory and then local disk. Reduce the data earlier — filter before joins, fewer columns — or try the next warehouse size up: it costs twice as much per second, and is often cheaper overall when it ends the spilling.

Queries that spilled
-- Queries that spilled to remote storage, the last 30 days.
SELECT
  a.query_parameterized_hash,
  ANY_VALUE(LEFT(q.query_text, 2000)) AS example_query,
  ANY_VALUE(q.warehouse_size) AS a_warehouse_size,
  COUNT(*) AS runs,
  ROUND(AVG(q.bytes_spilled_to_remote_storage) / POW(1024, 3), 1) AS gib_spilled_remote_per_run,
  ROUND(SUM(a.credits_attributed_compute), 2) AS credits
FROM snowflake.account_usage.query_attribution_history a
JOIN snowflake.account_usage.query_history q
  ON q.query_id = a.query_id
 AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP())
WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
  AND q.bytes_spilled_to_remote_storage > 0
GROUP BY a.query_parameterized_hash
ORDER BY credits DESC
LIMIT 20

Documented in: Snowflake: queries too large to fit in memory

Filters that do not prune (Snowflake)

Evidence: a query scanned 80% or more of the micro-partitions of tables with 1,000 or more.

Snowflake skips micro-partitions whose range of values cannot match the filter. A filter on a column the data arrives in order of — usually a date — prunes; otherwise a clustering key on the column the query filters by can.

Queries scanning most partitions
-- Queries that scanned 80% or more of the micro-partitions of 1,000 or more, the last 30 days.
SELECT
  a.query_parameterized_hash,
  ANY_VALUE(LEFT(q.query_text, 2000)) AS example_query,
  COUNT(*) AS runs,
  ROUND(AVG(q.partitions_scanned / q.partitions_total) * 100) AS pct_partitions_scanned,
  ROUND(SUM(a.credits_attributed_compute), 2) AS credits
FROM snowflake.account_usage.query_attribution_history a
JOIN snowflake.account_usage.query_history q
  ON q.query_id = a.query_id
 AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP())
WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
  AND q.partitions_total >= 1000
  AND q.partitions_scanned >= 0.8 * q.partitions_total
GROUP BY a.query_parameterized_hash
ORDER BY credits DESC
LIMIT 20

Documented in: Snowflake: micro-partitions and data clustering

dbt models rebuilt in full every run (both)

Evidence: a query with dbt's node_id that creates its table from a SELECT.

A table model is rebuilt from scratch every run. If new rows are mostly appended, materialized='incremental' with an is_incremental() filter processes only what is new.

BigQuery: dbt full rebuilds
-- dbt models rebuilt in full (CREATE TABLE AS SELECT), the last 30 days.
WITH rebuilds AS (
  SELECT
    COALESCE(
      REGEXP_EXTRACT(query, r'"node_id": *"([^"]+)"'),
      (SELECT value FROM UNNEST(labels) WHERE key = 'node_id')
    ) AS dbt_node,
    total_bytes_billed
  FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
  WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
    AND job_type = 'QUERY' AND state = 'DONE'
    AND IFNULL(statement_type, '') != 'SCRIPT'
    AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0)
    AND statement_type = 'CREATE_TABLE_AS_SELECT'
    AND (
      REGEXP_CONTAINS(query, r'"app": *"dbt"')
      OR EXISTS (SELECT 1 FROM UNNEST(labels) WHERE key = 'app' AND value = 'dbt')
    )
)
SELECT
  dbt_node,
  COUNT(*) AS rebuilds,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd
FROM rebuilds
GROUP BY dbt_node
ORDER BY tib_billed DESC
Snowflake: dbt full rebuilds
-- dbt models rebuilt in full (CREATE TABLE AS SELECT), the last 30 days.
SELECT
  REGEXP_SUBSTR(q.query_text, '"node_id": *"([^"]+)"', 1, 1, 'e', 1) AS dbt_node,
  COUNT(*) AS rebuilds,
  ROUND(SUM(a.credits_attributed_compute), 2) AS credits
FROM snowflake.account_usage.query_attribution_history a
JOIN snowflake.account_usage.query_history q
  ON q.query_id = a.query_id
 AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP())
WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
  AND q.query_type = 'CREATE_TABLE_AS_SELECT'
  AND REGEXP_INSTR(q.query_text, '"app": *"dbt"') > 0
GROUP BY dbt_node
ORDER BY credits DESC

See a sample audit: a made-up dbt model rebuilt in full seven times on one day, and the hint it gets.

Documented in: dbt: incremental models

The same query all day (both)

Evidence: the same query, literals aside, ran 24 times a day or more.

If its answer only changes when the data is loaded, build it once per load — a dbt table or a materialised view — and point the dashboard at that, or lengthen the dashboard's refresh interval.

BigQuery: repeated queries
-- Queries that ran 24 times a day or more on average, the last 30 days.
SELECT
  query_info.query_hashes.normalized_literals AS query_hash,
  ANY_VALUE(query) AS example_query,
  COUNT(*) AS runs,
  ROUND(COUNT(*) / 30) AS runs_per_day,
  ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd,
  ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
  AND job_type = 'QUERY' AND state = 'DONE'
  AND IFNULL(statement_type, '') != 'SCRIPT'
  AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0)
  AND query_info.query_hashes.normalized_literals IS NOT NULL
GROUP BY query_hash
HAVING COUNT(*) >= 24 * 30
ORDER BY on_demand_usd DESC, slot_hours DESC
LIMIT 20
Snowflake: repeated queries
-- Queries that ran 24 times a day or more on average, the last 30 days.
SELECT
  a.query_parameterized_hash,
  ANY_VALUE(LEFT(q.query_text, 2000)) AS example_query,
  COUNT(*) AS runs,
  ROUND(COUNT(*) / 30) AS runs_per_day,
  ROUND(SUM(a.credits_attributed_compute), 2) AS credits
FROM snowflake.account_usage.query_attribution_history a
LEFT JOIN snowflake.account_usage.query_history q
  ON q.query_id = a.query_id
 AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP())
WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
GROUP BY a.query_parameterized_hash
HAVING COUNT(*) >= 24 * 30
ORDER BY credits DESC
LIMIT 20

See a sample audit: a made-up dashboard that refreshed every fifteen minutes for a day, and the hint it gets that day.

Documented in: dbt: materializations · BigQuery: introduction to materialized views · Snowflake: working with materialized views

What this misses

Run this every day instead

dwhat reads the same history with a read-only account, every day. It keeps the daily cost by user, dbt model and dashboard, emails you when a day costs well above its 14-day median and says what grew, and lists the 20 most expensive queries with the fixes above where the evidence supports them.

Connect a read-only account for a free audit

Related guides