February 6, 2013 at 1:03 pm
k.srikalyan (2/6/2013)
Try to create a primary key/unique key on your table variable, and it is always better to go as a stored proc using temp tables and while loop instead of cursors and try building a clustered index on the temp table which will definitely improve your performance.
bobie (2/6/2013)
Not really looking in detail butI think it should be a store procedure.
And I quite sure we can eliminated the cursor using temp table and update and set variables in a single query.
Absolutely not. Temp table/While loop replacements for cursors is like smoking a pipe to quit cigars. They're essential identical and a well written firehose cursor (read only, forward only) is frequently faster than the Temp Table/While loop combination. To wit, replacing cursors with While Loops does little to help the situation and will sometimes cause even worse performance because most people aren't good at writing either. 😉
Please don't make such recommendations in the future because it's a myth and a general bad recommedation to replace cursors with Temp Tables/While loops.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 7, 2013 at 7:38 pm
What I mean is not a combination temp table and while loop but temp table with an bulk update statement with help of variable, is it the same as while loop?
Because if I check the statistics io and the time needed to execute it is much2 lower io and faster.
February 8, 2013 at 8:00 am
ChrisM@Work (2/5/2013)
Steven Willis (2/5/2013)
Just a generic observation...If this function is taking two hours to run--even with all of the cursors and subcursors--then you are probably accessing a LOT of data. I don't think a function is even appropriate. Is this being called by another procedure or query? If so the issues raised are multiplied by how many times the function itself is being called.
You should write a procedure instead. ..
Not necessarily. A properly written iTVF inlines like a view. This function is just a 4-table query with a few sums in it. I can't see any reason why it shouldn't be written as an iTVF regardless of how many rows the base tables contain.
+100!
--Jeff Moden
Change is inevitable... Change for the better is not.
February 8, 2013 at 8:04 am
bobie (2/7/2013)
What I mean is not a combination temp table and while loop but temp table with an bulk update statement with help of variable, is it the same as while loop?Because if I check the statistics io and the time needed to execute it is much2 lower io and faster.
Ah! Understood. Yes. Doing a "Divid'n'conquer" using a Temp Table to hold interim results during "bulk updates" is much better than using cursors or while loops. Of course, you can't use Temp Tables in functions and Table Variables usually aren't the thing to use when a lot of rows are present.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 16 through 18 (of 18 total)
You must be logged in to reply to this topic. Login to reply