I'm working on an interactive contact search page (contacts are returned via ajax as you type or select criteria). I want this page to be very responsive.
There is a complex set of rules to determine which contact records a given contact can see; these rules are rolled up into a user-defined function, DirectoryContactsByContact(@ContactID)
. I've optimized this function considerably but it's still a little expensive (1-2 seconds to execute), so to improve performance I'm thinking about something like this:
- When the page loads, cache DirectoryContactsByContact for this user as a SQL table, e.g.
cache_DirectoryContactsByContact_1
- Perform the search against the cached table (checking each time to make sure it exists)
- After a little while (say 30 minutes) kill the cache
It's OK if the data gets stale during this period, so I'm not concerned with invalidation.
Temporary tables don't last between requests, so it seems like I'd need to create the cache table as a permanent table; but then I'd need to be responsible for cleaning up old caches myself, which looks non-trivial at first glance.
Are there any mechanisms within SQL Server that would make this easier? Any advice on alternative approaches?