Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats better- query with long 'where in' condition or many small queries?

Maybe it's a little dumb, but i'm just not sure what is better. If i have to check more than 10k rows in db for existanse, what i'd do?

#1 - one query

select id from table1 where name in (smth1,smth2...{till 30k})

#2 - many queries

select id from table1 where name=smth1

Though, perfomance is not the goal, i don't want to go down with mysql either ;) Maybe, any other solutions will be more suitable... Thanks.

upd: The task is to fetch domains list, save new (that are not in db yet) and delete those that dissappeared from list. Hope, it'll help a little...

like image 637
DCrystal Avatar asked Apr 26 '10 18:04

DCrystal


2 Answers

What you should do is create a temp table, insert all of the names, and (using one query) join against this table for your select.

select id 
from table1 t1
inner join temptable tt on t1.name = tt.name
like image 145
D'Arcy Rittich Avatar answered Sep 18 '22 02:09

D'Arcy Rittich


The single query will most likely perform better as the second will give a lot of round-trip delays. But if you have a lot of names like in your example the first method might cause you to hit an internal limit.

In this case it might be better to store the list of names in a temporary table and join with it.

like image 34
Mark Byers Avatar answered Sep 21 '22 02:09

Mark Byers