Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of mysql WHERE ... IN

Tags:

sql

mysql

I have mysql queries with a WHERE IN statement.

SELECT * FROM table1 WHERE id IN (1, 2, 15, 17, 150 ....)

How will it perform with hundreds of ids in the IN clause? is it designed to work with many arguments? (my table will have hundreds of thousands of rows and id is the primary field)

is there a better way to do it?

EDIT: I am getting the Ids from the result set of a search server query. So not from the database. I guess a join statement wouldn't work.

like image 936
applechief Avatar asked Dec 13 '11 15:12

applechief


2 Answers

I am not sure how WHERE ... IN performes but for me it sounds like a JOIN or maybe a subselect would be the better choice here.

See also: MYSQL OR vs IN performance and http://www.slideshare.net/techdude/how-to-kill-mysql-performance

like image 64
PiTheNumber Avatar answered Oct 26 '22 17:10

PiTheNumber


You should put the IN clause "arguments" into table2 for instance.

Afterwords you make this:

SELECT t1.* FROM table1 t1
INNER JOIN table2 t2 ON t1.Id = t2.Id
like image 30
aF. Avatar answered Oct 26 '22 16:10

aF.