Is GetDate() deterministic

2019-07-23 11:18发布

问题:

This is a sort of philosophical question. In an interview I was present at GetDate was given as an example of a non deterministic function. I can see why that argument holds water; but it seems a specious argument to me.

To elaborate: For a given instance in time (within a 100 microsecond band) getdate will return a specific value.

For two computers running to the same clock time (synchronised by a sufficiently accurate clock) they will both return the same value for getdate.

So that is deterministic.

It can be argued that getdate returns different values at different times and so it can not be described as deterministic.

But a sql query "get x from y where primary key equals z" will return the same value for x where z is the same value. So if the clock is fixed to a certain value then we shall always receive the same value for getdate.

In other words the value of getdate is determined by an external parameter, in exactly the same way as a SQL query that uses a where clause is controlled by that where clause parameter.

So why should we imply that getdate is non deterministic whereas any other variable parameter in a select query which provides a result is described as deterministic.

And just to extend the question; if the data changes then we receive different values to the select query, which we then explain do not affect the deterministicity (to coin a word) as the values have changed in time, just as getdate has.

To expand (as an edit) I could use XP_CmdShell to set a particular date and then immediately run GetDate(); ignoring the vagaries of the speed of the system etc. I would then always get the same answer. This effectively of negates the argument that the system date time is not an input as I have modified it via SQL and thus kept the whole process within a SQL controlled loop.

回答1:

For two computers running to the same clock time (synchronised by a sufficiently accurate clock) they will both return the same value for getdate.

So that is deterministic.

No, it's not - deterministic means that the function returns the same value given the same inputs. In this case you have no inputs, but you get different values all the time! The system clock is not an input, it is external state that the function relies upon.

Any query that relies on table data is non-deterministic because it relies on external state. Examples of deterministic functions are those that do NOT rely on external state, but rely solely on inputs to the function: FLOOR, DATEADD, etc.

if the data changes then we receive different values to the select query, which we then explain do not affect the deterministicity (to coin a word) as the values have changed in time, just as getdate has.

Actually, that proves that the query is NOT deterministic - if a change in external state changes the output of the query.



回答2:

In my experience it is deterministic. If you

SELECT GetDate() from TableX

Where TableX has 1mn rows, you would expect the same value returned for all rows, since the evaluation of GetDate does not depend on any value in any of the rows.