Welcome Guest Search | Active Topics | Members | Log In | Register

performance question Options · View
Jeremy
Posted: Friday, October 19, 2007 8:29:44 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 19/10/2007 08:29:44
Date: Fri, 19 Oct 2007 15:29:44 -0700

I've got 3 functions. one that calculates a value, and two other ones that
return a value based on the value returned by the first.

I use these in a view
example:

select
dbo.calcvalue(t.[id]) as value,
dbo.calc1(calcvalue(t.[id]) )) as val1,
dbo.calc2(calcvalue(t.[id]) )) as val2
from
tabletest t

will calcvalue() get called 3 times? or would it be more efficient to use
two views.

example:

view 1:
select
dbo.calcvalue(t.[id]) as value
from
tabletest t


view 2:
select
dbo.calc1(value) as val1,
dbo.calc2(value) as val2
from
view1


techno_peasant via SQLMonster.com
Posted: Saturday, October 20, 2007 5:41:20 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 20/10/2007 17:41:20
Date: Sat, 20 Oct 2007 17:41:20 GMT

user defined functions must be deterministic - give the same output for the
same input. I think one reason this restriction is there is so that sql can
eliminate performing second and third calls to your function. That said, I
don't know if it will call it more than once. You can insure it won't by
using a derived table.

select t.id, value, dbo.calc1(value) as val1, dbo.calc2(value) as val2
from
(
select id, calcvalue(id) as value
from tabletest
) as t

Jeremy wrote:
>I've got 3 functions. one that calculates a value, and two other ones that
>return a value based on the value returned by the first.
>
>I use these in a view
>example:
>
>select
> dbo.calcvalue(t.[id]) as value,
> dbo.calc1(calcvalue(t.[id]) )) as val1,
> dbo.calc2(calcvalue(t.[id]) )) as val2
>from
> tabletest t
>
>will calcvalue() get called 3 times? or would it be more efficient to use
>two views.
>
>example:
>
>view 1:
>select
> dbo.calcvalue(t.[id]) as value
>from
> tabletest t
>
>view 2:
>select
> dbo.calc1(value) as val1,
> dbo.calc2(value) as val2
>from
> view1

--
Message posted via http://www.sqlmonster.com

Gert-Jan Strik
Posted: Sunday, October 21, 2007 7:14:35 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 21/10/2007 19:14:35
Date: Sun, 21 Oct 2007 17:14:35 +0200

Jeremy,

The rules for scalar UDFs allow for many types of optimization,
including substitution. However, a simple test will show that both SQL
Server 2000 and SQL Server 2005 will not reuse the result of the UDF for
the same row.

So if you have an expensive dbo.calcvalue, and a very cheap dbo.calc1,
then the query

select dbo.calcvalue(id), dbo.calc1(dbo.calcvalue(id)) from t

will need almost twice as long to finish when compared to

select dbo.calcvalue(id), dbo.calc1(id) from t

--
Gert-Jan


Jeremy wrote:
>
> I've got 3 functions. one that calculates a value, and two other ones that
> return a value based on the value returned by the first.
>
> I use these in a view
> example:
>
> select
> dbo.calcvalue(t.[id]) as value,
> dbo.calc1(calcvalue(t.[id]) )) as val1,
> dbo.calc2(calcvalue(t.[id]) )) as val2
> from
> tabletest t
>
> will calcvalue() get called 3 times? or would it be more efficient to use
> two views.
>
> example:
>
> view 1:
> select
> dbo.calcvalue(t.[id]) as value
> from
> tabletest t
>
> view 2:
> select
> dbo.calc1(value) as val1,
> dbo.calc2(value) as val2
> from
> view1
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.078 seconds.