optimizing-query-text

A Snowflake SQL query optimizer that works from query text. Snowflake is a cloud database service, and query optimization means making a query run more efficiently without changing its results.

In plain words
What is it for?
Use it when a Snowflake query is slow or needs tuning, especially to review filters, date conditions, and other patterns that can reduce efficient data scanning.
Why use it?
It identifies safe performance improvements while preserving the same columns, rows, ordering, limits, and meaning. If identical results cannot be guaranteed, it leaves the query unchanged.

Skill for Claude CodeCodex

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-text
Any agent
npx skills add AltimateAI/data-engineering-skills --skill optimizing-query-text
Clone the repo
git clone --depth 1 https://github.com/AltimateAI/data-engineering-skills

Made for: Claude Code, Codex.

Per session 113 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,729 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 $0.00113 $0.01729
Opus 5 $0.00056 $0.00864
Sonnet 5 $0.00023 $0.00346
Haiku 4.5 $0.00011 $0.00173

Measured 3d ago against content hash 5007588fe49f, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

optimizing-query-text 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 3d 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-text/SKILL.md · 167 lines

How it starts

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

Optimize Query from SQL Text

OUTPUT FORMAT

Return ONLY the optimized SQL query. No markdown formatting, no explanations, no bullet points - just pure SQL that can be executed directly in Snowflake.

CRITICAL: Semantic Preservation Rules

The optimized query MUST return IDENTICAL results to the original.

Before returning ANY optimization, verify:

  • Same columns: Exact same columns in exact same order with exact same aliases
  • Same rows: Filter conditions must be semantically equivalent
  • Same ordering: Preserve ORDER BY exactly as written
  • Same limits: If original has LIMIT N, keep LIMIT N. If no LIMIT, do NOT add one.

If you cannot guarantee identical results, return the original query unchanged.


Pattern 1: Function on Filter Column

Problem: Functions on columns in WHERE clause prevent partition pruning and index usage.

CAN Fix

Original Optimized Why Safe
WHERE DATE(ts) = '2024-01-01' WHERE ts >= '2024-01-01' AND ts < '2024-01-02' Equivalent range
WHERE YEAR(dt) = 2024 WHERE dt >= '2024-01-01' AND dt < '2025-01-01' Equivalent range
WHERE MONTH(dt) = 3 AND YEAR(dt) = 2024 WHERE dt >= '2024-03-01' AND dt < '2024-04-01' Equivalent range
WHERE DATE(ts) >= '2024-01-01' AND DATE(ts) < '2024-02-01' WHERE ts >= '2024-01-01' AND ts < '2024-02-01' Same boundaries
WHERE YEAR(dt) BETWEEN 1995 AND 1996 WHERE dt >= '1995-01-01' AND dt < '1997-01-01' Equivalent range

CANNOT Fix

Pattern Why Not
WHERE YEAR(dt) IN (SELECT year FROM ...) Dynamic values, cannot precompute range
WHERE DATE(ts) = DATE(other_col) Comparing two columns, both need function
WHERE EXTRACT(DOW FROM dt) = 1 Day-of-week has no contiguous range
WHERE DATE_TRUNC('month', dt) = '2024-01-01' in GROUP BY Needed for grouping logic
SELECT YEAR(dt) AS yr ... GROUP BY YEAR(dt) Function in SELECT/GROUP BY is fine, only filter matters

Read the full file on GitHub · 167 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. 3d ago First seen · 167 lines · 113 tokens per session scan A 5007588fe49f

Subscribe to this mod's changes

optimizing-query-text is a skill published in the GitHub repository AltimateAI/data-engineering-skills (122 stars, last pushed 1mo ago), licensed MIT. It adds 113 tokens to every session and 1,729 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

aegis-dq

Agentic data quality validation across warehouses (DuckDB, BigQuery, Athena, Databricks, Postgres) with LLM diagnosis, root cause analysis, and audit trail.

aegis-dq/aegis-dq · 42 tokens

data-parity

Validate that two tables or query results are identical — or diagnose exactly how they differ. Discover schema, identify keys, profile cheaply, then diff. Use for migration validation, ETL regression, and query refactor verification.

AltimateAI/altimate-code · 48 tokens

dbt-develop

REQUIRED before writing or modifying ANY dbt model. Invoke this skill FIRST whenever a task says "create", "build", "add", "modify", "update", "fix", or "refactor" a dbt model, staging file, mart, incremental, or snapshot. Skipping this skill is the leading cause of silent-correctness bugs — models that compile and…

AltimateAI/altimate-code · 294 tokens

dbt-troubleshoot

Debug dbt errors — compilation failures, runtime database errors, test failures, wrong data, and performance issues. Use when something is broken, producing wrong results, or failing to build. Powered by altimate-dbt.

AltimateAI/altimate-code · 50 tokens