• BSavoie (2/28/2013)


    Thanks all for the great advice. Rewriting the sproc is not an option at this time. Since this is just a wrapper around the real work horse, I guess it's really going to make little difference how the wrapper is coded. It seems like in this particular situation an RBAR solution is almost a clearer representation of what's going on. I'm finding that the real problem is that this sproc was not very well optimized, that's where I'm going to need to focus my time.

    I'll definitely keep the cross apply / function up my sleeve for next time. Although, a colleague of mine is trying to convince us to stay away from UDF's. Seems the optimizer really doesn't deal with them very well in most common situations. I think table value functions might be a little more optimizer friendly.

    If you're lucky, creating the multistatement table-valued function might require little more than a change to the object type in the CREATE script for the stored procedure. Obstacles would include temporary tables, which would need to be changed to table variables, and dynamic SQL.

    Inline and multistatement TVF's (and inline scalar functions[/url]) are dealt with by the optimiser quite nicely. A quick test might take you no more than a few minutes and would show you a) if it's a straightforward process and b) the execution plan.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden