On one of my PHP sites, I need to offer my Users the ability to select certain rows to exclude from all search results (queries) on the site.
Which way should I go about handling the exclusions, should I:
1) Pre-compile that list of excluded rows whenever the user logs in (or updates it) by querying it, save the list in a Session variable, and then simply add $query .= " AND userid NOT IN ({$_SESSION['ExclusionList']})" to every search query on the site,
or
2) Compile it always on-the-fly directly in each query by simply adding $query .= " AND userid NOT IN (SELECT userid FROM Excluded WHERE excludedBy = '$currentUserID')" to every search query on the site?
Method 1) seems more efficient since I’m only compiling the list once when the user logs in (or whenever they update it), rather than having to essentially compile the list every time any user runs a search query on the site.
What do you suggest and why?
Technically this would be called “caching” the values and it’s not a bad idea provided the lists don’t get too huge or your
$_SESSIONcould end up being kind of heavy-weight.Doing a sub-select can be very expensive, especially on large sets of records, so you’re usually better off caching it.