Tuesday, September 8, 2009

Optimizations solve the problems!!! (continue)

After running ANTS profiler, we found the method that causes the problem, this method simply requested by users every one minute to get some data, the implementation of this method was very bad.
The following is the pseudo-code for the method

GetData(startTime,endTime)
{
//Is Data in the cache?
if exist_cache
GetFromCache()
else
//select from database
//update cache
}
In the previous code we found two problems; the first one is that the mechanism of updating the cache was not done regularly, so a lot of users requests go to database, that causes a performance problem as the number of users increases.
The second problem is the way of getting data from the cache itself, the cache was DataSet that contains single table, getting data was done by calling DataTable.Select method that consumes a lot of time.
We solved these problems simply by updating the cache continuously in a separate background thread so whatever the number of users requests you have only one database access, and the second problem was solved by distributing data over tables so we can get data directly from tables without using DataTable.Select method.