Skip to content

Fix various places in PerfStatsAnalysis.sql - minor issues#489

Open
PiJoCoder wants to merge 3 commits intomasterfrom
FixPerfStatsIssues_pijocoder_032726
Open

Fix various places in PerfStatsAnalysis.sql - minor issues#489
PiJoCoder wants to merge 3 commits intomasterfrom
FixPerfStatsIssues_pijocoder_032726

Conversation

@PiJoCoder
Copy link
Copy Markdown
Collaborator

@PiJoCoder PiJoCoder commented Mar 27, 2026

This PR applies a set of small correctness and robustness fixes to PerfStatsAnalysis.sql, primarily around wait stats calculations and a few stored-procedure logic issues.

Changes:

  • Harden wait stats outputs (e.g., clamp negative wait deltas; exclude zero/negative waits from “top” category selection; use BIGINT for available CPU seconds).
  • Fix query logic issues (e.g., correct boolean grouping in blocking-chain time filtering; fix cursor loop condition).
  • Prevent runtime errors / logic inversions (e.g., NULLIF to avoid divide-by-zero; correct COL_LENGTH null check; remove stray debug SELECT).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR applies a set of small correctness and robustness fixes to PerfStatsAnalysis.sql, primarily around wait stats calculations and a few stored-procedure logic issues.

Changes:

  • Harden wait stats outputs (e.g., clamp negative wait deltas; exclude zero/negative waits from “top” category selection; use BIGINT for available CPU seconds).
  • Fix query logic issues (e.g., correct boolean grouping in blocking-chain time filtering; fix cursor loop condition).
  • Prevent runtime errors / logic inversions (e.g., NULLIF to avoid divide-by-zero; correct COL_LENGTH null check; remove stray debug SELECT).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sqlnexus/PerfStatsAnalysis.sql
Comment thread sqlnexus/PerfStatsAnalysis.sql
@PiJoCoder
Copy link
Copy Markdown
Collaborator Author

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown

Copilot AI commented Apr 25, 2026

@copilot apply changes based on the comments in this thread

Applied in commit 9e57bde — updated the usp_SmallSampledStats description text to use NULLIF([rows], 0) so it matches the safe predicate and users following the guidance won't hit a divide-by-zero.

@PiJoCoder
Copy link
Copy Markdown
Collaborator Author

@copilot: can you summarize and update the description in the PR of all the changes/commits in this PR

Copy link
Copy Markdown

Copilot AI commented Apr 25, 2026

@copilot: can you summarize and update the description in the PR of all the changes/commits in this PR

Updated the PR description with a full summary of all changes across the three commits. Here's the breakdown:

Wait stats hardening

  • Clamp negative wait-time deltas to 0 before computing wait_time_ms_per_sec
  • Use NULLIF(DATEDIFF(...)+1, 0) as divisor to avoid divide-by-zero on invalid intervals
  • Change @avail_cpu_time_sec from INTBIGINT (both wait stats procs)
  • Exclude zero/negative wait entries and SOS_SCHEDULER_YIELD from the TOP 5 subquery
  • Add missing DROP PROC guard before CREATE PROC DataSet_WaitStats_WaitStatsTopCategoriesOther

Query logic fixes

  • Fix boolean precedence in blocking-chain time filter (AND (...OR...))
  • Fix cursor WHILE condition from <> -1 to = 0

Runtime error / logic-inversion fixes

  • usp_SmallSampledStats: use NULLIF([rows], 0) in both the predicate and the recommendation text
  • usp_BatchSort: fix inverted COL_LENGTH null check (IS NULLIS NOT NULL)
  • usp_AutoUpdateStats: remove stray debug SELECT * that leaked a result set

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -4855,7 +4873,7 @@ DECLARE @FileName NVARCHAR(MAX),
@SqlStmt VARCHAR(MAX);

--if xml_plan column is in the table, the the file name (only) of the file that contains the query plan with optimized batch sort
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

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

Typo in comment: duplicated word "the" ("the the").

Suggested change
--if xml_plan column is in the table, the the file name (only) of the file that contains the query plan with optimized batch sort
--if xml_plan column is in the table, the file name (only) of the file that contains the query plan with optimized batch sort

Copilot uses AI. Check for mistakes.
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.

Examine PerfStatsAnalysis.sql for any hard-to-catch bugs/issues using GitHub Copilot

3 participants