Skip to content

Converted GETUTCDATETIME() to SYSDATETIME() #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2025

Conversation

rahulreddy15
Copy link
Contributor

@rahulreddy15 rahulreddy15 commented Jul 22, 2025

This PR corrects a critical time zone issue where MSSQLTopSlowQueries and ExecutionPlanQuery were using GETUTCDATE() to filter against qs.last_execution_time, which is stored in server local time. The mismatch caused queries to return no recent data in environments where local time is ahead of UTC.

Replace GETUTCDATE() with SYSDATETIME() in both query time filters to align with the sys.dm_exec_query_stats DMV's local time storage

Integration tests running against SQL Server instances in different time zones will validate that recent slow queries and execution plans are now properly collected within the specified interval window. No additional tests are needed IMO.

@rahulreddy15 rahulreddy15 requested a review from a team as a code owner July 22, 2025 08:44
Copy link
Contributor

@sairaj18 sairaj18 left a comment

Choose a reason for hiding this comment

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

Could you update the description of the PR with details of why this change is needed?

@rahulreddy15 rahulreddy15 requested a review from sairaj18 July 25, 2025 07:40
Copy link
Contributor

@sairaj18 sairaj18 left a comment

Choose a reason for hiding this comment

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

Thanks for updating the PR description.

Integration tests running against SQL Server instances in different time zones will validate that recent slow queries and execution plans are now properly collected within the specified interval window. No additional tests are needed IMO.

If Integration tests are working now. Shouldn't they have failed earlier because we were using UTC time to retrieve recent executed queries?

nravada
nravada previously approved these changes Jul 25, 2025
Copy link

@nravada nravada left a comment

Choose a reason for hiding this comment

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

LGTm

@@ -19,7 +19,7 @@ var Queries = []models.QueryDetailsDto{
sys.dm_exec_query_stats qs
WHERE
qs.execution_count > 0
AND qs.last_execution_time >= DATEADD(SECOND, -@IntervalSeconds, GETUTCDATE())
Copy link

@abhinav1602 abhinav1602 Jul 29, 2025

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Answered this in the comment with

The absolute timezone doesn't affect the relative time calculations, and both SYSDATETIME() and last_execution_time always operate in the same timezone context

@abhinav1602
Copy link

abhinav1602 commented Jul 30, 2025

This PR corrects a critical time zone issue where MSSQLTopSlowQueries and ExecutionPlanQuery were using GETUTCDATE() to filter against qs.last_execution_time, which is stored in server local time. The mismatch caused queries to return no recent data in environments where local time is ahead of UTC.

Replace GETUTCDATE() with SYSDATETIME() in both query time filters to align with the sys.dm_exec_query_stats DMV's local time storage

Integration tests running against SQL Server instances in different time zones will validate that recent slow queries and execution plans are now properly collected within the specified interval window. No additional tests are needed IMO.

last_execution_time datetime Last time at which the plan started executing. The time is recorded in the current timezone

as per this doc, the time zone is always set to UTC so I am not sure if this fix will work for all the cases. Is it tested to be working for all the environment types?

@rahulreddy15
Copy link
Contributor Author

SQL Server (On-Host):

SYSDATETIME() returns the system datetime based on the server's local timezone
Can be configured to any timezone via Windows settings
No built-in UTC awareness in SYSDATETIME()

Azure SQL Managed Instance:

SYSDATETIME() returns datetime in UTC timezone by default
Microsoft documentation states: "Azure SQL Managed Instance uses UTC time zone"
Cannot change the system timezone like on-premises SQL Server

The sys.dm_exec_query_stats.last_execution_time column stores timestamps in the same timezone as the server.
This means local server time in on host sql server and UTC in managed instance.
Refer: https://learn.microsoft.com/en-us/azure/azure-sql/managed-instance/timezones-overview?view=azuresql

So for managed instances:
If instance is configured as UTC: Both SYSDATETIME() and last_execution_time use UTC
And for any other timezone for example JST: Both SYSDATETIME() and last_execution_time use JST
So using SYSDATETIME() is the proper solution.

The absolute timezone doesn't affect the relative time calculations, and both SYSDATETIME() and last_execution_time always operate in the same timezone context.

@rahulreddy15 rahulreddy15 requested a review from nravada August 4, 2025 08:22
Copy link

@nravada nravada left a comment

Choose a reason for hiding this comment

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

LGTM

@nravada nravada merged commit d7792bd into master Aug 4, 2025
16 checks passed
@nravada nravada deleted the NR-441414-timezone-issue-in-queries-for-QLM-metrics branch August 4, 2025 12:37
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.

4 participants