views:

97

answers:

3

I'm running SQL query for MySQL server with

...
where name in ("term1","term2","term3")

I would like to get the list of terms from a text file with one term per line. How can I do this?

What about if this list of terms will get large? 10K, for example. Will this be efficient or I should use another approach? May be temporary table and join? The database is read-only.

A: 

I think your best bet might be to read this into a temp table/ table used specifically for this look up.

This way you can index the table, and use an inner join/ where in sub query.

astander
A: 

Generally, using a WHERE ... IN statement gets very slow after a couple of hundred/thousand terms.

Also, if the table is read-only, then you won't be able to add indexes to make it any faster, so, like astander said, I would suggest a temporary table with a join.

Topher Fangio
A: 

I am not so familiar with MySQL but I see something like this where you can load a text file in a table as you suggested in your question:-

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, column2, column3);

and then use joins for getting data.

Details here.

ydobonmai
I have to create t1 table first, right? Can I do it, if the database is read-only?
yuk
In SQL server, you could create the table in a different database and read from the other (read-only) database using the DatabaseName..TableName notation. I am not very sure if this is possible in MySQL. Should be possible there as well.
ydobonmai
Unfortunately this public MySQL server does not give permissions to create neither temp table or database.
yuk
Thank you all, guys! I just wanted to accept this question not to leave it unanswered. Since all questions were about the same and about at the same time, I accepted this one for code example and link to details.
yuk