views:

550

answers:

1

Related to this question, I decided to check the UDFs in my data warehouse (which should largely have been deterministic), and I found several which aren't which should be.

For instance:

CREATE FUNCTION [udf_YearFromDataDtID]
(
    @DATA_DT_ID int
)
RETURNS int
AS
BEGIN
    RETURN @DATA_DT_ID / 10000
END

Shows up in this query:

SELECT  ROUTINE_NAME
FROM    INFORMATION_SCHEMA.ROUTINES
WHERE   IS_DETERMINISTIC = 'NO'
        AND ROUTINE_TYPE = 'FUNCTION'
ORDER BY ROUTINE_NAME

Why is this?

+4  A: 

Yikes - apparently, it REQUIRES SCHEMABINDING to be specified other wise it could cause performance problems

ALTER FUNCTION [udf_YearFromDataDtID]
(
    @DATA_DT_ID int
)
RETURNS int
WITH SCHEMABINDING
AS
BEGIN
    RETURN @DATA_DT_ID / 10000
END

Looks like I could be getting some performance boosts by fixing these. Nice.

Cade Roux
Using UDFs in queries incurs an enormous performance hit no matter what you do. If you went in to all of your queries and replaced all instances of UDF calls with the sql in the functions, you could see several order of magnititude difference. It makes for repetetive SQL, but it can really help.
Eric Z Beard
These particular UDFs are constant, but end up on every row - I could evaluate them into a temporary, but I'm expecting them to be refactored out eventually as the output schema needs to come into line with the general system refactoring on the data warehouse input.
Cade Roux