Search code examples
c#performancewindows-servicessingletonfile-processing

Query from database or from memory? Which is faster?


I am trying to improve the performance of a Windows Service, developed in C# and .NET 2.0, that processes a great amount of files. I want to process more files per second.

In its process, for each file, the service does a database query to retrieve some parameters of the system.

Those parameters change annually, and I am thinking that I would gain some performance, if a loaded those parameters as a singleton and refreshed this singleton periodically. Instead of make a database query for each file being processed, I would get the parameters from memory.

To complete the scenario : I am using Windows Server 2008 R2 64 Bits, SQL Server 2008 is the database, C# and .NET 2.0 as already mentioned.

I am right in my approach? What would you do?

Thanks!


Solution

  • Those parameters change anually

    Yes, do cache them in memory. Especially if they are large or complex.

    You should take care to invalidate them at the right time once a year, depending how accurate that has to be.

    Simply caching them for an hour or even for a few minutes might be a good compromise.