Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a stored procedure in which i will generate records for users. But recently users feedback that the records generation are slow and we have decided to fine tuning the stored procedure itself. In my stored procedure, i have some SELECT statements in which i will join permanent table with table valued function(TVF) and my DBA feedback we can further improve the performance simply by replacing the TVF with table variable. Please see below for example of before and after changes:
SQL
SELECT
	 c.CustomerName
	,c.CustomerAddress
	,c.CustomerOrderNo
FROM
	dbo.Customer AS c
	INNER JOIN dbo.utf_CustomerDetailInfoStatus('ACTIVE') AS cdi
		ON c.CustomerStatusId = cdi.CustomerStatusId


SELECT
	 c.CustomerName
	,c.CustomerAddress
	,c.CustomerOrderNo
FROM
	dbo.Customer AS c
	INNER JOIN @CustomerDetailInfoStatus AS cdi
		ON c.CustomerStatusId = cdi.CustomerStatusId


The above example is just a simple mock up to show the changes. There are 5 TVF that I have replaced with table variables but after some testing, i found the performance has deteriorate further from around 20 seconds to 6 minutes of execution duration(both returned 10k of records)! I change the table variables back into TVF and it returned to around 20 seconds. But my DBA insists that by replacing the TVF with table variables, other factors such as table locking, I/O spiking can be reduced but through testing from my end, the result is not what i have been expecting.
Does anyone has knowledge on this?

What I have tried:

1. Done some study on TVF vs Table Variable but none of the topics is somewhere related to my case here.
Posted
Updated 19-Sep-18 18:16pm
Comments
Santosh kumar Pithani 20-Sep-18 0:01am    
i think TVF is causing performance issue so try to put TVF code for review else apply proper index on filter of TVF code.
Richard Deeming 20-Sep-18 9:23am    
The SQL query optimizer tends to assume that a table variable only contains a single row. If you've got lots of rows in the table variable, you might be better off using a temporary table instead.

1 solution

SQL
--if you don't have proper index on Customer,TVF i'm help less.

CREATE INDEX Customer_index(CustomerStatusId) INCLUDE(CustomerName,CustomerAddress,CustomerOrderNo);

SELECT
	 c.CustomerName
	,c.CustomerAddress
	,c.CustomerOrderNo
FROM
	dbo.Customer AS c 
	  CROSS APPLY dbo.utf_CustomerDetailInfoStatus('ACTIVE') AS cdi
		WHERE
             c.CustomerStatusId = cdi.CustomerStatusId OPTION(FAST 10);

 --try like this too

SELECT
	 c.CustomerName
	,c.CustomerAddress
	,c.CustomerOrderNo
FROM
	dbo.Customer AS c   WHERE EXISTS(
  SELECT 1 FROM  dbo.utf_CustomerDetailInfoStatus('ACTIVE') AS cdi
		WHERE
                   c.CustomerStatusId = cdi.CustomerStatusId 
                                         );
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900