Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SYSDATETIME() cost more than GETDATE()?

Is there any reason why I should stop using SYSDATETIME() everytime, instead of GETDATE() ?

Don't they both ask the cpu what time it is or does sysdatetime need more instructions to calculate the fractions? Does Getdate work on rounding it? Can sysdatetime be faster because it's not working on rounding?

I obviously wouldn't use sysdatetime if I'm not storing the nanoseconds, but I'm asking about the costs other than the storage size. (The current app I'm developing runs sysdatetime() at least 280 times a second)

like image 245
Uğur Gümüşhan Avatar asked Feb 13 '13 15:02

Uğur Gümüşhan


People also ask

What is the difference between Getdate () and Sysdatetime ()?

The main difference between GETDATE() and SYSDATETIME() is that GETDATE returns the current date and time as DATETIME but SYSDATETIME returns a DATETIME2 value, which is more precise.

What is the difference between Getdate and Getutcdate?

The difference between GETDATE() and GETUTCDATE() is in time zone. The GETDATE() function return current date and time in the local time zone, the time zone where your database server is running, but GETUTCDATE() return current time and date in UTC (Universal Time Coordinate) or GMT time zone.

What is the difference between CURRENT_TIMESTAMP and Getdate?

CURRENT_TIMESTAMP is an ANSI SQL function whereas GETDATE is the T-SQL version of that same function. One interesting thing to note however, is that CURRENT_TIMESTAMP is converted to GETDATE() when creating the object within SSMS. Both functions retrieve their value from the operating system in the same way.

What is Sysdatetime in SQL?

The SYSDATETIME() function returns the date and time of the computer where the SQL Server is running.


2 Answers

SELECT SYSDATETIME();
GO
DECLARE @d DATETIME2(7) = SYSDATETIME();
GO 10000
SELECT SYSDATETIME();
GO
DECLARE @d DATETIME = SYSDATETIME();
GO 10000
SELECT SYSDATETIME();
GO
DECLARE @d DATETIME2(7) = GETDATE();
GO 10000
SELECT SYSDATETIME();
GO
DECLARE @d DATETIME = GETDATE();
GO 10000
SELECT SYSDATETIME();

Results:

  • Assigning SYSDATETIME to DATETIME2(7) : 3.4 s
  • Assigning SYSDATETIME to DATETIME : 3.3 s
  • Assigning GETDATE to DATETIME2(7) : 3.4 s
  • Assigning GETDATE to DATETIME : 3.3 s

So it appears to not matter. What matters is what type of variable you assign it to, and even that is not by much. 10000/0.1 seconds means the delta is very, very small and not enough to worry about. I would rather be consistent in this case.

like image 100
Aaron Bertrand Avatar answered Oct 22 '22 00:10

Aaron Bertrand


In a few extermely poorly designed tests, on my machine it appears that SYSDATETIME() may be faster:

select sysdatetime()
go
declare @Dt datetime
select @dt = sysdatetime()
select @dt = DATEADD(day,1,@dt)
go 15000
select sysdatetime()
go
declare @Dt datetime
select @dt = GETDATE()
select @dt = DATEADD(day,1,@dt)
go 15000
select sysdatetime()
go

On my machine, this is tending to produce a gap of ~1 second between the first two result sets and ~2 seconds between the 2nd and 3rd. Reversing the order of the two tests reverses the gaps.

like image 45
Damien_The_Unbeliever Avatar answered Oct 22 '22 00:10

Damien_The_Unbeliever