Register | Login
Friday, August 29, 2008

Sections
  
About Us
  
Hosting Provided by Server Intellect
Partners
Downloads
  
 WWWCoder.com Resource Directory

Optimizing Your Asp.Net Pages for Faster Loading and Better Performance.
4/27/2006 9:12:59 AM

In this article the author discusses some options for speeding up the loading of your ASP.NET pages. Some tips include using caching, control use, and data handling techniques.

If you read the internet and all of the websites dedicated to Asp.Net you will inevitably read about the wonders of the DataGrid, DataList, and Repeater controls. While each of these has its place, if you are only displaying data there is a much faster and more efficient means to do so.

Let's say you have a page that displays articles based on a query string. Take my article pages for instance. Each article is stored in a database and displayed on the page based on the unique id of the article as stored in the database.

A normal asp page execution procedure goes something like this. The code queries the database based on the Article I.D. and then brings back that information to the page where you display it in the fashion that you would like. This is a fairly straight forward approach with asp and is done all the time.

So how do we speed up our asp.net pages?

Number 1: Use Asp.Net Caching!

This is a no-brainer, and I won't go into the brilliance or details of asp.net caching here because at the time of this writing Google has 2,780,000 articles on the topic. Basically instead of querying the database each time the page is loaded you only query the database once and load that result into the system cache. Subsequent calls to load the page retrieve the data from the cache as opposed to the database which gives you an instant and considerable performance boost. You can then set the cache for how long the cache should store the information as well as many other features. If you are not using the cache, you should be whenever possible!

Number 2: If possible, do NOT use the standard Asp.Net controls.

That's right. The standard asp.net controls are designed for rapid development and not page performance. They allow you to design pages that grab and display data very quickly but their actual performance suffers because of the extra overhead which is there for ease and speed of development time and not page execution speed.

Instead, create either a User Control or even better yet a Web Custom Control which is by far the fastest performance wise and really quite easy to create and use.

Number 3: Use an SqlDataReader or even better yet use a set based command for Sql Server data retrieval and simply execute that one command against the database.

An asp.net SqlDataReader is a fast forward only datareader that closes the connection after it reads the last set of results. Now for my article pages we are only returning 1 particular result. In this case we would opt for the set based command. If you had more than 1 result returned, in your table of contents for instance, you would use the SqlDataReader because you are returning multiple sets of results.

Set based commands are stored procedures that bring back data through parameters as opposed to a result set which then in turn needs to be looped through to obtain your data. So instead of writing your stored procedure like the following which brings back 1 result set:

Select Title, Body, Author
From Articles
Where ArtID = 215

We can write it using a set based command like this.

Create Procedure mysp_GetArticle

@Title varchar(200) Output,
@Body varchar(8000) Output,
@Author varchar(500) Output

As

Select @Title = Title, @Body = Body, @Author = Author
From Articles
Where ArtID = 215

GO

The above query will return only the three parameters called for and not a result or record set so you don't have to then walk through the returned record set that has only 1 result in it anyway. This second little process of work decreases your performance so you should avoid it whenever possible. Combine this technique with the asp.net cache.

Number 4: Use Classes and ArrayLists as opposed to returning an SqlDataReader.

Create a class and then if there are more than one set of results store those results into individual instantiations of that class. Finally store each of those classes into an ArrayList. You can then store only that ArrayList into the asp.net cache. So instead of getting the results back from a SqlDataReader when loading your page you get them from the ArrayList which is stored in the cache. Nice huh?

Finally... you want to incorporate all of these techniques into your final results which would be performed in the following manner and sequence.

On the first time the page loads, query the database and return all of your data storing it into individual classes. Then store each of those classes into an ArrayList. If you only have one single result you may store only the class into the cache. Then take your ArrayList and store it into the cache.

Next create a Web Custom Control and pass the cached ArrayList to the custom control and loop out your data using the HtmlTextWriter which is very fast. Remember each subsequent call to load the page will be called from the cache which stores your ArraList of classes or your single class.

Certainly it takes a significant amount of additional coding to do it in this fashion, especially when you take proper error handling into consideration, but if you follow this approach your pages will be screeching fast, you will immediately notice the difference, and your asp.net pages will execute in the proper sequence - Data handling in the Page_Load function and the html display in the Page_Render function.

Further, you will be glad you did and so will your visitors.

Happy Programming!

