Asp. The performance optimization problem in net design

Source: Internet
Author: User
Tags eval join sql injection first row
Asp.net| Design | problem | performance | optimization

I. Selection of Sqldataread and datasets

Sqldataread Benefits: Read data very quickly. If you do not need to do a lot of processing for the returned data, it is recommended to use SqlDataReader, which has a much better performance than Datset. Disadvantage: Do not close the connection to the database until the data is read

(SqlDataReader reading data is fast forward.) The SqlDataReader class provides a way to read a data-only stream retrieved from a SQL Server database. It uses SQL Server's native network data transfer format to read data directly from a database connection. DataReader need to be explicit close in time. Timely release of the connection to the data. )

The dataset reads the data out and is slow to exist in memory. Disadvantage: high memory footprint. If you need to do a lot of processing of the returned data with a dataset better can reduce the connection to the database operation. Benefits: Connect to the database only once

* In general, read a large amount of data, do not do a lot of processing of the return data with SqlDataReader. It is more appropriate to datset the return data. The choice of SqlDataReader and dataset depends on the implementation of the program function.

Ii. ExecuteNonQuery and ExecuteScalar

Updates to the data do not need to return the result set, and it is recommended that you use ExecuteNonQuery. Network data transfer can be skipped because the result set is not returned. It simply returns the number of rows affected. ExecuteNonQuery performance is less expensive if you only need to update the data.

ExecuteScalar it returns only the first column of the first row in the result set. Use the ExecuteScalar method to retrieve a single value, such as an ID number, from the database. This operation requires less code than the action required to generate a single value by using the ExecuteReader method.

* Only update data with ExecuteNonQuery. Single value query using ExecuteScalar data binding selection

Third, the data binding DataBinder

The general binding method <%# DataBinder.Eval (Container.DataItem, "field name")%> with DataBinder.Eval binding does not concern the data source (Dataread or DataSet). You don't have to care about the type of data. Eval converts this data object to a string. A lot of work was done on the bottom bound, using reflective performance. It is easy to use, but it affects the performance of the data. Take a look at the <%# DataBinder.Eval (Container.DataItem, field name)%>. When the dataset is bound, the DataItem is actually a datarowview (if the binding is a data reader (Dataread) it is a idatarecord. Therefore, the direct conversion to DataRowView will give a great boost to performance.

<%# CType (Container.dataitem,datarowview). Row (field name)%>

* The binding of data is recommended to use <%# CType (Container.dataitem,datarowview). Row (field name)%>. The data can be increased hundreds of times times faster when the volume is large. Note 2 when using: 1. Add <%@ Import namespace= "System.Data"%>.2 on the page. Note the case of the field name (to be particularly noted). If inconsistent with the query, in some cases it can lead to slower than <%# DataBinder.Eval (Container.DataItem, field name)%>. If you want to further improve the speed, you can use <%# CType (Container.dataitem,datarowview). Row (0)%> method. But its readability is not high.

The above is the writing of vb.net. In C #: <@% ((DataRowView) container.dataitem) [Field name]%>

The easiest way to view the state of each execution of a page: The Trace property of its page is true to see the details.

First, use stored procedures:

1. Performance aspects: Stored procedures provide advanced features that are not available in many standard SQL languages. Its ability to pass parameters and execute logical expressions helps application designers deal with complex tasks. In addition, stored procedures are stored on the local server, reducing the network transmission bandwidth and execution time required to perform the process. (The stored procedure has precompiled the SQL statement so that it executes much faster than executing the SQL statement in the program)

2, the structure of the program: from the scalability of the program, the use of stored procedures to the program after the changes brought convenience. For example, the structure of the database changed, just modify the corresponding storage structure, and the call part of the program can be. This part does not belong to the scope of this article, which belongs to the design of program structure. So do not unfold here.

3. Program security: Use stored procedures to avoid SQL injection attacks.

II. Optimization of query statements (for SQL server2000)

Many people write SQL statements for purposes only, regardless of the execution efficiency of SQL statements. Here I only provide a way to optimize the order of the Tables (SQL statement optimization and principles will be in my SQL server2000 learning notes in the discussion)

Efficiency of SQL statement execution the SQL server2000 Query Analyzer can be used to view the execution of the statement.

