Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: SELECT IN faster and best practice?

Tags:

sql

if i m using 2 queries:

first: queries all keys/ids required

second: select * from tab1 where tab1.id in (...ids list..,,)

ids list can be several thousands...

is it wise or best practice or recommended to do things like that?

like image 419
Abu Aqil Avatar asked Feb 27 '26 04:02

Abu Aqil


2 Answers

As a general rule, it's perfectly acceptable best practice to use an IN clause in your statements... given that you use a subquery for larger sets of values.

The answer should always be: it depends! It's not clear from your question whether your list of values would be a SELECT, or whether you'd hardcode them. It would definitely be more performant if those several thousand value were in another table (temp table or table variable) and you subqueried them. i.e.

    SELECT * FROM Customer 
    WHERE CustomerID IN (SELECT ID FROM MyOtherLargeTableOfCustomers)
  • how well indexed is that column that you're performing that IN clause on?
  • what's the datatype? If a numeric datatype, then you shouldn't have any performance problems.
  • ensure your STATISTICS (if SQL Server) are updated regularly.
  • if the datatype is char-based, or guid, you may run into some performance problems, especially when you're running multiple queries at the same time, and the list is in the thousands as you describe.
  • consider JOINing those tables instead; that should always produce a faster query.
SELECT * FROM Customer AS C 
INNER JOIN MyOtherLargeTableOfCustomers AS M
ON C.CustomerID = M.CustomerID
like image 169
p.campbell Avatar answered Feb 28 '26 16:02

p.campbell


if your IDs are queryable in a joinable table, using an inner join SHOULD give you the fastest query and give you a substantially smaller query to send to the db.

like image 28
Jason Avatar answered Feb 28 '26 17:02

Jason