Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Performance - Using Inline/Table Functions


Query Performance - Using Inline/Table Functions

Author
Message
Jako de Wet
Jako de Wet
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 215
Hi everyone, I have query that doesn't even register a time when running it. But when I add the lines that are commented out in the code below, it takes between 20 and 30 seconds! When I run the code for functions directly, it also runs without registering a time. I know when I include it like this, it loses the Indexing capabilities?


SELECT
      .
      .
      .
      ----, ISNULL(CAST(NULLIF(dbo.ufnGetRetail(I.ISBN13),0.00) AS VARCHAR(20)), 'N/A') RetailPrice
      ----, ISNULL(CAST(NULLIF(SP.LocalPrice,0.00) AS VARCHAR(20)),'on request') LocalPrice
      .
      .
      .
   FROM
      TABLE1 T1
         INNER JOIN dbo.TABLE2 T2 ON T1 = T2
   ---OUTER APPLY dbo.ufnGetLocal(T1.ISBN13) SP
   FOR XML AUTO, ROOT ('BS')


Any idea how to have the functions included but have the query response time come down?
Jako de Wet
Jako de Wet
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 215
I went through the functions and realized they are not needed as advanced as they are in this query.

"Fixed" it by using this:


SELECT
      .
      .
      .
      , ISNULL(CAST(NULLIF(PR.RetailPrice,0.00) AS VARCHAR(20)), 'N/A') RetailPrice
      ,
      (
         SELECT TOP 1
            (LocalPrice * 1.08) LocalPrice
         FROM LocalPriceList
         WHERE RecordReference = T1.Key
         ORDER BY LocalPriceList ASC
      Wink LocalPrice
      .
      .
      .
   FROM
      TABLE1 T1
         INNER JOIN dbo.TABLE2 T2 ON T1.Key = T2.Key
         LEFT JOIN RetailPriceList PR ON PR.Key = T1.Key
   FOR XML AUTO, ROOT ('BS')


Guess its probably not the best, but performance wise its WAY better
TheSQLGuru
TheSQLGuru
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5937 Visits: 8298
UDFs can be one of the worst things you can do in SQL Server. Read my Death by UDF chapter in the SQL Server MVP Deep Dives 2 book and/or download my SQL Saturday presentation by the same name here: http://www.sqlsaturday.com/viewsession.aspx?sat=122&sessionid=10200

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Jako de Wet
Jako de Wet
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 215
Thanks Kevin

Will read through it tomorrow.

Interested to read how to get info back in a query that requires more than a plain join.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44981 Visits: 39869
Jako de Wet (2/26/2014)
Thanks Kevin

Will read through it tomorrow.

Interested to read how to get info back in a query that requires more than a plain join.


Do the functions have the word "BEGIN" in them? If they do and they're in the FROM clause, then they are mTVFs (Multi-Statement Table Valued Functions) and not iTVFs (Inline Table Valued Functions). mTVFs are usually worse than even scalar functions.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search