About the Author:
John Belthoff is an avid web developer who who writes about Asp.Net in his spare time. He owns a Windows Asp.Net, Asp Web Hosting Company where you can contact him about hosting your website/blog or just to learn more.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 6/27/2008 6:18:10 PM by Anonymous
Comments: doesn't understand asp.net fundamentals so is working around them
Left on 10/2/2007 6:04:34 PM by Anonymous
Comments: I had the same problem. The reason is that the application domain times out every 20 mins if there is no activity, the first request after the timeout can force a recompile and reload of cache. Changing some settings in the machine.config file will solve the problem; unfortunately for me my hosting provider would not allow me to make this change. I found this utility to be useful.

http://www.spikesolutions.net/ViewSolution.aspx?ID=c2b7edc0-5de1-4064-a432-05f6eded3b82

Essentially it "Pings" my home page every few mins so the application domain does not time out. The utility can also be configured to ping more than one page so that auxiliary pages are fast too.

Left on 5/2/2007 12:39:51 PM by Anonymous
Comments: This is exactly what we needed. We are a start-up running sql server and IIS on the same box and our queries only return 1 row. We needed ideas to improve performance as the user base grows and until we can upgrade to multiple servers where we will separate the DB and IIS. Even after that, the set based approach and selective caching eorks for us. Thanks much, Tom
Left on 9/8/2006 11:41:04 AM by Anonymous
Comments:
No ratings available.
Left on 7/4/2006 2:42:27 AM by Anonymous
Comments: Some of the relevant points here are merely stating the obvious! The rest is what I can only describe as extremely poor design by a by one who has not fully embraced OO methodologies, not to mention some of the over-simplistic solutions. Nothing short of a narrow-minded archaic method of code and design.
No ratings available.
Left on 6/8/2006 7:15:32 AM by Anonymous
Comments: I don't understand what th asp.net 2003 with c# mas a lots of time to load only in a few pages 
Left on 5/25/2006 8:08:29 AM by Anonymous
Comments: This is what i m looking for!!!
Left on 5/19/2006 12:49:17 AM by Anonymous
Comments: To respond to last poster: When you have 150 users using your app, and each page has to perform 150 queries to load everything (dynamic controls, xml data from DB to fill them) and file IO (for other XML data etc) .. you will see your performance fall into a pit. And there's a ton of stuff to add to this article in regards to performance tweaks.
Left on 5/15/2006 1:13:51 PM by Anonymous
Comments: I'd like to know what the heck these people are doing on a page to require such drastic optimization measures. I've worked on a lot of web apps and virtually never have to do any of this stuff. Dang, I virtually never have to optimize. Why? Because the pages are fast enough already. "Well they could be faster", you say. Fine. Yes, perhaps they could-- but, you miss my point. What I am saying is, in short, the general rule of thumb-- "try not to make your page do too much". What is "too much"? Well, is your page slow? Well, then it is probably trying to do too much. Apply Divide-And-Conquer to functionality and segment the set available to the enduser. No, this does NOT mean sacrificity functionality-- it is simply recognizing and leveraging the hard FACT that a given enduser can only look at and process a small amount of data/ information/ functionality. So, why pack the page when the user cannot process it anyway. It comes down to common sense. That said, yes, the article is OK-- but, I say if the page is designed properly, most of this is not necessary. (And, yes, I am talking about enterprise applications.)
Left on 5/9/2006 9:59:33 AM by Anonymous
Comments: Overly simplistic coverage and some bad advice overall.  There are much easier ways to reap the benefits of caching without having to create your own controls; the built-in controls can perform just fine.  And ArrayList's are almost always bad.  Use strongly-typed collections or arrays or generics if you're in 2.0.
Left on 4/28/2006 2:44:11 PM by Anonymous
Comments: Thank you for this beautiful article. If you ever would like to add more to this artile please forward me a copy of this at waheez@gmail.com
Left on 4/28/2006 11:40:31 AM by Anonymous
Comments: Question, say I have a collection of articles, and the first time I load the page I store each article in it's own class then store those classes into an arraylist.  That fine, however, next time the page loads, say I get the article ID from the querystring, at that point I'm using an array list.  What would be the most efficent way to search the arraylist for the article I need?
No ratings available.
Left on 4/28/2006 9:43:19 AM by Anonymous
Comments: It's great and all how you try to say "this is faster than that", but it would sure be nice to have some reasons "why" it is faster...  like your claim that a customer web control is less overhead than a built in web control..  why is this?
Left on 4/28/2006 9:04:23 AM by Anonymous
Comments: Overkill
Left on 4/27/2006 10:24:32 PM by Anonymous
Comments: Comments from the following blog: -:[web caboodle]:-, located at: http://blog.dannyboyd.com/archive/0001/01/01/25589.aspx
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970