optimizing-query-by-id

optimizing-query-by-id is a skill for Claude Code, Codex from AltimateAI/data-engineering-skills. It costs 121 tokens per session (912 once invoked), scanned A, original, MIT.

A troubleshooting workflow for improving a Snowflake database query when you have its query ID. Snowflake is a cloud data platform, and a query ID identifies one query run in its history.

In plain words
What is it for?
Use it to inspect query history, review database operations, find excessive scanning, data expansion, sorting, or memory spill, and suggest query changes.
Why use it?
It helps locate why a query is slow or reads too much data by examining its execution details and profile.

Skill for Claude CodeCodex

Part of the snowflake-skills plugin — 3 skills shipped together

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/altimateai/data-engineering-skills/optimizing-query-by-id
Any agent
npx skills add AltimateAI/data-engineering-skills --skill optimizing-query-by-id
Clone the repo
git clone --depth 1 https://github.com/AltimateAI/data-engineering-skills

Made for: Claude Code, Codex.

Or install snowflake-skills, the plugin that ships this one along with the rest of its 3 skills.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for optimizing-query-by-id

README.md
[![agentmods](https://agentmods.dev/badge/skills/altimateai/data-engineering-skills/optimizing-query-by-id.svg)](https://agentmods.dev/skills/altimateai/data-engineering-skills/optimizing-query-by-id)
Your own site
<a href="https://agentmods.dev/skills/altimateai/data-engineering-skills/optimizing-query-by-id"><img src="https://agentmods.dev/badge/skills/altimateai/data-engineering-skills/optimizing-query-by-id.svg" alt="Measured on agentmods" height="20"></a>
Per session 121 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 912 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00121 $0.00912
Opus 5 $0.00060 $0.00456
Sonnet 5 $0.00024 $0.00182
Haiku 4.5 $0.00012 $0.00091

Measured 5d ago against content hash 75953e4bb991, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-05, from the pricing page.

Security

Grade A, and why

optimizing-query-by-id scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 5d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/snowflake/optimizing-query-by-id/SKILL.md · 133 lines

How it starts

The opening of the file, as written. The whole thing — 133 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Optimize Query from Query ID

Fetch query → Get profile → Apply best practices → Verify improvement → Return optimized query

Workflow

1. Fetch Query Details from Query ID

SELECT
    query_id,
    query_text,
    total_elapsed_time/1000 as seconds,
    bytes_scanned/1e9 as gb_scanned,
    bytes_spilled_to_local_storage/1e9 as gb_spilled_local,
    bytes_spilled_to_remote_storage/1e9 as gb_spilled_remote,
    partitions_scanned,
    partitions_total,
    rows_produced
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY())
WHERE query_id = '<query_id>';

Note the key metrics:

  • seconds: Total execution time
  • gb_scanned: Data read (lower is better)
  • gb_spilled: Spillage indicates memory pressure
  • partitions_scanned/total: Partition pruning effectiveness

2. Get Query Profile Details

-- Get operator-level statistics
SELECT *
FROM TABLE(GET_QUERY_OPERATOR_STATS('<query_id>'));

Look for:

  • Operators with high output_rows vs input_rows (explosions)
  • TableScan operators with high bytes
  • Sort/Aggregate operators with spillage

3. Identify Optimization Opportunities

Based on profile, look for:

Metric Issue Fix
partitions_scanned = partitions_total No pruning Add filter on cluster key
gb_spilled > 0 Memory pressure Simplify query, increase warehouse
High bytes_scanned Full scan Add selective filters, reduce columns
Join explosion Cartesian or bad key Fix join condition, filter before join

4. Apply Optimizations

Rewrite the query:

  • Select only needed columns
  • Filter early (before joins)
  • Use CTEs to avoid repeated scans
  • Ensure filters align with clustering keys
  • Add LIMIT if full result not needed

5. Get Explain Plan for Optimized Query

EXPLAIN USING JSON
<optimized_query>;

6. Compare Plans

Compare original vs optimized:

  • Fewer partitions scanned?
  • Fewer intermediate rows?
  • Better join order?

7. Return Results

Provide:

  1. Original query metrics (time, data scanned, spillage)
  2. Identified issues
  3. The optimized query
  4. Summary of changes made
  5. Expected improvement

Read the full file on GitHub · 133 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 5d ago First seen · 133 lines · 121 tokens per session scan A 75953e4bb991

Subscribe to this mod's changes

optimizing-query-by-id is a skill published in the GitHub repository AltimateAI/data-engineering-skills (122 stars, last pushed 1mo ago), licensed MIT. It adds 121 tokens to every session and 912 once invoked, about $0.0006 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

Other skills, from other repositories

explain-sql

Explain complex SQL queries in plain English with Mermaid data flow diagrams, performance annotations, and anti-pattern detection. Use when the user pastes a SQL query and asks "what does this do?", "explain this query", or needs to understand inherited SQL, CTEs, window functions, recursive queries, or dbt model…

adityawrk/analytics-with-claude-code · 72 tokens

sql-optimizer

Analyze and optimize slow SQL queries. Use when the user says a query is slow, asks to optimize or speed up SQL, wants to find anti-patterns, needs index recommendations, or asks for a query rewrite. Also use when EXPLAIN output shows full table scans or poor join strategies.

adityawrk/analytics-with-claude-code · 63 tokens

backend-patterns

Backend architecture patterns, API design, database optimization, and server-side best practices for Node.js, Express, and Next.js API routes.

Methasit-Pun/data_engineer_claude_skills · 31 tokens

dbt-patterns

The most important design decision in a dbt project is how to organize model layers. A clear layer structure means every model has exactly one place it belongs, and anyone reading the project can understand what each model does.

Methasit-Pun/data_engineer_claude_skills · 143 tokens

data-migration

Moving data between systems safely — cutover planning, backfill strategies, dual-write patterns, validation, rollback procedures, and zero-downtime migration techniques. Use this skill whenever the team is migrating from one database or warehouse to another (MySQL → Snowflake, Redshift → BigQuery, on-prem → cloud)…

Methasit-Pun/data_engineer_claude_skills · 160 tokens

schema-design

Data modeling for analytical workloads — star schema, snowflake schema, one big table (OBT), slowly changing dimensions (SCD), normalization tradeoffs, grain definition, and surrogate key strategies. Use this skill whenever the user is designing or reviewing a data warehouse schema, planning a fact/dimension table…

Methasit-Pun/data_engineer_claude_skills · 143 tokens