Performance Trimming Tips

Source: Internet
Author: User
Tags query client
Performance Tuning Tips


any programming model has a common performance flaw, asp.net is no exception. This section describes some of the ways you can avoid performance bottlenecks in your code.





1. Disable session state when not in use: Not all applications or pages require session state based on each user. If you do not need it, you can disable it completely. This can be easily achieved through the following page-level directives:


<%@ Page enablesessionstate= "false"%>





Note: If the page requires access to session variables but does not create or modify them, set the directive value to ReadOnly. Session state can also be disabled for XML Web service methods. See Use objects and internal objects in the XML Web Service section.








2. Choose a session-state provider carefully: ASP.net provides three different methods for storing session data for an application: in-process session state, Out-of-process session state as a Windows service, and out-of-process session state in the SQL database. Each method has its own advantages, but in-process session state is by far the fastest solution. If you store only a small amount of volatile data in session state, you should use an in-process provider. Out-of-process solutions are primarily used for Web garden and Web farm scenarios, or for situations where data cannot be lost when a server/process restarts.





3. Avoid excessive round trips to the server: the Web Forms page framework is one of the best features of ASP.net because it can significantly reduce the amount of code that is written to accomplish a task. Programmatic access to page elements using server controls and postback event handling models is undoubtedly the most time-saving feature. However, there are appropriate and inappropriate ways to use these features, and it is important to know when to use them properly.


applications often need to travel to and from the server only when retrieving data or storing data. Most data operations can be performed on the client between round trips. For example, you can usually validate form items on the client before the user submits the data. Typically, you should not commute to the server if you do not need to relay the information back to the server.





If you write your own server controls, consider having them render client code for the superior (support ECMAScript) browser. By adopting an "intelligent" control, you can significantly reduce the number of unnecessary clicks on your WEB server.








4. Use Page.IsPostBack to avoid extra work on round trips: If you are working with server control postbacks, you typically need to execute code the first time the page is requested, unlike the code used for round trips when the event is fired. If you check the Page.IsPostBack property, the code can execute conditionally, depending on whether there is an initial request to the page or a response to the server control event. This seems obvious, but you can actually ignore the check without changing the behavior of the page. For example:





<script language= "C #" runat= "Server" >





Public DataSet ds;


...





void Page_Load (Object sender, EventArgs e) {


//... set up a connection and command ...


if (! Page.IsPostBack) {


String query = "SELECT * from Authors where FirstName like '%justin% '";


Mycommand.fill (ds, "Authors");


Mydatagrid.databind ();


}


}





void Button_Click (Object sender, EventArgs e) {


String query = "SELECT * from Authors where FirstName like '%brad% '";


Mycommand.fill (ds, "Authors");


Mydatagrid.databind ();


}





</script>





<form runat= "Server" >


<asp:datagrid datasource= ' <%# ds. DefaultView%> ' runat= ' server '/><br>


<asp:button onclick= "Button_Click" runat= "Server"/>


</form>








<script language= "VB" runat= "Server" >





public DS as DataSet


...





Sub Page_Load (sender as Object, e as EventArgs)


' ... set up a connection and command ...


If Not (page.ispostback)


Dim query as String = "SELECT * from Authors where FirstName like '%justin% '"


Mycommand.fill (ds, "Authors")


Mydatagrid.databind ()


End If


End Sub





Sub Button_Click (sender as Object, e as EventArgs)


Dim query as String = "SELECT * from Authors where FirstName like '%brad% '"


Mycommand.fill (ds, "Authors")


Mydatagrid.databind ()


End Sub





</script>





<form runat= "Server" >


<asp:datagrid datasource= ' <%# ds. tables["Authors"]. DefaultView%> ' runat= ' server '/><br>


<asp:button onclick= "Button_Click" runat= "Server"/>


</form>








<script language= "JScript" runat= "Server" >





public var Ds:dataset;


...





function Page_Load (Sender:object, E:eventargs): void {


//... set up a connection and command ...


if (! Page.IsPostBack) {


var query:string = "Select * from Authors where FirstName like '%justin% '";


Mycommand.fill (ds, "Authors");


Mydatagrid.databind ();


}


}





function Button_Click (Sender:object, E:eventargs): void {


var query:string = "Select * from Authors where FirstName like '%brad% '";


Mycommand.fill (ds, "Authors");


Mydatagrid.databind ();


}





</script>





<form runat= "Server" >


<asp:datagrid datasource= ' <%# ds. DefaultView%> ' runat= ' server '/><br>


<asp:button onclick= "Button_Click" runat= "Server"/>


</form>







The
Page_Load event executes on all requests, so the page.ispostback is checked so that the first query is not executed when the Button_Click event postback is processed. Note that even without this check, the behavior of the page will not change, because the binding in the first query will be overthrown by the DataBind call in the event handler. Remember that this simple performance improvement is easy to ignore when writing pages.








3. Use server controls sparingly: Although server controls are easy to use, they are not always the best choice. In many cases, simple rendering or data-binding substitution can accomplish the same thing. For example:


<script language= "C #" runat= "Server" >





public String ImagePath;


void Page_Load (Object se







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.