Skip to content

Conversation

@DavidWiseman
Copy link
Collaborator

Replace OUTER APPLY with JOINs. This can improve performance when there are a large number of databases. Improves the performance of updating summary.

Copilot AI review requested due to automatic review settings December 6, 2025 12:24

This comment was marked as outdated.

Replace OUTER APPLY with JOINs.  This can improve performance when there are a large number of databases.  Improves the performance of updating summary.
@DavidWiseman DavidWiseman force-pushed the FileStatus-performance-fix branch from 4f333a8 to 03d2367 Compare December 6, 2025 12:31
@DavidWiseman DavidWiseman requested a review from Copilot December 6, 2025 12:32
Copy link
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 5 comments.


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

LEFT OUTER JOIN dbo.DBFileThresholds T_FG ON T_FG.InstanceID = F.InstanceID
AND T_FG.DatabaseID = F.DatabaseID
AND T_FG.data_space_id = F.data_space_id
AND T_FG.data_space_id <> 0 /* Exclude log as this is considered a DB threshold */
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

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

The exclusion condition <> 0 is inconsistent with the DB level join which uses data_space_id = 0 for logs. Consider using NOT IN (0, -1) or > 0 to align with the pattern used in the original OUTER APPLY logic which checked data_space_id NOT IN(0,-1) for filegroups.

Suggested change
AND T_FG.data_space_id <> 0 /* Exclude log as this is considered a DB threshold */
AND T_FG.data_space_id NOT IN (0, -1) /* Exclude log and DB-level thresholds as these are considered DB thresholds */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's correct

OR
(T_DB.data_space_id=0 AND F.type=1) /* Log Threshold */
)
AND T_FG.InstanceID IS NULL /* Only JOIN if we don't have a Filegroup level threshold (so COALESCE works with NULL thresholds) */
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

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

The exclusion check uses T_FG.InstanceID IS NULL but should check if a filegroup threshold exists for this specific file. If T_FG joins but has NULL threshold values, this condition would still be NULL and the DB threshold would be excluded. Consider using T_FG.data_space_id IS NULL or T_FG.FreeSpaceWarningThreshold IS NULL AND T_FG.FreeSpaceCriticalThreshold IS NULL to more accurately detect when a filegroup threshold is not configured.

Suggested change
AND T_FG.InstanceID IS NULL /* Only JOIN if we don't have a Filegroup level threshold (so COALESCE works with NULL thresholds) */
AND (T_FG.data_space_id IS NULL OR (T_FG.FreeSpaceWarningThreshold IS NULL AND T_FG.FreeSpaceCriticalThreshold IS NULL)) /* Only JOIN if we don't have a Filegroup level threshold configured (so COALESCE works with NULL thresholds) */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's configured as NULL, we don't want to go to the next level up the tree.

OR
(T_Inst.data_space_id=0 AND F.type=1) /* Log Threshold */
)
AND T_DB.InstanceID IS NULL /* Only JOIN if we don't have a DB level threshold (so COALESCE works with NULL thresholds) */
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

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

Similar to the T_FG check, this condition may incorrectly exclude instance thresholds when a DB threshold row exists but has NULL values. Consider using T_DB.DatabaseID IS NULL or checking for NULL threshold values to accurately determine when a DB-level threshold is not configured.

Suggested change
AND T_DB.InstanceID IS NULL /* Only JOIN if we don't have a DB level threshold (so COALESCE works with NULL thresholds) */
AND (
T_DB.DatabaseID IS NULL
OR (
T_DB.WarningThreshold IS NULL
AND T_DB.CriticalThreshold IS NULL
)
) /* Only JOIN if we don't have a DB level threshold or DB-level threshold values are NULL (so COALESCE works with NULL thresholds) */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's configured as NULL, we don't want to go to the next level up the tree.

OR
(T_Root.data_space_id=0 AND F.type=1) /* Log Threshold */
)
AND T_Inst.InstanceID IS NULL /* Only JOIN if we don't have an instance threshold (so COALESCE works with NULL thresholds) */
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

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

This condition may fail to properly detect when an instance threshold is configured. Since T_Inst joins on InstanceID = F.InstanceID, checking T_Inst.InstanceID IS NULL works, but for consistency with the hierarchical intent, consider checking a more specific column like threshold values or using the pattern established by the other levels.

Suggested change
AND T_Inst.InstanceID IS NULL /* Only JOIN if we don't have an instance threshold (so COALESCE works with NULL thresholds) */
AND T_Inst.FreeSpaceWarningThreshold IS NULL /* Only JOIN if we don't have an instance threshold value (so COALESCE works with NULL thresholds) */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's configured as NULL, we don't want to go to the next level up the tree.

LEFT JOIN dbo.CollectionDatesThresholds CDT_Inst ON CDT_Inst.InstanceID = F.InstanceID AND CDT_Inst.Reference = 'DBFiles'
-- Join for Collection Date Thresholds (Global)
LEFT JOIN dbo.CollectionDatesThresholds CDT_Root ON CDT_Root.InstanceID = -1 AND CDT_Root.Reference = 'DBFiles'
AND CDT_Inst.InstanceID IS NULL /* Only JOIN if we don't have an instance level threshold. (so COALESCE works with NULL thresholds) */
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

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

This exclusion pattern has the same issue as the DBFileThresholds joins. If CDT_Inst joins but has NULL WarningThreshold and CriticalThreshold values, the Root threshold would be incorrectly excluded. Consider checking CDT_Inst.WarningThreshold IS NULL AND CDT_Inst.CriticalThreshold IS NULL instead.

Suggested change
AND CDT_Inst.InstanceID IS NULL /* Only JOIN if we don't have an instance level threshold. (so COALESCE works with NULL thresholds) */
AND (CDT_Inst.InstanceID IS NULL OR (CDT_Inst.WarningThreshold IS NULL AND CDT_Inst.CriticalThreshold IS NULL)) /* Only JOIN if we don't have an instance level threshold, or if instance threshold values are NULL. (so COALESCE works with NULL thresholds) */

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's configured as NULL, we don't want to go to the next level up the tree.

@DavidWiseman DavidWiseman merged commit dfcf6f7 into trimble-oss:main Dec 6, 2025
@DavidWiseman DavidWiseman deleted the FileStatus-performance-fix branch December 6, 2025 13:00
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.

1 participant