Skip to content

Add query --explain to show execution plan and index usage#131

Open
mkrueger wants to merge 4 commits into
mainfrom
dev/mkrueger/query-explain
Open

Add query --explain to show execution plan and index usage#131
mkrueger wants to merge 4 commits into
mainfrom
dev/mkrueger/query-explain

Conversation

@mkrueger

@mkrueger mkrueger commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Implements query "<sql>" --explain, which reports how the query engine resolves a query instead of returning documents. Closes #106.

The output shows whether the query performed a full scan or an index seek, lists the utilized and potential indexes, the index hit ratio, retrieved/output document counts, and the request charge, plus a plain-language evaluation that highlights full scans and recommends indexes to add.

Behavior

  • Cost-aware: executes only the first page (MaxItemCount = 1) to keep RU cost low. Reported metrics are an estimate based on that page. --max is ignored when --explain is supplied.
  • Human-readable by default: interactive sessions render a Spectre table with verdict lines.
  • Machine-readable when appropriate: MCP (McpPort), output redirection (StdOutRedirect), and explicit --format json emit the structured JSON plan/evaluation document, so AI tool integration is unaffected.

Changes

  • QueryCommand: new --explain option, ExecuteExplainAsync, RenderExplain, BuildExplainJson, and extracted ThrowIfRequestFailedAsync shared with the normal query path.
  • New pure helpers ParseIndexPlan and EvaluatePlan plus the PlanEvaluation record (own file).
  • Localization strings in lang/en.ftl.
  • Docs: docs/commands.md query section and README.md feature list.
  • Unit tests for EvaluatePlan (full scan / index seek / recommendations) and ParseIndexPlan (single + composite specs, null/empty/whitespace, malformed JSON).

Validation

  • dotnet build main + test projects: 0 warnings / 0 errors.
  • QueryCommandTests: 16 passing (8 new).
  • Localization key audit and help verification tests pass.

Example:
image

Implements a --explain flag on the query command that reports the query execution plan (utilized/potential indexes, index hit ratio, retrieved/output document counts, request charge) and a plain-language evaluation instead of returning documents.

- Executes only the first page (MaxItemCount = 1) to keep RU cost low; reported metrics are an estimate.
- Interactive sessions render a human-readable table with verdict lines; MCP, output redirection, and explicit --format json emit structured JSON.
- Adds PlanEvaluation record, ParseIndexPlan, and EvaluatePlan helpers with unit tests.
- Updates localization strings, docs/commands.md, and README.

Closes #106

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds an --explain mode to the existing query command so the shell can report Cosmos DB query execution/index usage (human-readable in interactive sessions, structured JSON for MCP/redirected output), along with the supporting docs, localization, and unit tests.

Changes:

  • Extend QueryCommand with --explain execution path, plan parsing/evaluation helpers, and shared request-failure handling.
  • Introduce PlanEvaluation data model and add unit tests for ParseIndexPlan / EvaluatePlan.
  • Update localization strings and user docs (README.md, docs/commands.md) to describe the new option.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
README.md Advertises query "<sql>" --explain as a supported feature.
docs/commands.md Documents --explain usage, output intent, and cost/limit behavior.
CosmosDBShell/lang/en.ftl Adds help text and user-facing strings for explain output.
CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs Implements explain execution, plan parsing/evaluation, rendering/JSON output, and refactors request error handling.
CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/PlanEvaluation.cs Adds a record type to carry explain evaluation data.
CosmosDBShell.Tests/CommandTests/QueryCommandTests.cs Adds unit coverage for explain plan parsing and evaluation behaviors.

Comment thread CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs Outdated
Copilot AI review requested due to automatic review settings June 16, 2026 12:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Comment thread CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs Outdated
Comment thread CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs Outdated
Comment thread CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs Outdated
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.

[P2] Add Query Plan / Explain functionality

2 participants