Lonefish
Lonefish

Reputation: 677

One query that gets all data vs 300 specific queries

First of all, I'm aware that there are similar questions out there, but I can't seem to find one that fits my case.

I have a program that updates the stock in a CSV file of about 300 products by checking with our database.

The database has +/- 100k records. At the moment I'm doing a select of all records in the database and just use the ones I need. But I selected 99k records too much.

I could do specific selects for the products too, but I'm worried sending 300 queries (might become more in the future) in a matter of seconds would be too much.

Or another (probably stupid) option:

select * from database where id=product1 || id=product2 
    || id=product3 ||     id=product4,...

What's the better option in this case? I'm not worrying about execution time, I'd rather have clean and "efficient" code than fast code in this scenario.

Upvotes: 1

Views: 64

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

You can try like this:

select *
from database where id IN (1, 2, 3)

If the count of values to search is more than the count which is not then do the reverse

select *
from database where id NOT IN (some values)

Upvotes: 3

Jeff Burgin
Jeff Burgin

Reputation: 175

You could do something like this:

select *
from database
where id IN (1, 2, 3)

All of your ids that you want to get can just go into that array so you don't have to use a long list of or clauses.

Upvotes: 1

Related Questions