I have a large DataTable cached in my web app that is the result of a complex query that returns a large data set. Whilst this data table is cached the query that runs to “refresh” this cache still takes a long time, largely due to the sheer amount of data being returned.
In order to speed this up I am considering implementing a timestamp type approach to my tables in order to limit my query to only return rows which have changed.
I then intend to merge this smaller dataset with my cached datatable.
Has anyone done anything similar to this, or is there anything out there that handles this already?
I feel this could be a re-inventing the wheel situation if I dive straight in.
Personally, I’ve used the timestamp approach before and that does work well – it does make the caching more efficient by only retrieving the data that has changed since the last read.
Alternatively, I’d suggest the SqlCacheDependency class which takes care of keeping the cache up to date for you. I can’t comment on any real-world pros + cons of this, or of performance comparison vs. timestamp approach as I haven’t used it myself.
There’s another useful article on SqlCacheDependency here
Update:
Yes, I don’t think it will actually refresh the data. It sounds like you’d have to do that yourself.
From the 2nd link:
There’s also SQL 2005 specific implementation notes in the 2nd link:
I personally think I’d go for the timestamp approach (that’s what I’ve done before) as I can’t see on the face of it that SqlCacheDependency would give any performance benefits – I think it would be less performant (just easier to implement). One day, I’ll get round to actually trying out SqlCacheDependency to do a proper performance analysis 🙂
Update 2:
Regarding the merging of new data into the existing datatable, I think the Merge method of the datatable is what you want.
You just need to ensure you define the column(s) on the datatable that are the primary key.