views:

48

answers:

3

I have a table which holds ~1M rows. My application has a list of ~100K IDs which belong to that table (the list being generated by the application layer).

Is there a common-method of how to query all of these IDs? ~100K Select queries? A temporary table which I insert the ~100K IDs to, and Select query via join the required table?

Thanks, Doori Bar

A: 

Do those id's actually reference the table with 1M rows?

If so, you could use SELECT * ids FROM <1M table>

where ids is the ID column and where "1M table" is the name of the table which holds the 1M rows.

but I don't think I really understand your question...

Aelux
Yes, as I stated, the list of ~100K IDs belong to that table. Not sure if I understood your suggestion - you suggested to run ~100K select queries?
Doori Bar
No no... You run ONE select query for all the IDs in the 1M table, but I still don't understand what you're wanting... You have 1 million rows but only 100k ids? Shouldn't every row have an ID if each has and ID column?
Aelux
Let me clarify. The ~1M table has ~1M IDs. The application has a list of only ~100K IDs to select. How would you suggest me to query ~100K bulk IDs?
Doori Bar
I see now... so you want to search for the 100k ids WITHIN the 1M table. There is one easy way to do it, if you wrote a program that loaded all the rows into memory and then looped through them consecutively to find all the corresponding ids. Short of that, you'd have to write a SELET query for each ID.
Aelux
To be honest, creating a program that does that would probably only take me like 10 minutes in C# lol.
Aelux
A: 

You could do it in one query, something like

SELECT * FROM large_table WHERE id IN (...)

Insert a comma-separated list of IDs where I put the ... Unfortunately, there is no easy way that I know of to parametrize this, so you need to be extra-super careful to avoid SQL injection vulnerabilities.

tdammers
Select 100K IDs within 1 query? do you happen to know the maximum length of such query?
Doori Bar
A: 

A temporary table which holds the 100k IDs seems like a good solution. Don't insert them one by one though ; INSERT ... VALUES syntax in MySQL accepts the insertion of multiple rows.

By the way, where do you get your 100k IDs, if it's not from the database ? If they come from a preceding request, I'd suggest to have it fill the temporary table.

Edit : For a more portable way of multiple insert :

INSERT INTO mytable (col1, col2) SELECT 'foo', 0 UNION SELECT 'bar', 1 
Vanmachin
What would be the maximum length of such insert queries? Is it common to be able to insert ~100K at once?
Doori Bar
@Vanmachin, SQLite can't do multiple inserts like that, though.
MPelletier
I don't know how MySQL scale with this type of request, but to avoid a too large request, I'd go for a hundred IDs by INSERT as a start.@MPelletier : answer edited
Vanmachin
@Vanmachin: I was wondering, why wouldn't you suggest to simply select 100 IDs at a time, which means instead of ~100K select queries - It's ~1K select queries, without the need for the initial inserts/temporary table?
Doori Bar
@MPelletier : didn't test it, just read SQLite manual which didn't said it was not good
Vanmachin
@Doori Bar : this works too, but in case you need to additional treatment on the server, you have your IDs at hand ; plus, if the unique SELECT following have multiple joins, the query engine will be more efficient
Vanmachin
You need to use parameterized queries when you use sqlite else sqlite will spend too much time parsing all those different queries. See http://stackoverflow.com/questions/904796/how-do-i-get-around-the-problem-in-sqlite-and-c/926251#926251 . Just do a lot of insert..values statements and bundle all those insert..value statements in one transaction because the commit is slow in sqlite.
TTT
Sqlite's insert is fast but the commit is slow.
TTT