Mysql: 1000 select queries vs 1 query ( select .. in )

MySQLperformance

I have just read about N+1 queries problem. So I have the doubt about the performance of 2 ways.

The way 1: I have 1000 queries like that:

select * from topic where user_id = 1

select * from topic where user_id = 2

select * from topic where user_id = 1000

I supposed the topic table has 1.000.000 rows and each query uses binary search, so I think the Big O's way is:

1000 * log2(1.000.000)

The way 2: I have 1 query:
select * from topic where user_id in (1,2,…, 1000)

I don't know the algorithm of 2nd way. So can you explain me the algorithm of (select .. in) query. Is there any case the 1st way is better than 2nd way?

Best Answer

to get the same result, you need to UNION the result and you can't union 10000 selects. the limit is 63 table references per query

Further IN clause has also a limit of elements you can use.

The number of values in the IN() list is only limited by the max_allowed_packet value.

see manual

As you have many ids, you should try

select t1.* 
from topic t1
      INNER JOIN 
     (SELECT  user_id FROM topic WHERE user_id <= 1000) t2 ON t1.user_id = t2.user_id

On big numbers for the IN you will get faster results.