Some time ago, due to the slow access speed of the Learning Network, I studied how to optimize it.
Asp.net
I checked a lot of information on the Internet and summarized the following experiences:
I,
SqlDataRead
And
Dataset
Select
Sqldataread
Advantage: Reading data is very fast. We recommend that you use
SqlDataReader
, Its performance is better
Datset
A lot better. Disadvantage: it is not available until the data is read.
Close
Connection to the database
(SqlDataReader
Reading data is fast forward.
SqlDataReader
Class provides
SQL Server
Database Retrieval Method. It uses
SQL Server
The Local Network Data Transmission Format for reading data directly from the database connection.
DataReader
Explicit
Close
. Data connections can be released in a timely manner.
)
Dataset
Read the data and cache it in the memory. Disadvantage: High memory usage. If a large amount of processing is required for the returned data
Dataset
It can reduce database connection operations. Advantage: Only one connection is required.
Close
Database Connection
Generally, a large amount of data is read.
,
Do not process the returned data in large quantities
SqlDataReader.
Massive processing of returned data
Datset
Suitable
.
Pair
SqlDataReader
And
Dataset
The selection depends on the implementation of program functions.
II,
ExecuteNonQuery
And
ExecuteScalar
No result set is required for data update. We recommend that you use
ExecuteNonQuery
. Because no result set is returned, network data transmission can be saved. It returns only the number of affected rows. If you only need to update data
ExecuteNonQuery
The performance overhead is relatively small.
ExecuteScalar
It returns only the first column of the First row in the result set. Use
ExecuteScalar
Method to retrieve a single value from a database (for example
Id
). And use
ExecuteReader
Method,
Compared to the operations required to generate a single value for the returned data, this operation requires less code.
You only need to update the data
ExecuteNonQuery
,
Query and use of a single value
ExecuteScalar
Iii. Data Binding
DataBinder
General Binding method
<% # DataBinder. Eval (Container. DataItem,
"
Field name
") %>
Use
DataBinder. eval
Binding does not concern about data sources
(Dataread
Or
Dataset)
. Don't worry about Data Types
Eval
Converts the data object to a string. It does a lot of work on the underlying binding and uses the reflection performance. This is because it is easy to use, but affects data performance.
Data Binding is recommended.
<% #
Ctype (Container. DataItem, DataRowView). Row ["
Field name
"]
%>
. When the data volume is large, the speed can be increased several times. Pay attention to the following two aspects during use:
1.
You need to add
<% @ Import
Namespace = "System. Data" %>.
2.
Note the case sensitivity of the field name ). If it is inconsistent with the query, in some cases
<% # DataBinder. Eval (Container. DataItem,
"
Field name
") %>
Slow down. If you want to further increase the speed, you can use
<% #
Ctype (Container. DataItem, DataRowView). Row (0) %>
. But its readability is not high.
4. Use stored procedures
Performance: stored procedures provide many standards
SQL
Advanced features not available in the language. The function of passing parameters and executing logical expressions helps Application designers process complex tasks. In addition, the stored procedure is stored on the local server, reducing the network transmission bandwidth and execution time required to execute the process. (The stored procedure has
SQL
The statement is pre-compiled, so the execution speed is faster than that in the program.
SQL
Statements are much faster)
Program structure: From the Perspective of program scalability, using stored procedures will facilitate subsequent modifications to the program. For example, if the database structure changes, you only need to modify the corresponding storage structure and the calling part of the program.
5. Use paging query for a large number of queries
If we have a large amount of data to query, but it is displayed on one page, we can use the paging query function, that is, to query as much data as needed, in this way, the speed can be optimized from at least two aspects:
1.
Less data is transmitted, which can reduce the bandwidth pressure.
2.
Because the query data is small, the query time of the database can be reduced.
VI,
EnableViewState (
View status of the page
)
. Set
False
.
It is mainly used to maintain the page
UI
Status,
Web
Is stateless,
ASP. NET
Pages are not in the status. They are instantiated, executed, rendered, and processed during every round-trip to the server. Therefore, the existence of pages consumes the performance of the server, of course, his existence can also reduce the amount of code I can use, because the status of many controls is maintained for us and can be completely disabled without maintenance.
Viewstate
.
As follows:
Single page:
<% @ Page EnableViewState = "False" %>
All pages:
Web. config
Medium
<Pages EnableViewState = "false"/>
VII,
Html
Control and Server Control Selection
The convenience and functional implementation of server controls are as follows:
Html
Control is incomparable. However, it is achieved at the sacrifice of server resources. My personal suggestion: If
Html
The control does not provide the functions to be implemented, and it can be used with some scripting languages (such
Javascrpt/vbscript
.
It mainly describes several common data controls:
DataGrid
: Comes with the most powerful data display control, built-in data modification, deletion, addition, paging and many other practical functions. If you only need to display data, try not to select
DataGrid
(It stores all data in
Viewstate
Medium)
,
Do not use the built-in paging function,
Microsoft
I have done a lot of work on the underlying layer of automatic paging. Although it is easy to use, the performance overhead is high.
DataList
: Ratio
DataGrid
A lot fewer features. However, the customization is much stronger. The unique multi-row data display brings us a lot of convenience.
DataGrid
It can be implemented basically, so we recommend that you use it.
Repeater
: The minimum number of features, but the customization is very strong. We recommend that you only display data. Due to the reduction of many functions, the performance of the server is minimized. Therefore, for data display, I usually choose
Repeater
Then
DataList
Last
DataGrid
Select as much as possible
Html
Controls, functions that can be implemented on the client are implemented on the client.
(
Proficient
Javascript)
To reduce the pressure on the server. Data Control selection order:
Repeater
,
DataList
,
DataGrid
.
8,
String
And
StringBuilder
Comparison
String
Class objects cannot be changed.
String
In essence, the re-assignment of an object is re-created.
String
Object and assign a new value to the object.
ToString
Performance improvement is not significant.
It is best to use
StringBuilder
Class, its
. NET
The namespace is
System. Text
. This class does not create a new object, but uses
Append
,
Remove
,
Insert
And other methods to directly operate the string
ToString
Method returns the operation result.
The above are only my personal summaries and excerpts from the Internet. In fact
Asp.net
The optimization is far more than that.
. Net
Is very familiar with the running mechanism and object usage. Of course, there is also a very important point, that is, the performance of the program. The results of several statements of code can be written into a large segment, and the results affect the performance of the entire page, optimization in this area depends on your basic skills and experience.