What is the better option (performance wise) Inline Scalar UDF, or Outer Apply in SQL 2012

StackOverflow https://stackoverflow.com/questions/22874390

  •  28-06-2023
  •  | 
  •  

I am currently using a bigger, more complex version of:

Select A.Address, A.City, A.State, A.Zip, S.MeterNumber, Ac.AccountNumber, C.CustomerName,
uci.fnGetAttribute(S.ServiceID, 'AMW') as Avg1Mw,
uci.fnGetAttribute(S.ServiceID, 'RateCode') as Rate

from Address a inner join Service s on a.addressid = s.serviceid
inner join vwAccountService v on s.serviceid = v.serviceid
inner join Account ac on v.accountid = ac.accountid
inner join Customer on ac.customerid = c.customerid

The function gathers the most recent attribute requested from a historical data table that has one entry per month.

Would an outer apply be a better choice here performancewise? Or is there a better construct for this?

有帮助吗?

解决方案

1) Write the queries you want to test

2) Click the "Include Actual Execution Plan" button in SQL Server Management Studio

3) Run both queries and it'll display how they perform versus each other. One might say it uses 80% of the entire amount of resources used for both queries. That would mean the other one is a lot better.

其他提示

My gut tells me that a CROSS APPLY, but the true test will be on how they perform against your actual data. It will depend, in part, of what the UDF is actually doing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top