This seems interesting.
Assume three tables, users
, items
and a join table users_items
. The latter links users.id
s to items.id
s. We say that user U
owns item I
when there's a row in the users_items
table that contains the pair (U.id
, I.id
).
Now: how would I go about randomly choosing an item that a particular user does not yet own?
The most feasible solution seems to be:
- gather all item
id
s. - gather
id
s of items owned by userU
. - remove all
id
s from step #2 from the set generated in #1 - pick random element from the resulting set (trivial)
Is this the only sane way? Is it optimal? Let's assume this would be a heavily used feature. And the numbers of elements in each table are huge.
Any fun ideas, thoughts?