Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

When is Multi-Valued UDF faster than inline UDF?

Posted on 2012-03-13
8
Medium Priority
?
310 Views
Last Modified: 2012-03-20
Hi,

I have rewritten an UDF from inline to multi-valued UDF and was able to reduce the query time from ~20seconds down to ~2seconds.  All the literature indicates that inline are generally faster than multi valued UDF's.  

The original inline query looked something like the following:

create function udf_inline
(
   @p1 int,
   @p2 datetime
)
returns table
as
return

select
    *
from
      (
            select
                         *
            from
                  (
                        select
                              *
                        from
                              tableA
                        where
                              col1 = @p1
                              and col2 = @p2
                  )
      )
            

I have rewritten the query to use multi lined UDF


CREATE FUNCTION dbo.MV_UDF
(
   @p1 INT,
   @p2 DATETIME
)


RETURNS @myTable TABLE
(
   colA varchar(50),
   colB varchar(50),
   etc...
)
AS
BEGIN


insert into @myTable
select
    *
from
      (
            select
                         *
            from
                  (
                        select
                              *
                        from
                              tableA
                        where
                              col1 = @p1
                              and col2 = @p2
                  )
      )
RETURN


Why the multi-valued UDF runs faster I do not know.  Anyone can provide some insight?

thanks
0
Comment
Question by:yechan
8 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 37714562
Maintenance plan will learn you more about the different usage.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 37716438
Actually your "inline" udf_inline function is NOT a inline but still a table function if you look at its definition (below) and yours is fater as you work with explicit @table in return and most likely better optimized. but in my opinion you can still get rid of one unnecessary SELECT * in your function code.


create function udf_inline
(
   @p1 int,
   @p2 datetime
)
returns table
as
0
 
LVL 40

Expert Comment

by:lcohan
ID: 37716467
Also it may faster because the data was cached from running prior "inline" code so it just look faster but it may be not for the first time you run it with SQL cold cache.
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 40

Expert Comment

by:lcohan
ID: 37716521
And BTW....why not just a regular SQL stored proc if all you need a the record set returned byt the SQL code? I would use a table UDF if I need to join that record set but otherwise I would use from all aspects including performance just a regular SPROC like:

CREATE PROCEDURE dbo.My_SPROC
(
   @p1 INT,
   @p2 DATETIME
)
AS
SET NOCOUNT ON;

      select * from tableA
            where col1 = @p1 and col2 = @p2
       
RETURN
GO
0
 
LVL 1

Author Comment

by:yechan
ID: 37716793
Hi lcohan

Not sure why you would say that my inline function is not an inline function but is instead an Multi-Statement Table-Valued function.

Would love to convert it to a stored procedure but that function is being dynamically called from a web page but at this point, not sure if it is worth the trouble doing this.  At least not at the moent.
0
 
LVL 40

Accepted Solution

by:
lcohan earned 1100 total points
ID: 37716885
What I was trying to say is that the original function would still return a table like record set and if you execute that first then the second having almost the same code you may be misled by the execution time agreed? SQL will cache code and data therefor running the same SQL code inside first "inline" function udf_inline will take longer than running the identical code immediately after that from your new dbo.MV_UDF table function.
To be certain please run the commands below in a test environment and DO <<NOT>> run it in a production SQL box because it will fully flush data and code SQL cache.
Run them before each inline or table udf call for a cold SQL cache.

dbcc dropcleanbuffers;
dbcc freeproccache;

you could also use

set statistics time on;
set statistics io on;

to see the actual exec/compile times and IO for both of your functions.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 200 total points
ID: 37717652
Here is the definition from SQL Server's BOL for an Inline UDF

Inline user-defined functions follow these rules:

The RETURNS clause contains only the keyword table. You do not have to define the format of a return variable, because it is set by the format of the result set of the SELECT statement in the RETURN clause.
There is no function_body delimited by BEGIN and END.
The RETURN clause contains a single SELECT statement in parentheses. The result set of the SELECT statement forms the table returned by the function. The SELECT statement used in an inline function is subject to the same restrictions as SELECT statements used in views.
The table-valued function accepts only constants or @local_variable argument

So I would say yes, your first function is an Inline User-Defined Function.

But the point about caching is well made.  An Inline function should always out-perform a Multi-Statement function.  In other words, it may be just your perception.
0
 
LVL 1

Author Comment

by:yechan
ID: 37741896
Sorry for getting back so late.  Turns out that the web application wraps sql statements around sp_executesql whereas in SSMS I do not use sp_executesql.  This defn. changed things and probably explains the behavior.  Thank you for all the feedback.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question