June 14, 2006 at 7:27 pm
Is their a performance difference with using either or?
June 15, 2006 at 8:46 am
They both serve a different purpose, so performance comparison is not really applicable.
June 15, 2006 at 9:44 am
unless you want to degrade performance in which using functions badly will certainly achive this!
[font="Comic Sans MS"]The GrumpyOldDBA[/font]
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
June 15, 2006 at 10:46 am
can you give me some examples of using functions badly? appreciate it!
June 15, 2006 at 11:23 am
A Select statement using a function to generate a column. So far so good - looks harmless enough:
Select dbo.SomeFunction(t.Column1) As FunctionResult
From YourTable As t
Where ...
The function definition:
Create Function dbo.SomeFunction(@Param As varchar)
As
Begin
Return (
Select Max(SomeValue)
From SomeOtherTable
Where SomeKey = @Param
)
End
Ouch. You've essentially created a cursor and created a performance issue. The function performs another SELECT, which has to get processed for *every* row through the main resultset. Horrible performance awaits. And a profitable consulting gig awaits the SQL professional who knows not to use functions like this and who gets called in to clean up the mess 🙂
June 17, 2006 at 7:48 am
It really depends on the sproc you build... if it returns a scalar value, as does a function, then there's no advantage to a sproc and it's easier to use the function.
But, PW hit the nail on the head... The real key, as PW implied, is that both (Scalar proc or function) are a form of "RBAR" (Row By Agonizing Row). Sure, there are some very useful functions that you can build but they are not a panacea nor a replacement for good set based code.
Now, for GUI support code, functions are pretty good because, ususally, a GUI is working with one row at a time or a very small set of rows. Again, though, PW is correct... a function like the example PW gave just shouldn't be allowed in the database...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply