I have a SQL2008 table of automotive makes, models, and year. To display a list of years to the user I need to know the lowest and the highest years in the table, like 1919 – 2012.
I use a LINQ query as follows:
return (from car in context.Cars select car.Year).Min();
…which turns into this SQL query:
SELECT MIN([t0].[Year]) AS [value] FROM [dbo].[Cars] AS [t0]
I know (and you know) that the min will rarely if ever change and the max will change, at most, once per year. But since they are indeed data-dependant, they’re not suitable as constants. Since they -could- change during runtime, I don’t want to make them static.
I’m assuming that SQL will cache that min and max, and as long as the underlying tables are not modified, it’s smart enough to return me cached information. Until and unless it ever became a performance bottleneck, it’s more of a curiosity, but to what extent can I rely on SQL (in this case, SQL2008 R2) caching query results as long as the tables do not change?
Well instead of even hitting the database why not cache it on the application level? You can use
SqlCacheDependencySQL Cache DependencyInvalidate the cache if the table changes otherwise don’t even hit it.