optimizing-query-text
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "optimizing-query-text":https://skillpi.com/zh/skills/deepskill-61dbd79cd795fb33a14a93b489e47eb4b9da6f1f8a0ea2911f1a4a07e762627b。安装前核对上游来源和版本:https://github.com/AltimateAI/data-engineering-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
对提供的 Snowflake SQL 查询文本进行性能优化与调优。适用于用户粘贴或提交 SQL 并请求使其更快或提高性能、指出性能反模式(如在过滤列上使用函数、隐式连接等)、以及用户询问查询为何变慢或如何加速时的场景。会审查 SQL 并提出改写与优化建议。
介绍来源: third_party · AltimateAI · 0.0.1 参考来源
SkillPi 解读
- 能完成的任务
- 翻译待完成,请参阅来源文档。
- 输入和输出
- 翻译待完成,请参阅来源文档。
- 软件、账号及付费要求
- 翻译待完成,请参阅来源文档。
- 实测状态
- 未测试
文件浏览
Frontmatter
name: optimizing-query-text
description: |
Optimizes Snowflake SQL query performance from provided query text. Use when optimizing Snowflake SQL for:
(1) User provides or pastes a SQL query and asks to optimize, tune, or improve it
(2) Task mentions "slow query", "make faster", "improve performance", "optimize SQL", or "query tuning"
(3) Reviewing SQL for performance anti-patterns (function on filter column, implicit joins, etc.)
(4) User asks why a query is slow or how to speed it upOptimize 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 BYexactly as written - Same limits: If original has
LIMIT N, keepLIMIT 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 |
Pattern 2: Function on JOIN Column
Problem: Functions on JOIN columns prevent hash joins, forcing slower nested loop joins.
CAN Fix
| Original | Optimized | Why Safe |
|---|---|---|
ON CAST(a.id AS VARCHAR) = CAST(b.id AS VARCHAR) |
ON a.id = b.id |
If both are same type (e.g., INTEGER) |
ON UPPER(a.code) = UPPER(b.code) |
ON a.code = b.code |
If data is already consistently cased |
ON TRIM(a.name) = TRIM(b.name) |
ON a.name = b.name |
If data has no leading/trailing spaces |
CANNOT Fix
| Pattern | Why Not |
|---|---|
ON CAST(a.id AS VARCHAR) = b.string_id |
Types genuinely differ, CAST required |
ON DATE(a.timestamp) = b.date_col |
Different granularity, DATE() required |
ON UPPER(a.code) = b.code |
If b.code might have different case |
ON a.id = b.id + 1 |
Arithmetic transformation, cannot remove |
Pattern 3: NOT IN Subquery
Problem: NOT IN has poor performance and unexpected NULL behavior.
CAN Fix
| Original | Optimized | Why Safe |
|---|---|---|
WHERE id NOT IN (SELECT id FROM t WHERE ...) |
WHERE NOT EXISTS (SELECT 1 FROM t WHERE t.id = main.id AND ...) |
Equivalent when subquery column is NOT NULL |
WHERE id NOT IN (SELECT id FROM t) where id has NOT NULL constraint |
WHERE NOT EXISTS (SELECT 1 FROM t WHERE t.id = main.id) |
NOT NULL guarantees equivalence |
CANNOT Fix
| Pattern | Why Not |
|---|---|
WHERE id NOT IN (SELECT nullable_col FROM t) |
If subquery returns NULL, NOT IN returns no rows; NOT EXISTS doesn't |
WHERE (a, b) NOT IN (SELECT x, y FROM t) |
Multi-column NOT IN has complex NULL semantics |
Key Rule: Only convert NOT IN to NOT EXISTS if you can verify the subquery column cannot be NULL.
Pattern 4: Repeated Subquery
Problem: Same subquery executed multiple times causes redundant scans.
CAN Fix
| Original | Optimized |
|---|---|
| Subquery appears 2+ times identically | Extract to CTE, reference CTE multiple times |
| Same aggregation used in multiple places | Compute once in CTE |
CANNOT Fix
| Pattern | Why Not |
|---|---|
| Correlated subquery (references outer table) | Each execution is different, cannot cache |
| Subqueries with different filters | Not actually the same subquery |
| Subquery in SELECT that depends on current row | Correlation prevents extraction |
Pattern 5: Implicit Comma Joins
Problem: Comma-separated tables in FROM clause are harder to read and optimize.
CAN Fix - Always
Convert FROM a, b, c WHERE a.id = b.id AND b.id = c.id to explicit JOIN syntax.
This is always safe - just restructuring, no semantic change.
UNSAFE Optimizations (NEVER apply)
- UNION to UNION ALL: UNION deduplicates rows, UNION ALL does not - different results
- Changing window functions: Do not modify
SUM(SUM(x)) OVER(...)or similar nested aggregates - Adding redundant filters: Do not add filters in JOIN ON if same filter exists in WHERE
- Changing column names: Copy column names EXACTLY from original - do not "simplify" or rename
- Changing column aliases: Keep all aliases exactly as original
- Adding early filtering in JOINs: If a filter is in WHERE, do not duplicate it in JOIN ON clause
Principles
- Minimal changes: Make the fewest changes necessary. Simpler optimizations are more reliable.
- Preserve structure: Keep subqueries, CTEs, and overall query structure unless there's a clear benefit.
- When in doubt, don't: If unsure whether a change preserves semantics, skip it.
- Copy exactly: Column names, table aliases, and expressions should be copied character-for-character.
Priority Order
- Date/time functions on filter columns - Highest impact
- Implicit joins to explicit JOIN - Always safe, improves readability
- NOT IN to NOT EXISTS - Only if NULL-safe
Requirements
- Results must be identical: Same rows, same columns, same order
- Valid Snowflake SQL: Output must execute without errors in Snowflake
当前收录版本
0.0.1
安全与评测信息
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 面向 Snowflake SQL 性能优化这一高频刚需场景
- 直接解决数据工程师日常核心痛点(慢查询调优)
- 提供具体可落地的优化模式和反模式指南
亮点
- 触发条件覆盖全面,包含用户主动请求、关键词匹配、反模式审查等多种场景
- 边界清晰,不易与其他 Skill 重叠
待提升项
-
description重复定义 原报告严重程度:轻微
顶层frontmatter和skill_body内部均包含description字段,内容重复
改进建议:建议移除skill_body内部的description,仅保留顶层frontmatter中的定义
亮点
- 结构清晰,按Pattern分类组织,每类含CAN/CANNOT Fix对比表格
- 包含完整的UNSAFE优化清单和核心原则说明
- 提供大量具体SQL转换示例,便于理解执行
亮点
- 严格遵循语义保持规则,无法保证结果一致时返回原查询
- 详细列出各类优化模式的适用条件和限制
- 强调最小化变更原则,降低执行风险
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
翻译待完成,请参阅来源文档。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。