Re: getdate() in UDF
From: Erland Sommarskog (sommar_at_algonet.se)
Date: 03/25/04
- Next message: Chumley the Walrus: "sql connection error saying "Provider" is not a valid keyword ???"
- Previous message: Delbert Glass: "Re: getdate() in UDF"
- In reply to: Kris Kirk: "Re: getdate() in UDF"
- Next in thread: Delbert Glass: "Re: getdate() in UDF"
- Reply: Delbert Glass: "Re: getdate() in UDF"
- Reply: Kris Kirk: "Re: getdate() in UDF"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 25 Mar 2004 00:01:31 +0000 (UTC)
Kris Kirk (kriskirk@hotmail.com) writes:
> But the same problem would exist if getdate() was replaced by an input
> parameter, wouldn't it? How is getdate() any more 'unknown' than an
> input parameter which can change each time the function is called?
The point is that a deterministic function will always give the same result
for the same input, that is data in the database and input parameters.
> Which begs the question why bother insisting on something being
> deterministic in the first place? What advantage does it give knowing
> that the output will be the same for a specific database state and input
> parameter(s), when these two things are unknown until runtime anyway?
Because you are materializing derived data.
I another posting tonight I had this table:
create table Steve (empid int NOT NULL,
startdate datetime NOT NULL,
noofyears as datediff(year, startdate, getdate()),
constraint pk_steve primary key (empid))
Here noofyears is a computed column which tells us how long a person
has been employed in the company. So far so good. Say now that for some
reason, we find many searches on this column, and find it important that
they are fast, so we want to put an index on the column
create index empid_ix on Steve (noofyears)
But this gives us:
Server: Msg 1933, Level 16, State 1, Line 1
Cannot create index because the key column 'noofyears' is non-
deterministic or imprecise.
As long as noofyears is just a plain computed column, it does not
take up any space. It is just a forumula. But to create an index on it,
SQL Server must materialize the values. It is not a problem if the
underlying data changes, because SQL Server can update the index
in such case. But the definition of noofyears is such that even if
nothing happens in the database, at regular points in time, SQL Server
would need to change the index because the anniversary of a start
date is passed.
-- Erland Sommarskog, SQL Server MVP, sommar@algonet.se Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
- Next message: Chumley the Walrus: "sql connection error saying "Provider" is not a valid keyword ???"
- Previous message: Delbert Glass: "Re: getdate() in UDF"
- In reply to: Kris Kirk: "Re: getdate() in UDF"
- Next in thread: Delbert Glass: "Re: getdate() in UDF"
- Reply: Delbert Glass: "Re: getdate() in UDF"
- Reply: Kris Kirk: "Re: getdate() in UDF"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|