I created custom database caching and it works very well. Here is the relevant SQL code for anyone interested
create table dbo.Cache ( CacheKey binary(20), CacheDate datetime, CacheTable varchar(200) )
The CacheKey is created by concatenating the name of the stored proc and parameters.
The cache results table is built on the output of the stored proc, named "Cache_" + stored proc name
This is the code added to the existing long running stored procs.
declare @cacheKey binary(20)
set @cacheKey = HashBytes( 'SHA1', Object_Name(@@ProcID) + isnull( @ContatenatedParms, 'Null' ))
if exists ( select * from dbo.Cache where CacheKey = @cacheKey )
begin
select Field1, Field2, Field3
from dbo.Cache_GetAccountTrailingReturnsBI
where CacheKey = @cacheKey
return
end
-- Do long running stuff, load to @results in this example
-- Load cache table
insert into dbo.Cache ( CacheKey, CacheDate, CacheTable )
values ( @cacheKey, getdate(), 'dbo.Cache_GetAccountTrailingReturnsBI' )
-- Load cache results
insert into dbo.Cache_GetAccountTrailingReturnsBI
( CacheKey, Field1, Field2, Field3 )
select @cacheKey, Field1, Field2, Field3
from @results
-- Return results
select Field1, Field2, Field3
from dbo.Cache_GetAccountTrailingReturnsBI
where CacheKey = @cacheKey
I have a job that truncates all cache tables every night, because my data is daily.
If anyone knows how I can paste sql code where it keeps the colors and indentation I'll buy you a beer. :)