Join of Function or Join of View, which one is cost effective?

  • Hi,

    I have a table "Table1" on which I want to apply a join. I have two options.

    Option1: Create a multitable join view "View1" which will consist of union query as well along with a cross join. Apply join of "View1" on "Table1" to fetch the records.

    Option2: Create a Function "Function1" which will do some processing and return data in a Table variable. Apply join of this table on "Table1" to fetch the records.

    which one will be performance efficient in terms of time taken and memory utilisation.

    Thanks.

  • This is hard to tell because your description is too vague.

    You could build both solutions, set the statistics on (set statistics time on; set statistics io on;

    ) and execute both solutions. Also look at the execution plans. The output of the statistics and the execution plan will give you information to choose the best option.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Why restrict yourself to a view or a function? Why not just write the query?

    “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

  • A two-word answer is: it depends.

    That said, generally, using a multi-statement table valued function lowers your odds for good performance, because the optimizer has no clue about how many rows that function will return, so if this solution is more efficient, it's a bit of luck.

    On the other hand, if you replace the function with an INSERT into a temp table and then use that table in the query, this may be winner, because the temp table has statistics and may help with the next phase. Then again, the temp table means overhead.

    A view is just macro and the only effect of the view is that it makes the query easier to read. Or more difficult.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Thanks Erland, I will use View based approach.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply