Skip to content

Conversation

@DavidWiseman
Copy link
Collaborator

The performance optimization for MAX(instance_id) is subject to plan variation. Changing the OUTER APPLY to use TOP(1) instead of MAX should allow us to get the query plan we want more reliably.

The performance optimization for MAX(instance_id) is subject to plan variation.  Changing the OUTER APPLY to use TOP(1) instead of MAX should allow us to get the query plan we want more reliably.
Copilot AI review requested due to automatic review settings December 4, 2025 15:22
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

This PR optimizes the performance of the JobHistory_Upd stored procedure by improving how the maximum instance_id is retrieved from partitioned data. The change replaces a MAX(instance_id) aggregation with a TOP(1)... ORDER BY instance_id DESC pattern to achieve more predictable query plan generation that efficiently seeks on InstanceID within each partition and performs a backward ordered scan.

Key Changes:

  • Replaced MAX(instance_id) with TOP(1) instance_id ORDER BY instance_id DESC in the OUTER APPLY subquery
  • Added inline documentation explaining the performance optimization strategy

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

@DavidWiseman DavidWiseman merged commit ac97a9b into main Dec 4, 2025
7 checks passed
@DavidWiseman DavidWiseman deleted the DavidWiseman-patch-2 branch December 4, 2025 15:46
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