Gcp bigquery

From j4flmao/agent-skills · View interactive version

Gcp bigquery is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about BigQuery cost, bytes scanned, partitioning and clustering, slots and editions, slow BigQuery queries, or Dremel and Capacitor internals. Produces table designs, query fixes and a pricing-model choice. Not for warehouse vendor comparison (use data-data-warehouse) or dbt p

How to use this skill

Skill file

GCP BigQuery Internals and Optimization

Purpose

BigQuery cost and speed are determined by bytes read and slot time. Knowing how Dremel distributes work and how Capacitor stores columns explains why partitioning, clustering, and column selection matter more than anything else.

Agent Protocol

Trigger

"bigquery", "bigquery cost", "partitioning", "clustering", "dremel", "capacitor", "slots", "editions", "slow query", "bytes scanned".

Input Context

Output Artifact

DDL, rewritten SQL, or reservation plan. No file unless requested.

Response Format

Problem: {bytes scanned|slot contention|shuffle/skew|streaming cost}
Change: {DDL/SQL}
Expected effect: {partitions pruned / columns avoided}
Verify with: {dry run bytes, INFORMATION_SCHEMA.JOBS}

Completion Criteria

Architecture

flowchart LR
  Q[SQL] --> P[Query planner]
  P --> S1[Stage 1: scan + filter on slots]
  S1 --> SH[(Distributed shuffle)]
  SH --> S2[Stage 2: join / aggregate]
  S2 --> R[Result]
  S1 -.reads columns.-> C[(Capacitor files on Colossus)]

Pricing Models

| Model | You pay for | Fit |

|---|---|---|

| On-demand | Bytes processed per query (logical bytes; first 1 TiB/month free) | Spiky, small/medium usage |

| Editions (Standard, Enterprise, Enterprise Plus) | Slot-hours via reservations with autoscaling (optional baseline / commitments) | Predictable heavy usage, workload isolation |

| Storage | Logical or physical (compressed) bytes, chosen per dataset; long-term rate after 90 days unmodified | Physical billing often cheaper for well-compressed data |

On-demand queries are capped by a per-project concurrent slot quota; heavy concurrency is the usual reason to move to reservations.

Table Design

CREATE TABLE analytics.events
(
  event_ts    TIMESTAMP NOT NULL,
  customer_id STRING,
  event_type  STRING,
  country     STRING,
  payload     JSON
)
PARTITION BY DATE(event_ts)
CLUSTER BY customer_id, event_type
OPTIONS (
  require_partition_filter = TRUE,
  partition_expiration_days = 400
);

Query Tuning Rules

| Symptom | Cause | Fix |

|---|---|---|

| Huge bytes billed | SELECT *, no partition filter | Select needed columns; filter on partition column directly (not wrapped in functions of other columns) |

| LIMIT didn't reduce cost | LIMIT applies after the scan | Filter instead; use table preview for sampling |

| Slow join, "skew" in plan | One key with massive row count | Pre-aggregate, filter nulls/hot keys, or split hot key |

| Repeated identical dashboards | Recomputing aggregates | Materialized views, BI Engine, or scheduled aggregate tables |

| Subquery re-evaluated | Non-deterministic or complex CTE used multiple times | Materialize to a temp table in a multi-statement query |

| ORDER BY on large result fails | Final sort on single worker | Add LIMIT with ORDER BY, or sort downstream |

Inspect cost:

SELECT user_email, query, total_bytes_billed / POW(1024, 4) AS tib_billed, total_slot_ms
FROM `region-eu`.INFORMATION_SCHEMA.JOBS
WHERE creation_time > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
  AND job_type = 'QUERY'
ORDER BY total_bytes_billed DESC
LIMIT 20;

Dry run: bq query --use_legacy_sql=false --dry_run 'SELECT ...'.

Ingestion

Anti-Patterns

Checklist

References

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.