Optimize table order: In general, SQL Server automatically optimizes the connection to a table. For example: Select Name,no from a join B in a. id=b.id join C on c.id=a.id where Name= ' Wang '

Although table A is listed first in from, then B, and finally C. However, SQL Server may first use the C table. Its selection principle is to reduce the total amount of data found in other tables relative to the query by restricting it to a single line or a few rows. In most cases, SQL Server makes the best choice, but if you find that a complex join query is slower than expected, you can use the SET FORCEPLAN statement to force SQL Server to use the table in the order in which it appears. As the previous example adds: Set Forceplan On.......set Forceplan the order in which the off table is executed will be executed in the order you write. View 2 execution efficiencies in Query Analyzer to select the connection order of the tables.

* Use SET Forceplan to select Table Join order

Third, the optimization of the page (. aspx)

Mainly for several page properties

1, EnableViewState (view state of the page). If no special requirements are set to false. With ViewState, each object must be serialized into the ViewState and then deserialized by a postback, so there is no cost to using ViewState. Minimize the use of objects and, if possible, minimize the number of objects placed in ViewState. The following situation can basically disable ViewState:

(1) Page control (. ascx)
(2) The page is not passed back to itself.
(3) You do not need to handle the control's events.
(4) The control has no dynamic or data-bound property value (or is handled in code for each postpack)

A single page or every page disables ViewState as follows: Single page: <%@ page enableviewstate= "False"%> per page: Web.config in <pages Enableviewstate= "false"/> EnableSessionState Keep the default value (it will not occupy resources if the page uses sessionstate). enableViewStateMac Keep the default value if there is no security specific requirement.

2, pagelayout. Page layout model. It is recommended that you use FlowLayout (elements are added without an absolute positioning attribute). Gridlayout (absolute positioning attribute), because of absolute positioning, will produce more code than FlowLayout, mainly the positioning information of the control.

3, when the project release, remember to release the debug state of the page.

4, the optimization of HTML language. My advice is to master Html/javascript, use less vs.net2003 automatically produced code, it will automatically generate some useless HTML code.

5, smart navigation set to True can make the user significantly improve the performance of the feeling. Enabling this property has little impact on the client and server side. It can be smart to rinse new needs to rinse new parts that need to be shabu-shabu.

Iv. Selection of controls:

The selection of HTML controls and server controls. The convenience of server controls and the implementation of functionality are unmatched by HTML controls. But it is achieved at the expense of server-side resources. My personal recommendation: If the HTML control does not reach the functionality that is to be implemented, and some scripting language (such as javascrpt/vbscript) can not achieve the combination. The server control is selected. When you select a server control, you also try to optimize its controls, such as canceling some page state (see the control Optimization)

Server control selection: mainly for several commonly used data controls to explain:

DataGrid: With the most powerful data display control, built-in data modification, deletion, add, paging and many other practical functions. If you only need to show the data, try not to select the DataGrid (it stores the data in ViewState). Also, do not use the paging feature, Microsoft has done a lot of work at the bottom of the automatic paging, although it is convenient to use, but the performance overhead.

DataList: A lot less than the DataGrid function. But the customization is much stronger. The unique multiline data show that it brings us a lot of convenience. The function that the DataGrid can achieve, it basically can achieve. So it is recommended to use it.

Repeater: Features are minimal, but customization is very strong. If you only need to show the data, use the recommended. The server's performance is minimized because of the reduced functionality. So if it's on the data, I basically choose Repeater and DataList the final DataGrid.

* Choose HTML controls as much as possible. The functionality that can be implemented on the client side is implemented on the client side (mastering JavaScript) to reduce server pressure. Data Control Selection Order: Repeater, DataList, DataGrid

Five, server control optimization:

1, Viewstate

The viewstate of the control is basically consistent with the viewstate of the page. Used to save some of the state of a control. The processing principle is the same as the viewstate of the processing page. Interested in the DataGrid binding data to test the amount of data saved under ViewState, the data it holds is basically the same as the amount of data displayed in the DataGrid.

2, Ispostpack

Default false. Set to True when you need to generate an event.

Control, primarily to see how familiar you are with this control. The more you understand the principle of the internal workings of a control, the better you can optimize it.

Performance optimization is 32 words, I write is only the tip of the iceberg, performance optimization is to rely on the accumulation of experience and the process of the operation of the principle of continuous recognition.



Related Article

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.