Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Join of Function or Join of View, which one is cost effective? Expand / Collapse
Author
Message
Posted Tuesday, August 27, 2013 12:08 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 6, 2013 3:00 AM
Points: 7, Visits: 25
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.
Post #1488563
Posted Tuesday, August 27, 2013 12:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:58 PM
Points: 2,290, Visits: 2,752
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’! **
Post #1488565
Posted Tuesday, August 27, 2013 1:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 12, 2014 9:43 AM
Points: 7,284, Visits: 13,824
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1488597
Posted Tuesday, August 27, 2013 5:25 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 4:57 AM
Points: 802, Visits: 717
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.


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1488686
Posted Tuesday, August 27, 2013 5:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 6, 2013 3:00 AM
Points: 7, Visits: 25
Thanks Erland, I will use View based approach.
Post #1488690
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse