Take the 2-minute tour ×

Which of the following query is better... This is just an example, there are numerous situations, where I want the user name to be displayed instead of UserID

Select  EmailDate, B.EmployeeName as [UserName], EmailSubject
    from Trn_Misc_Email as A
         inner join 
         Mst_Users as B on A.CreatedUserID = B.EmployeeLoginName


Select  EmailDate, GetUserName(CreatedUserID) as [UserName], EmailSubject
    from Trn_Misc_Email

If there is no performance benefit in using the First, I would prefer using the second... I would be having around 2000 records in User Table and 100k records in email table...


share|improve this question
Have you tried this for yourself? –  Neil Knight Sep 1 '10 at 9:25

4 Answers 4

up vote 7 down vote accepted

A good question and great to be thinking about SQL performance, etc.

From a pure SQL point of view the first is better. In the first statement it is able to do everything in a single batch command with a join. In the second, for each row in trn_misc_email it is having to run a separate BATCH select to get the user name. This could cause a performance issue now, or in the future

It is also eaiser to read for anyone else coming onto the project as they can see what is happening. If you had the second one, you've then got to go and look in the function (I'm guessing that's what it is) to find out what that is doing.

So in reality two reasons to use the first reason.

share|improve this answer

The inline SQL JOIN will usually be better than the scalar UDF as it can be optimised better.

When testing it though be sure to use SQL Profiler to view the cost of both versions. SET STATISTICS IO ON doesn't report the cost for scalar UDFs in its figures which would make the scalar UDF version appear better than it actually is.

share|improve this answer
Do you have any references that help me in understanding the Execution plan and the Statistics Informations –  The King Sep 1 '10 at 9:56
@The King - See this free ebook "Dissecting SQL Server Execution Plans" by Grant Fritchey –  Martin Smith Sep 1 '10 at 10:21

Scalar UDFs are very slow, but the inline ones are much faster, typically as fast as joins and subqueries

BTW, you query with function calls is equivalent to an outer join, not to an inner one.

share|improve this answer

To help you more, just a tip, in SQL server using the Managment studio you can evaluate the performance by Display Estimated execution plan. It shown how the indexs and join works and you can select the best way to use it.

Also you can use the DTA (Database Engine Tuning Advisor) for more info and optimization.

share|improve this answer
Scalar UDFs are just treated as a black box on this I think. –  Martin Smith Sep 1 '10 at 9:34
Do you have any references that help me in understanding the Execution plan and the Statistics Informations. –  The King Sep 1 '10 at 9:51
@Martin Smith, nope, the execution plan clearly shown with all the steps and what happen as cost, .... You can try by yourself. Plus a scalar UDF cache the execution plan for optimize the execution in the next runs –  Luka Milani Sep 1 '10 at 10:00
@The King, there are a lot of sites that explain the execution plan. Here one for example: simple-talk.com/sql/performance/… –  Luka Milani Sep 1 '10 at 10:04

Your Answer


