November 20, 2007 at 9:11 am
From a sql-server-performance posting (edited):
Carl Federl (11/16/2005)
I may change my mind on UDFs.I am in the process of writting an article and a set of test cases but here is an interesting post:
Did you ever finish that article?
When using a UDF in the where, run time goes from < .01 seconds to 2.14 minutes (134 seconds)
-- Comment this is 13 thousand times slower
I also see bad performance hits (which is why I wound up in this old thread), but the question is why? In any other language one would assume that function calls were badly implemented; why does T-SQL get a pass?
November 20, 2007 at 10:06 pm
Jim,
Is this in reference to the table variable function you created that calls itself?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 21, 2007 at 4:36 am
Same topic (http://www.sqlservercentral.com/Forums/FindPost424410.aspx), but I don't see anywhere the function is calling itself. You may have found the key, but I am still missing it. The table returning function being called isn't recursive; the only difference is the utility functions to get the parameters. If the utility function return values are placed in local variables, and those variables are then passed as arguments to the table returning function, it runs fast. If instead, the utility functions are in-lined in the table returning function calling sequence, it runs forever.
I appreciate your interest in this; help me see the light!
November 21, 2007 at 8:46 pm
Jim Russell (11/20/2007)
When using a UDF in the where, run time goes from < .01 seconds to 2.14 minutes (134 seconds)
-- Comment this is 13 thousand times slower
I also see bad performance hits (which is why I wound up in this old thread), but the question is why? In any other language one would assume that function calls were badly implemented; why does T-SQL get a pass?
Because often enough the Optimizer is "too stupid" to know what it doesn't need to repeat for each row in a table. The biggest and most common reason the performance goes "south" like that is that the query goes from being a seek to a scan (usually an index seek to a table scan), because the function's output is not determined to be deterministic, so it is run once for every row (often time even when the function is fully deterministic from a human standpoint, but NOT from a SQL Server standpoint).
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
November 22, 2007 at 1:33 am
Can I assume that it is acceptable to put parameterized sql into a stored procedure? Nobody is saying don't do that, right?
What solution would people suggest if the table returned from a stored procedure needed (was desired) to be joined to another table? Or would that simply be another indicator of flawed thinking?
November 23, 2007 at 4:32 pm
Output from a stored procedure does not have a PK Constraint and cannot be indexed unless first materialized as a table. So, it depends...
--Jeff Moden
Change is inevitable... Change for the better is not.
November 27, 2007 at 3:48 am
Jeff:
Obviously everything always depends... I've previously used UDFs where I've had some complex query and the result needs to be reused in multiple places, which has not exactly improved performance.
I guess what I'm interested in is how other people manage a similar situation. Stored procedures and temporary tables? Cut and paste? etc.
November 27, 2007 at 8:04 pm
If it's within the same proc, yes... I'll many times use a temp table with indexes, etc. If there are several procs that require the same process, I'll build the temp table within each proc and call the common proc with the understanding that it may use the temp table. I'd rather do it that way than to use a Global Temp table.
In other instances, I'll create a "real" working table that I leave as a table just to see what the last results were.
Then, there's tools like the "Tally" or "Numbers" table that have allowed me to circumvent the need for a great number of UDF's including Split functions, sequential date or date/time bin creation, etc, etc.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 28, 2007 at 1:06 am
Thanks Jeff - it's always good to know what others are doing.
Viewing 9 posts - 16 through 23 (of 23 total)
You must be logged in to reply to this topic. Login to reply