Optimized performance of often used in ASP.NET

xiaoxiao2021-03-13  184

1. Database Access Performance Optimization

Database connection and close

Accessing database resources requires a connection to open and turn off a few operations. These processes need to exchange information multiple times with databases to compare server resources through authentication. ASP.NET provides a connection pool to improve the impact of the database to the performance. The system places the user's database connection in the connection pool, take it out, and the connection is retracted, waiting for the next connection request.

The size of the connection pool is limited. If the connection is required to create a connection after the connecting pool is maximized, it will inevitably affect performance. Therefore, after the establishment of the database connection, only the connection is turned on when you truly need to operate. It is immediately turned off again, thereby minimizing the time of the database connection to open, avoiding the situation that exceeds the connection limit.

Use stored procedures

The stored procedure is a set of pre-compiled SQL statements stored on the server, similar to batch files in the DOS system. The stored procedure has functions that are immediately accessed immediately, and the information processing is extremely rapid. Using the stored procedure can avoid multiple compilation of the command, after executing the execution plan resides in the cache, just call the binary code in the cache when needed.

In addition, the stored procedure runs on the server side, independent of the ASP.NET program, which is easy to modify, the most important thing is that it can reduce the transmission of the database operation statement in the network.

Optimize query statements

ADO connection consumption in ASP.NET is quite large, the longer the SQL statement runs, the longer the time occupying system resources. Therefore, try to use an optimized SQL statement to reduce execution time. For example, not in the query statement contains a child query statement, take advantage of indexing, etc.

2. String Operation Performance Optimization

TOSTRING method for use value

When connecting a string, use the " " number directly to the string. Although this method is simple, the correct result can be obtained, but due to the different data types, the number needs to be converted into the reference type by packing operation to be added to the string. However, the packing operation has a large effect on performance, because when such processing is performed, a new object will be allocated in the hosted heap, and the original value is copied into the newly created object.

Use the TOSTRING method of the value type to avoid packing operations, thereby improving application performance.

Use StringBuilder class

The String class object is an uncharged. Re-assay for String objects is inherently recreated a String object and give the new value to the object, how to improve performance is not very significant.

When processing a string, it is best to use the StringBuilder class, and its .NET namespace is System.Text. This class does not create a new object, but through the Append, Remove, Insert, etc., the string is directly operated, and the results result is returned by the TSTRING method.

Its definitions and operation statements are as follows:

Int Num;

System.Text.StringBuilder str = new system.text.stringbuilder (); // Create a string

Str.Append (Num.toString ()); // Add Numerical Num

Response.write (str.tostring); // Display operation results

3. Optimize the web server computer and the specific application of the specific application to meet your specific needs.

By default, the ASP.NET configuration is set to enable the most extensive features and to adapt to the most common solutions. Therefore, application developers can optimize and change certain configurations in the functions used by the application to improve the performance of the application. The following list is some options you should consider.

Enable authentication only for applications. By default, the authentication mode is Windows, or integrated NTLM. In most cases, for applications that require authentication, it is best to disable authentication in a Machine.config file and enable authentication in the web.config file. Configure the application based on appropriate requests and response coding settings. ASP.NET default encoding format is UTF-8. If your application is strictly ASCII, configure an application to use ASCII to get a slight performance improvement.

Consider disabling AutoEventwireup for applications. Set the AutoeventWireUp property to false in the Machine.config file, meaning that the page does not match the method name and the events (for example, Page_Load). If the page developer wants to use these events, you need to rewrite these methods in the base class (for example, to override page.onLoad, instead of using the Page_Load method). If you disable autoeventwireup, the page will receive a slight performance boost by leaving the event connection to the page author instead of it.

Remove the unused module from the request processing line. By default, all functions of the node in the machine computer's Machine.config file are reserved as activation. Depending on the feature used by the application, you can remove unused modules from the request line to get a slight performance increase. Check each module and its features and customize it according to your needs.

