debugging-snowflake-queries
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDebugging Snowflake Queries
调试 Snowflake 查询
Debug Snowflake SQL systematically: reproduce the failure, classify the problem, isolate the smallest failing unit, verify grain and joins, and only then rewrite the query.
系统地调试 Snowflake SQL:复现失败、分类问题、隔离最小失败单元、验证粒度和 JOIN,然后才重写查询。
When to Use
使用场景
- Diagnose Snowflake errors such as ,
SQL compilation error,invalid identifier,object does not exist, cast failures, permission failures, or timeoutsambiguous column - Debug queries that run but return the wrong count, duplicate rows, empty results, stale-looking data, or impossible percentages
- Investigate performance problems such as long scans, warehouse issues, or unexpectedly expensive joins
- Review an ad hoc SQL query and explain how to make it correct and reproducible
- 诊断 Snowflake 错误,例如 、
SQL compilation error、invalid identifier、object does not exist、类型转换失败、权限失败或超时ambiguous column - 调试可以运行但返回错误计数、重复行、空结果、看起来过期的数据或不合理百分比的查询
- 调查性能问题,例如长时间扫描、warehouse 问题或异常昂贵的 JOIN
- 审查临时 SQL 查询,并说明如何使其正确且可复现
Do Not Use For
不适用场景
- Template or application-layer SQL generation failures where no Snowflake SQL is actually being debugged
- General business investigations where the main task is answering the question rather than debugging the query itself
- 实际并未调试 Snowflake SQL,而是模板或应用层 SQL 生成失败
- 主要任务是回答业务问题,而非调试查询本身的通用业务调查
Workflow
工作流
- Reproduce the exact failing query or reconstruct the smallest equivalent query.
- Classify the issue before changing SQL:
- compile/name-resolution
- permissions/environment
- type/cast/null semantics
- join/grain/result correctness
- performance/scan/warehouse
- stale or incomplete upstream data
- Reduce the query to the smallest failing component:
- one CTE at a time
- one join at a time
- one computed expression at a time
- one filter at a time
- Verify object existence, schema, and exact column names before assuming anything.
- State the grain of each input and intermediate result explicitly.
- Validate counts and null rates around every join boundary.
- When a query is logically wrong but syntactically valid, prove the failure mode with a targeted counterexample or diagnostic query.
- Only after isolating the root cause, rewrite the full query.
- 复现确切的失败查询,或重建最小的等价查询。
- 修改 SQL 前先对问题进行分类:
- 编译/名称解析
- 权限/环境
- 类型/类型转换/null 语义
- JOIN/粒度/结果正确性
- 性能/扫描/warehouse
- 上游数据过期或不完整
- 将查询缩减为最小失败组件:
- 每次处理一个 CTE
- 每次处理一个 JOIN
- 每次处理一个计算表达式
- 每次处理一个过滤条件
- 在做任何假设之前,验证对象是否存在、schema 和列名是否准确。
- 明确说明每个输入和中间结果的粒度。
- 验证每个 JOIN 边界附近的计数和 null 比例。
- 当查询在语法上有效但逻辑错误时,使用有针对性的反例或诊断查询证明失败模式。
- 仅在隔离出根因后,才重写完整查询。
Core Rules
核心规则
- Prefer minimal diagnostic queries over speculative rewrites.
- Keep each debugging step falsifiable: each query should answer one question.
- Never assume two tables can be joined just because they share similarly named IDs.
- Treat row explosion as a grain mismatch until proven otherwise.
- Treat zero rows as a filter, time-window, or join-key problem before blaming missing data.
- Treat surprising percentages as denominator scope problems before blaming arithmetic.
- Use fully qualified names when working outside dbt macros or when environment ambiguity is possible.
- Check for soft-delete columns and freshness/ingestion timestamps when backend mirrors are involved.
- For large fact tables, tighten the date filter first and inspect scan behavior before running broad diagnostics.
- 优先使用最小诊断查询,避免凭猜测重写。
- 让每个调试步骤都可证伪:每个查询都应只回答一个问题。
- 绝不能仅因为两个表具有相似的 ID 名称,就假设它们可以 JOIN。
- 在证明其他原因之前,将行膨胀视为粒度不匹配。
- 在归因于数据缺失之前,先排查零行结果是否由过滤条件、时间窗口或 JOIN key 导致。
- 在归因于算术错误之前,先排查异常百分比是否由分母范围导致。
- 在 dbt macro 之外工作或可能存在环境歧义时,使用完全限定名称。
- 涉及后端镜像时,检查软删除列以及数据新鲜度/摄取时间戳。
- 对于大型事实表,先收紧日期过滤条件并检查扫描行为,再运行大范围诊断。
References
参考资料
Load these references as needed:
- Use for the end-to-end workflow, diagnostic query templates, and how to narrow a failure.
references/debugging-playbook.md - Use to map common Snowflake error messages to likely causes and next checks.
references/error-taxonomy.md - Use when the query runs but results look wrong: duplicates, drops, denominator mistakes, stale data.
references/result-correctness-checklist.md - Use for concrete warehouse-agnostic examples of common failure modes and how to correct them.
references/query-patterns-and-failure-modes.md
按需加载以下参考资料:
- 用于了解端到端工作流、诊断查询模板以及如何缩小失败范围。
references/debugging-playbook.md - 用于将常见 Snowflake 错误消息映射到可能原因和后续检查项。
references/error-taxonomy.md - 用于处理查询可以运行但结果异常的情况,例如重复、缺失、分母错误和过期数据。
references/result-correctness-checklist.md - 用于查看常见失败模式的具体示例及其修复方式,这些示例与具体数据仓库无关。
references/query-patterns-and-failure-modes.md
Expected Output
预期输出
When returning a debugging answer:
- State the likely root cause in one sentence.
- Show the minimal diagnostic evidence.
- Provide the corrected query or exact fix.
- Note any remaining assumptions, especially around grain, environment, or upstream freshness.
返回调试答案时:
- 用一句话说明可能的根因。
- 展示最小诊断证据。
- 提供修正后的查询或确切修复方案。
- 指出仍存在的假设,尤其是关于粒度、环境或上游数据新鲜度的假设。