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?
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)
IN
clause on?STATISTICS
(if SQL Server) are updated regularly.JOIN
ing 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
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.