Inicio / Skills / Productividad del desarrollador / BigQuery Optimization
Productividad del desarrollador

BigQuery Optimization

GoogleApache-2.0Idioma del Skill: Inglés
Licencia verificadaSin scriptsRequiere un servicio externoRequiere credenciales
DemostraciónEjecución real · claude-sonnet-5-5 · octubre de 2026
Optimize this BigQuery query (project shop-prod, I don't know the region): SELECT * FROM shop.orders WHERE EXTRACT(YEAR FROM order_ts) = 2025 AND customer_id NOT IN (SELECT customer_id FROM shop.blocked) ORDER BY order_ts
Skill: bigquery-optimization
Read: references/sql_optimization.md
Read: references/table_partitioning_clustering.md

Scope: project shop-prod, region us. You didn't give a region, so I assumed us. If shop lives elsewhere, use your region in any INFORMATION_SCHEMA queries (for example region-europe-west1). I haven't run anything against your project. This rewrite comes from the query text alone, and I'm assuming order_ts is a TIMESTAMP.

Optimized query

SELECT
  order_id,        -- replace with the columns you actually need
  customer_id,
  order_ts
FROM
  `shop-prod.shop.orders`
WHERE
  order_ts >= TIMESTAMP '2025-01-01 00:00:00'
  AND order_ts <  TIMESTAMP '2026-01-01 00:00:00'
  AND customer_id NOT IN (
    SELECT customer_id
    FROM `shop-prod.shop.blocked`
    WHERE customer_id IS NOT NULL
  )
ORDER BY
  order_ts
LIMIT 1000;  -- remove or adjust if you really need every row

Summary of optimizations

  1. Replaced EXTRACT(YEAR FROM order_ts) = 2025 with a range filter. A function on the column prevents partition pruning and block pruning. A plain range on order_ts lets BigQuery prune statically.
  2. Added WHERE customer_id IS NOT NULL to the NOT IN subquery. This lets BigQuery use an anti-hash join instead of the slower left outer hash join.
    • Behavior change: if shop.blocked contains a NULL customer_id, the original query returns zero rows, because NOT IN with a NULL evaluates to NULL. The rewrite returns the rows you almost certainly intended. If you somehow relied on the old behavior, tell me.
  3. Replaced SELECT * with an explicit column list. This reads less data. I don't know your schema, so fill in the columns you need.
  4. Added LIMIT to the ORDER BY. A full sort of a year of orders is expensive. If you need all rows, drop the LIMIT, or consider whether the sort is needed at all.

Table layout (biggest lever if the table is large)

The range filter only prunes partitions if shop.orders is partitioned on order_ts. To check, run:

bq show --format=prettyjson shop-prod:shop.orders

Look at timePartitioning, clustering and requirePartitionFilter in the output.

If the table is not partitioned, and each day holds roughly 10 GB or more (otherwise cluster only), you can create a partitioned copy:

CREATE TABLE `shop-prod.shop.orders_partitioned`
  PARTITION BY TIMESTAMP_TRUNC(order_ts, DAY)
  CLUSTER BY customer_id
  OPTIONS (require_partition_filter = TRUE)
AS
SELECT * FROM `shop-prod.shop.orders`;

You can't convert the existing table in place. Note that require_partition_filter = TRUE makes any query without an order_ts filter fail.

Validate before running

A dry run shows the bytes the query would process, and it costs nothing:

bq query --use_legacy_sql=false --dry_run "<the optimized query>"

Run it on the original query and on the rewrite, and compare totalBytesProcessed. The dry run won't show savings from clustering, since those happen at execution time.

These changes should improve cost-efficiency, but I can't promise a particular saving until you've compared the dry-run numbers.

Trial with only the Skill and Read tools: no Google Cloud access, no gcloud or bq. So only the SQL-rewrite path was exercised, from the query text alone; the model proposed the bq show and dry-run commands but did not run them. Capacity, billing and gcloud workflows were read but not tried.

Qué hace
Proporciona al modelo las pautas de Google para optimizar BigQuery. Reescribe SQL, propone estructuras de tablas y explica las opciones de facturación; le indica que te deje ejecutar los cambios que afecten al costo.

Cómo funciona

  1. Optimización SQL con reglas concretas: filtros por rango en vez de EXTRACT, NOT IN seguro con valores nulos, LIMIT junto con ORDER BY, evitar SELECT * en el nivel superior, agregar datos antes de los joins y más. Siempre termina con un resumen de cambios.
  2. Diseño de tablas: particiones frente a clústeres, plantillas DDL, migración de tablas sin particionar y require_partition_filter.
  3. Almacenamiento y facturación: almacenamiento lógico o físico, recuperación histórica (time travel), caducidad de particiones y cuándo convienen los índices de búsqueda, las vistas materializadas o BI Engine.
  4. Capacidad: interpretar las recomendaciones de Slot Estimator para On-Demand y Editions. Los compromisos, las reservas y los cambios de modelo de facturación quedan en tus manos, desde Cloud Console.

Ideal para
Optimizar consultas costosas, diseñar o corregir tablas grandes y revisar la factura de BigQuery.

Limitaciones
La parte SQL funciona solo con el texto de la consulta. El análisis necesita Google Cloud SDK y acceso al proyecto; no sustituye la comprobación de las estimaciones de una ejecución de prueba (dry run).

Notas y riesgos

Riesgo medio: no incluye scripts, pero indica al agente que ejecute `gcloud` y `bq` en tu proyecto. Requiere Google Cloud SDK, una cuenta conectada o archivo de clave de servicio, facturación y permisos IAM; menciona administrador de BigQuery, editor de datos y usuario de trabajos. `gcloud config set project` y `gcloud services enable` cambian la configuración; también propone instalar Skills complementarias con `npx skills add ...`. Pide etiquetar sus llamadas `gcloud` para atribuir el uso y enviar a Google el nombre y la versión de la Skill en User-Agent; puedes pedir que se desactive. Prohíbe al agente comprar compromisos, cambiar la facturación o ejecutar DDL: debe proponértelo para que lo hagas tú. Aun así, revisa cada comando antes de autorizarlo y empieza en un proyecto de pruebas. Las consultas y los cambios de almacenamiento tienen costo. En nuestra prueba solo se reescribió SQL, sin acceder a la nube.

SKILL.md original11 archivos en este paquete