On one of my PHP/MySQL sites, every user can block every other user on the site. These blocks are stored in a Blocked
table with each row representing who did the blocking and who is the target of the block. The columns are indexed for faster retrieval of a user's entire "block list".
For each user, we must exclude from any search results any user that appears in their block list.
In order to do that, is it better to:
1) Generate the "block list" whenever the user logs in by querying the Blocked
table once at login and saving it to the $_SESSION
(and re-querying any time they make a change to their "block list" and re-saving it to the $_SESSION
), and then querying as such:
NOT IN ($commaSeparatedListFromSession)
or
2) Exclude the blocked users in "real-time" directly in the query by using a sub-query for each user's search query as such:
NOT IN (SELECT userid FROM Blocked WHERE Blocked.from = $currentUserID)
?