i have an orders table that has a userID column
i have a user table that has id, name,
i would like to have a database trigger that shows the insert, update or delete by name.
so i wind up having to do this join between these two tables on every single db trigger. I would think it would be better if i can one query upfront to map users to Ids and then reuse that "lookup " on my triggers . . is this possible?
DECLARE @oldId int DECLARE @newId int DECLARE @oldName VARCHAR(100) DECLARE @newName VARCHAR(100) SELECT @oldId = (SELECT user_id FROM Deleted) SELECT @newId = (SELECT user_id FROM Inserted) SELECT @oldName = (SELECT name FROM users where id = @oldId) SELECT @newName = (SELECT name FROM users where id = @newId) INSERT INTO History(id, . . .Good news, you are already are using a cache! Your SELECT name FROM users WHERE id = @id is going to fetch the name for the buffer pool cached pages. Believe you me, you won't be able to construct a better tuned, higher scale and faster cache than that.
Result caching may make sense in the client, where one can avoid the roundtrip to the database altogether. Or it may be valuable to cache some complex and long running query result. But inside a stored proc/trigger there is absolutely no value in caching a simple index lookup result.