I have an SQL Azure database with a table that has JobId primary key. A user sends in a bunch of “job ids” as an array that can contain one or more (maybe ten, maybe ten thousand) values. I can’t decide how to do write SQL queries for that. I will use .NET framework classes.
One option I see is creating a temp table and doing a WHERE IN nested SELECT (SQL+C#-like pseudocode):
CREATE TEMP TABLE JobIdsTable
foreach( JobId in JobIdsFromClients ) {
INSERT INTO JobIdsTable VALUES (JobId)
}
SELECT * FROM JobsTable WHERE JobId IN SELECT * FROM JobIdsTable
Here it looks cool from code writing standpoint, but I’m not sure it will always run fast.
Another option is creating a huge OR concatenation forming a giant SELECT:
SELECT * FROM JobsTable WHERE JobId='JobId1' OR JobId='JobId2' OR JobId='JobId3' .....
Here it’s one SELECT but the idea of concatenating this longs string doesn’t sound good (normally I’d use System.Data.SqlClient.SqlCommand.Parameters to build a parameterized query, but here it will be very complicated. Also I’m unsure of whether there’s a limit on how long an SQL query can be.
I’d like my query to be both manageable and performant. So I see drawbacks in both options.
Which do I choose? Do I do it any other way instead?
I’d go with the temp table, but use a join instead of a subquery. if the Id columns are indexed, the performance should be decent: