Sunday, December 30, 2007

Enhance Data View Performance using Asynchronous Method

Currently I find a way how to enhance a performance to view 10.000 records which taken from Web service layer.

First of all, I can not using any caching because this data will change very often.
Every time the user change something, I need to refresh the data.

I can not use Sql Dependency because all the data are encapsulate on service layer.
I can not perform paging on service layer because it is not provided to me.


Finally, the only way is using Asynchronous Method.

The idea is to display the first page of the records and start retrieving others data asynchronously.

The next time the user want to perform, sorting, filtering or paging, It will force to finish all retreiving data.

This algorithm will force using user thinking time in retreiving huge records rather than forcing user to wait retrieving huge records every time even they just want to see the first page records.

Here is the Algorithm:
=================================
If ( ! IsPostback)
{
* Clear Data set from session
* Retrieve 1st page only and bind to data grid with next/previous paging style
* Create a delegate to retrieve the rest of data
* Start asynchronous call with BeginInvoke.
* Store that delegate and IAsynResult into session.
}
else
{ //( Postback) and user try to bind a data (Sorting, Paging, Filtering)
If (There still no data set in Session)
{
* Take delegate and IAsynResult
* Force delegate to finish by using EndInvoke(IAsynResult)
* Store the data set into Session.
* Bind to data grid with default paging style.
}
else
{
* directly use data set from session and bind to data grid with default paging style.
}
}
===============================

Note:
You may optimize this algorithm by getting a number of record first.
If the number of records is less or equal then the number of first page records, just store that data into session and bind it to data grid with default paging style.
You don't need to perform asynchronous call for this.


The only weakness of this methodology is it will be more thread in your application.

No comments: