SQL Clone
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
Right there with Babe
Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)

Group: General Forum Members
Points: 737 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
Right there with Babe
Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)

Group: General Forum Members
Points: 737 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
) 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
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33563 Visits: 8682
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 on googles mail service
Jako de Wet
Jako de Wet
Right there with Babe
Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)Right there with Babe (737 reputation)

Group: General Forum Members
Points: 737 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 Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223358 Visits: 42003
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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