For example, if you don't use a session status and output cache in your application, you can remove them from the list so that you don't have to perform the entry and leaving code of each module when you do not perform other meaningful processes. .

4. Be sure to disable debug mode

Always remember to disable debug mode before deploying production applications or performs any performance measurements. If debug mode is enabled, the performance of the application may be very affected.

5. For applications that are widely rely on external resources, consider enabling network gardening on multiprocessor computers.

The ASP.NET process model helps enable scaling on multiprocessor computers, distribute work to multiple processes (one each CPU), and each process sets the processor relationship to its CPU. This technology is called network gardening. If the application uses a slower database server or a COM object with external dependencies (here just mentioned two possibilities), it is beneficial to enable network gardeners for your application. However, before deciding to enable network gardening, you should test the implementation of the application in the online garden.

6. As long as it is possible, the cache data and page output

ASP.NET provides some simple mechanisms that cache these page output or data without requiring dynamic calculation page output or data for each page. In addition, the performance of these pages can be optimized by designing the cached pages and data requests to be cached (especially the area where the volume is expected to be larger in the site). Compared with any Web Forms for .NET Framework, properly use the cache can better improve the performance of the site, sometimes this increase is superior.

There are two points to pay attention to using the ASP.NET cache mechanism. First, don't cache too much. Cache is overhead, especially in memory use. Do not cache items that are easy to recalculate and rarely used. Second, the validity period given to the cache is not too short. Items that have expired will result in unnecessary turnover in the cache and often lead to more code clearance and garbage collection. If you are concerned about this issue, monitor the Cache Total Turnover Rate Performance Counter associated with the ASP.NET Applications Performance Object. High turnover rates may explain that there is a problem, especially when the item is removed before expire. This is also called memory pressure. 7. Select the data viewing mechanism for your page or application

There is an important trade-off based on how you choose to display data in a web form page, and there is an important weighing between facilities and performance. For example, the DataGrid Web server control may be a convenient and easy way to display data, but its overhead is often the largest. In some simple situations, you may be effective by generating appropriate HTML yourself, but custom and browser orientation will quickly offset the additional efficacy. Repeater Web Server Control is a compromise for convenience and performance. It is efficient, customizable and programmable.

8. Use the SqlDataReader class to quickly enter the data cursor

The SqlDataReader class provides a way to read only data streams from the SQL Server database. If you have a case where you have allowed you to use it when you create an ASP.NET application, the SqlDataReader class provides higher performance than the DataSet class. The situation is thus because SqlDataReader reads data directly from the database connection using SQL Server native network data transmission format. In addition, the SQLDataReader class implements the IEnumerable interface, which also allows you to bind the data to the server control. For more information, see the SqlDataReader class. For information on how ASP.NET access data, see Accessing Data over ASP.NET.

9. Use the SQL Server stored procedure for data access

In all data access methods provided by .NET Framework, SQL Server-based data access is a recommended selection of high performance and scales web applications. Additional performance improvements can be obtained by using the compiled stored procedure instead of a special query by using the hosting SQL Server provider.

10. Avoid single-threaded units (STA) COM components

By default, ASP.NET does not allow any STA COM components to run within the page. To run them, you must include the aspcompat = true property in the .aspx file in the @ page instruction. This will switch to the STA thread pool to the STA thread pool, and HTTPCONTEXT and other built-in objects can be used for COM objects. The former is also a performance optimization because it avoids any call to the multi-threaded unit (MTA) to the STA thread.

With STA COM components, it may be greatly harmful and should be avoided as much as possible. If you must use the STA COM components, as in any InteroP scheme, you should perform a large number of calls during execution and send as much information as possible during each call. In addition, be careful not to create any STA COM components during the constructing page. For example, in the following code, the MyStacomponent created by a thread is instantiated when the page is constructed, and the thread is not a STA thread that will run the page. This may have adverse effects on performance because the page must be constructed to complete the encapsulation processing between the MTA and STA threads.

<% @ Page language = "vb" aspcompat = "true"%>