Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 5123

Query Tuning - IN Operator

$
0
0

Dear Friends,

I have a the following query which takes long time

DECLARE @ACCOUNTS TABLE(ACCOUNT_ID INT)

INSERT INTO @ACCOUNTS
SELECT ACCOUNT_ID FROM ACCOUNT
WHERE A_DESCR in ('AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG')

SELECT ID.ITEM_ID
FROM INVOICE I INNER JOIN INVOICE_DETAIL ID
ON I.INVOICE_ID = ID.INVOICE_ID
WHERE ACCOUNT_ID IN (SELECT A.ACCOUNT_ID FROM @ACCOUNTS A)

I noticed that the problem is in the IN operator 

when I replaced the sub-query and hard coded its results, the the query run fast.

SELECT ID.ITEM_ID
FROM INVOICE I INNER JOIN INVOICE_DETAIL ID
ON I.INVOICE_ID = ID.INVOICE_ID
WHERE ACCOUNT_ID IN (1,2,3,4,5,6,7)

I searched for a solution on Google but I did not find an answer.

I used the following key words: sql server query tuning in operator 

Thanks for helping.


Viewing all articles
Browse latest Browse all 5123

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>