1

To Query everytime on the Database and use 'WHERE' operator?

SELECT * FROM tblProduct WHERE productID = @productID

OR

To Filter the Products List that are put into Cache?

DataTable dtProducts = new DataTable();
dtProducts = HttpContext.Current.Cache["CachedProductList"] as DataTable;

DataView dvProduct = new DataView();
dvProduct = dtProducts.DefaultView;
dvProduct.RowFilter = String.Format("[productID] = {0}", iProductID);

Please share your opinion. Thanks in advance.

2
  • 2
    How many rows in tblProduct? Regardless, it seems exceedingly bizarre to cache a DataTable; that type is far too rich. Caches should be made up of simple data structures.
    – Kirk Woll
    Sep 21, 2012 at 3:01
  • I presume in the future the datatable will be made up of 20000+ rows... So which do you think is better? Sep 21, 2012 at 3:07

3 Answers 3

2

Performance is very subjective to your data and how you use it. The method to know what works for sure is to benchmark.

Decide to cache only when your db performance does not meet the performance you require.

When you cache data, you add a lot of overhead in making sure it is up-to-date.

Sql server does not read from disk every time you fire a query, it caches results of frequent queries. Before you decide to cache, know the caching mechanisms used by your database. Using a stored procedure would allow you to cache the query plan too.

2

Caching data, especially through an in-memory mechanism like HttpContext.Current.Cache is (almost) always going to be faster than going back to the database. Going to the database requires establishing network connections, then the database has to do I/O, etc., whereas using the cache you just use objects in memory. That said, there are a number of things you have to take into account:

  • The ASP.NET runtime cache is not distributed. If you will be running this code on multiple nodes, you have to decide if you're okay with different nodes potentially having different version of the cached data.
  • Caches can be told to hold onto data for as long as you want them to, as short as just a few minutes and as long as forever. You have to take into consideration how long the data is going to remain unchanged when deciding how long to cache it. Product data probably doesn't change more often than once a day, so it's a very viable candidate for caching.
  • Be aware though that the cache time limits you set are not absolutes; objects can be evicted from the cache because of memory limits or when a process/app pool recycles.
  • As pointed out above, DataTable is not a good object to cache; it's very bulky and expensive to serialize. A list of custom classes is a much better choice from a performance standpoint.

I would say as a general rule of thumb, if you need a set of data more frequently than a few times an hour and it changes less frequently than every few hours, it would be better to pull the list from the database, cache it for a reasonable amount of time, and retrieve it by a filter in code. But that's a general rule; this is the kind of thing that's worth experimenting with in your particular environment.

200,000 objects is a lot of data to put into a cache, but it's also a lot of work for the database if you have to retrieve it frequently. Perhaps there's some subset of it that would be better to cache, and a different, less frequently used subset that could be retrieved every time it's needed. As I said, experiment!

0

I would prefer the first method. Having 20000 rows in cache does not sound good to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.