1

I'm using stored procedure to fetch data and i needed to filter dynamically. For example if i dont want to fetch some data which's id is 5, 10 or 12 im sending it as string to procedure and im converting it to table via user defined function. But i must consider performance so here is a example:

Solution 1:

SELECT * 
FROM Customers 
WHERE CustomerID NOT IN (SELECT Value 
                         FROM dbo.func_ConvertListToTable('4,6,5,1,2,3,9,222',','));

Solution 2:

CREATE TABLE #tempTable (Value NVARCHAR(4000));

INSERT INTO #tempTable 
        SELECT Value FROM dbo.func_ConvertListToTable('4,6,5,1,2,3,9,222',',')

SELECT * 
    FROM BusinessAds 
    WHERE AdID NOT IN (SELECT Value FROM #tempTable)

DROP TABLE #tempTable

Which solution is better for performance?

1 Answer 1

2

You would probably be better off creating the #temp table with a clustered index and appropriate datatype

CREATE TABLE #tempTable (Value int primary key);
INSERT INTO #tempTable 
SELECT DISTINCT Value 
FROM dbo.func_ConvertListToTable('4,6,5,1,2,3,9,222',',')

You can also put a clustered index on the table returned by the TVF.

As for which is better SQL Server will always assume that the TVF will return 1 row rather than recompiling after the #temp table is populated, so you would need to consider whether this assumption might cause sub optimal query plans for the case that the list is large.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.