I have a large list of strings (1500 email addresses to be more specific) and I need to look up a piece of data in a very large DB table for each of the strings (e.g. the primary key, mydataitem
). How can I do it efficiently?
For example, this is way too slow (amongst other problems):
$stringArray = ('foo','bar','baz',..., 'for 1000s of items');
foreach($stringArray as $mystring) {
$res = mysql_query("select mydataitem,blah FROM users WHERE blah = '$mystring'");
$info=mysql_fetch_assoc($res);
...
}
Things I want to avoid:
- I don't want to loop over the list and do a SELECT for each item. i.e. 1500 queries (as in example above)
- I don't want to read the whole table into an array in one query and do the lookup in code because it would take too much memory. The DB table has 100k+ rows.
- I don't want to build a massive query with 1499 ORs because the query would be too big. (for example "
select mydataitem FROM users WHERE blah = 'aaa' OR blah = 'bbb' OR ...
")
Note: I'm using MySql v5.0.45
Update: Thanks everyone - for some reason I thought IN
was just for Integer ID lists - now I know better.