optimizing-query-by-id

Optimize Snowflake query performance using historical query IDs. Used when a user provides a Snowflake query ID (UUID) for analysis or optimization, for tasks mentioning “slow query”, “optimize”, “query history”, or “query analysis”, and to examine metrics like scanned bytes, spill behavior, and partition pruning. Produces query analysis findings, recommended optimized SQL, and expected improvement estimates.

Last repository push
GitHub Stars
137
Forks
13
Issues / PRs
5

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "optimizing-query-by-id" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-14e9097f6ae11b1e7a626915cdecae8c08aed50e18df41ce90d4723bb0215f3e. Verify the upstream source and version against https://github.com/AltimateAI/data-engineering-skills before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Optimize Snowflake query performance using historical query IDs. Used when a user provides a Snowflake query ID (UUID) for analysis or optimization, for tasks mentioning “slow query”, “optimize”, “query history”, or “query analysis”, and to examine metrics like scanned bytes, spill behavior, and partition pruning. Produces query analysis findings, recommended optimized SQL, and expected improvement estimates.

0 Downloads

Introduction source: third_party · AltimateAI · 0.0.1 Reference

SkillPi interpretation

Tasks
Fetch query → Get profile → Apply best practices → Verify improvement → Return optimized query**
Inputs and outputs
Original Query Metrics:** Execution time: 45 seconds Data scanned: 12.3 GB Partitions: 500/500 (no pruning) Spillage: 2.1 GB Issues Found:** 1. No partition pruning - filtering on non-cluster column 2. SELECT * scanning unnecessary columns 3. Large table joined without pre-filtering Optimized Query:** Changes:** Added date range filter matching cluster key Replaced SELECT * with specific columns Pre-filtered in CTE before join Expected Improvement:** Partitions: 500 → ~15 (97% reduction) Data scanned: 12.3 GB → ~0.4 GB Estimated time: 45s → ~3s
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: optimizing-query-by-id
description: |
  Optimizes Snowflake query performance using query ID from history. Use when optimizing Snowflake queries for:
  (1) User provides a Snowflake query_id (UUID format) to analyze or optimize
  (2) Task mentions "slow query", "optimize", "query history", or "query profile" with a query ID
  (3) Analyzing query performance metrics - bytes scanned, spillage, partition pruning
  (4) User references a previously run query that needs optimization
  Fetches query profile, identifies bottlenecks, returns optimized SQL with expected improvements.

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

Example Output

Original Query Metrics:

  • Execution time: 45 seconds
  • Data scanned: 12.3 GB
  • Partitions: 500/500 (no pruning)
  • Spillage: 2.1 GB

Issues Found:

  1. No partition pruning - filtering on non-cluster column
  2. SELECT * scanning unnecessary columns
  3. Large table joined without pre-filtering

Optimized Query:

WITH filtered_events AS (
    SELECT event_id, user_id, event_type, created_at
    FROM events
    WHERE created_at >= '2024-01-01'
      AND created_at < '2024-02-01'
      AND event_type = 'purchase'
)
SELECT fe.event_id, fe.created_at, u.name
FROM filtered_events fe
JOIN users u ON fe.user_id = u.id;

Changes:

  • Added date range filter matching cluster key
  • Replaced SELECT * with specific columns
  • Pre-filtered in CTE before join

Expected Improvement:

  • Partitions: 500 → ~15 (97% reduction)
  • Data scanned: 12.3 GB → ~0.4 GB
  • Estimated time: 45s → ~3s

Current listing version

0.0.1

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value85/100

Highlights

  • 针对 Snowflake 查询性能优化的明确技术场景,具有实际工程价值
  • 功能闭环完整,从获取查询ID到返回优化建议,解决具体痛点
Trigger Precision90/100

Highlights

  • 触发场景描述详细,覆盖用户使用 Snowflake 查询ID进行优化的典型情况,边界清晰
Structure and Standards95/100

Highlights

  • 文档结构清晰,包含完整的工作流、SQL示例、问题识别表格、优化前后对比示例
  • frontmatter 完整,符合规范
Execution Reliability75/100

Areas to improve

  • 异常处理不完善 Reported severity: Minor

    未显式处理 query_id 不存在、权限不足等异常情况,也未对不可逆操作做确认提示

    Recommendation: 建议增加异常处理说明和对生产环境执行优化查询的确认机制

Highlights

  • 提供了完整的 SQL 查询语句和 EXPLAIN 计划验证环节,优化建议具体可行
Evidence Verifiability (mean of the first four)86/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.