views:

38

answers:

2

Hey there

I'm trying to find a way to check if some IDs are already in the DB, if an ID is already in the DB I'd naturally try to avoid processing the row it represents

Right now I'm doing a single query to check for the ID, but I think this is too expensive in time because if I'm checking 20 id's the script is taking up to 30 seconds

I know i can do a simple WHERE id=1 OR id=2 OR id=3 , but I'd like to know of a certain group of IDs which ones are already in the database and which ones are not

I don't know much about transactions but maybe this could be useful or something

any thoughts are highly appreciated!

+1  A: 

Depends how you determine the "Group of IDs"

If you can do it with a query, you can likely use a join or exists clause.

for example

SELECT firstname
  from people p 
  where not exists (select 1 from otherpeople op where op.firstname = p.firstname)

This will select all the people who are not in the otherpeople table

If you just have a list of IDs, then use WHERE NOT IN (1,3,4...)

Chad
I really like this approach, how can I generate a table on the fly with my ids?
PERR0_HUNTER
+1  A: 

30 seconds for 20 queries on a single value is a long time. Did you create an index on the ID field to speed things up?

Also if you create a unique key on the ID field you can just insert all ID's. The database will throw errors and not insert those those ID's that already exist, but you can ignore those errors.

Matijs
Yes, indeed the ID is the primary key, If I try the method you said and try to insert everything anyway I'm still wasting CPU time in preparing what is going to be inserted and thats something I'd like to avoid
PERR0_HUNTER
You do that in any case. Whatever method you choose, your SQL server has to perform a check before insertion. Only if the insertion is a large amount of data (say at least in megabytes) will the preparation time be a factor.
Matijs