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 queryFurther
IN
clause has also a limit of elements you can use.see manual
As you have many ids, you should try
On big numbers for the
IN
you will get faster results.