Skip to content

Feature:支持 SQL Server 执行计划#3382

Merged
t8y2 merged 2 commits into
t8y2:mainfrom
zipg:codex/sqlserver-explain-plan
Jul 16, 2026
Merged

Feature:支持 SQL Server 执行计划#3382
t8y2 merged 2 commits into
t8y2:mainfrom
zipg:codex/sqlserver-explain-plan

Conversation

@zipg

@zipg zipg commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

改动说明

  • 为 SQL Server 开启执行计划入口,使用 SHOWPLAN_XML 获取估算计划,不实际执行源 SQL。
  • 在独立会话中严格按 ON -> 生成计划 -> finally OFF 顺序执行;ON 失败时不会发送源 SQL,异常或取消后会关闭会话。
  • 解析 Showplan XML,支持树形、摘要和原始 XML 三种视图,展示物理算子、表、索引、估算行数、成本和表达式。
  • 补充 SQL Server XML 类型转换、只读连接兼容和 GO 多批次拦截。

评审意见处理

  • ShowPlan XML 缺失、格式错误、内容截断、非 ShowPlan XML 或不存在 RelOp 时改为明确错误,不再显示空白计划。
  • 新增显式 executionMode: simple,SQL Server explain 的 ON、源 SQL 和 OFF 都直接走 TDS simple_query
  • simple 模式绕过 sys.dm_exec_describe_first_result_set 结果类型探测和 SQL 重写。
  • 已同步最新 main 并解决原 merge conflict。

真实环境验证

已在 SQL Server 2022 Developer Edition 16.0.4225.2(Linux)真实实例验证:

  • JOIN、CTE、GROUP BY 和索引查询能返回包含 Sort、Stream Aggregate、Nested Loops、Clustered Index Scan、Index Seek 的完整计划。
  • 正常 ShowPlan 返回 XML 和 RelOp,执行 OFF 后同一物理连接继续执行普通查询成功。
  • 无效对象编译失败后,同一物理连接执行 OFF,随后普通查询成功。
  • 使用无 SHOWPLAN 权限的临时账号验证权限失败,清理后普通查询成功。
  • 在 ShowPlan 查询进行中取消,随后同一物理连接执行 OFF 和普通查询均成功。
  • 只读连接可以生成计划。
  • 源 SQL 包含独立 GO 批次时会拒绝,字符串中的 GO 不受影响。
  • 临时登录、数据库用户和测试表均已删除。

测试

  • pnpm check
  • SQL Server 前端执行计划与状态流转测试:10 项通过
  • cargo test -p dbx-core --no-default-features sqlserver --lib:141 项通过
  • 执行模式反序列化测试通过
  • cargo check -p dbx --no-default-features
  • cargo fmt --all -- --check

@t8y2 t8y2 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request changes: SQL Server 执行计划链路还有几个可靠性问题,且当前分支存在 merge conflict。

  1. apps/desktop/src/lib/diagram/explainPlan.ts:386 在 XML 缺失、截断或不是有效 ShowPlan XML 时返回空 nodes,调用方随后会把它当成成功结果,用户只能看到空白计划。请让解析失败成为明确错误,并覆盖 malformed、truncated、非 ShowPlan XML 和零 RelOp 的测试。

  2. crates/dbx-core/src/db/sqlserver.rs:1722 在启用 SHOWPLAN_XML 后仍走普通查询路径,其中会调用 sys.dm_exec_describe_first_result_set。该探测 SQL 同样受当前 session 的 SHOWPLAN 状态影响。建议为 explain 增加显式执行模式,直接走 simple query,绕过结果类型探测和 SQL 重写。

这条链路依赖同一物理连接、TDS XML 类型以及 SHOWPLAN 的 session 清理,当前 mock 测试不足以证明可用。请至少补一次真实 SQL Server 集成验证,包括权限失败、无效对象、取消和 OFF 清理后的普通查询。

@zipg
zipg force-pushed the codex/sqlserver-explain-plan branch from 10a7fd2 to 526b967 Compare July 15, 2026 14:12
@zipg

zipg commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

已按评审意见更新:

  1. ShowPlan XML 缺失、malformed、truncated、非 ShowPlan XML、零 RelOp 均改为明确错误,并补齐对应测试。
  2. 新增显式 executionMode: simple。SQL Server explain 的 ON / 源 SQL / OFF 直接走 TDS simple_query,不会再调用 sys.dm_exec_describe_first_result_set 或进行 SQL 重写。
  3. 已在 SQL Server 2022 真实实例验证正常计划、权限失败、无效对象和查询中取消;后三类场景清理后均能在同一物理连接执行普通查询。
  4. 已 rebase 最新 main 并解决 merge conflict。

本地 pnpm check、SQL Server Rust 定向测试(141 项)、执行模式传输测试、桌面端无 DuckDB 编译检查均通过。最新提交:53af2dd60

新一轮 PR CI 已全部通过。

@zipg
zipg force-pushed the codex/sqlserver-explain-plan branch from 526b967 to 53af2dd Compare July 15, 2026 14:26
@zipg
zipg requested a review from t8y2 July 15, 2026 14:34
@zipg
zipg force-pushed the codex/sqlserver-explain-plan branch from 53af2dd to d955ae5 Compare July 16, 2026 07:47
@t8y2
t8y2 merged commit e4c56e6 into t8y2:main Jul 16, 2026
7 checks passed
@t8y2

t8y2 commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Thanks for the contribution! Merged in e4c56e6, will be released in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants