Use your quick query in the subquery and remove duplicates in the outer select:
SELECT DISTINCT sub.id FROM ( SELECT a.id FROM a INNER JOIN b ON a.id=b.id_table_a && b.condition1=1 WHERE b.id_table_a > :offset ORDER BY a.column1 LIMIT 50 ) sub
Due to removing duplicates, you can get less than 50 rows. Just repeat the query until you get aough strings. Start with :offset = 0 . Use the last identifier from the last result as :offset in the following queries.
If you know your statistics, you can also use two restrictions. The limit in the inner query must be high enough to return 50 different rows with a probability that is high enough for you.
SELECT DISTINCT sub.id FROM ( SELECT a.id FROM a INNER JOIN b ON a.id=b.id_table_a && b.condition1=1 ORDER BY a.column1 LIMIT 1000 ) sub LIMIT 50
For example: if you have an average of 10 duplicates per identifier, LIMIT 1000 in an internal query will return an average of 100 different rows. It is very unlikely that you will receive less than 50 rows.
If the condition2 column is boolean, you know that you can have a maximum of two duplicates. In this case, a LIMIT 100 in the internal query will suffice.
Paul spiegel
source share