sql - Is there a performance disadvantage if an in-line function calls a scalar function? -



sql - Is there a performance disadvantage if an in-line function calls a scalar function? -

i've been getting grips apply, , @ same time reading performance improvement of in-line functions on scalar functions (see rob farley's post here).

i have application manages requests. 1 request might apply single person on system, or many persons. transactions (not sql tran!!) created against requests.

so, i've written little function returns latest transaction of type, given personid , requestid

alter function [dbo].[fnreturnpersoncollectiontime] ( -- add together parameters function here @personid int, @requestid int ) returns table homecoming ( select top 1 transactions.datecreated transactiondatetime, transactions.fkpersonid, transactions.fkrequestid transactions ((transactions.fkpersonid = @personid) , (transactions.fkrequestid = @requestid) , (transactions.fktransxtypeid = 169)) order transactions.datetime desc )

here in action.

select tx.transactiondatetime request inner bring together requestpersons on request.id = requestpersons.fkrequestid inner bring together person on requestpersons.fkpersonid = person.id cross apply dbo.fnreturnpersoncollectiontime(requestpersons.fkpersonid, requestpersons.fkrequestid) tx

however. colleagues , don't magic number 169 in clause of function - in other places in application have used scalar functions homecoming singleton values these (this is, believe, regarded thing, since such references centralised single location). phone call scalar function.

alter function [dbo].[fnreturnpersoncollectiontime] ( -- add together parameters function here @personid int, @requestid int ) returns table homecoming ( select top 1 transactions.datecreated transactiondatetime, transactions.fkpersonid, transactions.fkrequestid transactions ((transactions.fkpersonid = @personid) , (transactions.fkrequestid = @requestid) , (transactions.fktransxtypeid = dbo.fnreturncollectiontransactionid())) order transactions.datetime desc )

so question - if in-line function requires phone call scalar function, negate accrues writing in-line function in first place?

many thanks

edward

i recommend changing either inline table function or params table (as suggested hogan).

i wrote next test demonstrate severe performance difference between itvf , scalar function:

;with l0 (select 0 c union select 0), l1 (select 0 c l0 cross bring together l0 b), l2 (select 0 c l1 cross bring together l1 b), l3 (select 0 c l2 cross bring together l2 b), l4 (select 0 c l3 cross bring together l3 b), l5 (select 0 c l4 cross bring together l4 b), nums (select row_number() over(order (select 0)) n l5) select n keyfield #testdata nums n <= 10000; create table tempdata (keyfield int) insert tempdata (keyfield) values (30) go create function fn_test() returns int schemabinding begin homecoming (select top 1 keyfield dbo.tempdata) end go create function fn_test2 (@keyfield int) returns table homecoming ( select top 1 keyfield dbo.tempdata keyfield = @keyfield ) go select * #testdata keyfield = dbo.fn_test() --select * #testdata td cross apply dbo.fn_test2(td.keyfield) drop table #testdata drop function dbo.fn_test drop function dbo.fn_test2 drop table tempdata

this populates 10000 rows , uses function determine rows match tempdata (30). on system, scalar function takes ~3 seconds, itvf instant.

sql sql-server-2008 tsql

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -