I want to create a "view" to eliminate the same three-line sub-query from about 90 queries in an app I'm working on.
The problem is the sub-query contains a condition based on a variable.
SELECT * FROM items WHERE id NOT IN (
SELECT item_id FROM excluded_items WHERE user_id = 123
);
If it wasn't variable, I could simply make a view and be done with it.
I'm not sure what to do in this case though. Adopting the same mentality behind a view I'm tempted to make a stored procedure that returns the desired record set, so that it could be called something like this:
SELECT * FROM user_items(123);
Now I have a single place to update this item exclusion and any further conditions, however I'm not sure how indexing is affected if I want to join the results of that SP against other tables?
So is this good/bad practice? Is there another way to do it, or should I just suck it up and keep replicating this sub-query?