Agent Wranglers

DEN-6Recommended

Cap what each query and each month can cost

Set a maximum of bytes scanned or credits per query, and a monthly budget with alerts for the warehouse. Partition large tables by date and require a date filter on them.

How to do it

Warehouses that charge per scan bill each query, so one SELECT * over a year of events can cost more than the month's plan. Set the limit on the account, or on each tool's connection, such as the maximum bytes billed. Partition by event date, so a query for one week scans one week. Set the account budget as Give every account a budget with alerts does.

Examples

Python example

from google.cloud import bigquery

MAX_BYTES_PER_QUERY = 10 * 1024**3

def run_report(client: bigquery.Client, sql: str):
    config = bigquery.QueryJobConfig(maximum_bytes_billed=MAX_BYTES_PER_QUERY)
    return client.query(sql, job_config=config).result()

JavaScript example

import { BigQuery } from '@google-cloud/bigquery';

const MAX_BYTES_PER_QUERY = String(10 * 1024 ** 3);

export async function runReport(sql) {
  const [job] = await new BigQuery().createQueryJob({ query: sql, maximumBytesBilled: MAX_BYTES_PER_QUERY });
  const [rows] = await job.getQueryResults();
  return rows;
}

How to check

A query on a partitioned table without a date filter is refused. A test query above the per-query limit fails before it runs. The warehouse appears in the budget alerts with its owner.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data-engineering/warehouse-and-models/query-cost-limits">Agent Wranglers data engineering playbook</a>. In code you write, add a comment next to it with the same link.