asp. net talks about website performance optimization

  • 2020-05-10 18:03:00
  • OfStack

Of course, website performance optimization is multifaceted, here to talk about the first of these days:
1. The habit of writing code;
Again complex logic, but also from the most simple start. In the process of writing code, many bad specifications will affect the performance of the website;
Here are a few code habits:
1) compare strings with string.Empty instead of ""
2) in the process of traversal, count variables are defined first and then traversed, which will reduce the memory space allocated once for each traversal:
 
int i; 
for( i=0; i<100;i++) 
{ 
// codeing 
} 

3) similarly, use StringBuilder.Append () instead of [csharp] string +="ABC" [/csharp];
4) process the logic in the traversal loop without calling other method forms, as there will be a performance penalty when invoked
5) when summing/subtracting, A+=1, A-=1; Instead of A=A+1; This slows down the number of memory requests
6) multiple collection operations
a: if the element types in the collection are fixed, you can use their corresponding collection classes, such as groups, generics, etc., so as to avoid boxed and unboxed operations.
b: if the number of elements in the collection is fixed and the type is 1, an array is used for storage.
c: you can use HashTable, Dictionaty if the operation on the collection is primarily a lookup aspect < TYey,TValue >
7) use Server.Transfer for page redirection
Benefits: performance is much better than response.redirect, and it has the benefit of hiding URL from the client to avoid page redirection.
Cons: use with caution if the user USES refresh or back can cause an accident
8) reduce the use of server controls
9) use caches reasonably (when appropriate)
10) reduce the size of Cookies
... ...
2. Database
1) the database should be opened at the latest and closed at the earliest;
2) optimize the database connection configuration. For large websites, due to the large amount of data in the database, the database connection pool can be increased
Max Pool Size = 512; (default is 100)
3) optimize SQL statements and use stored procedures
Note: try to avoid sql statements like "select * from"; Try not to use subqueries in query statements; Use indexes whenever possible;
4) use DataReader
In DataReader, we often use the dr[" field name "] lookup form, but this is the most expensive;
In general, using "serial number" based lookups is more efficient than "named" based lookups. Among them, it can be divided into four categories:
Write (1) using the DataReader index + search based on the "serial number", such as: dr[1].ToString(),
Write (2) using the DataReader index + "named" based look-up, such as: dr["LastName"].ToString(), which is the most poorly performing
(3) use the method beginning with Get + search based on the "serial number", such as: dr.GetString (1), write
Write (4) using the method beginning with GetSql + search based on the "serial number", e.g. dr.GetSqlString (1),
Write (5) using a lookup based on the "serial number" + GetOrdinal() method.
What is the GetOrdinal() function method?
We write the corresponding field from the database to the sequence number we are looking for, but sometimes when we insert a new field, it changes. What GetOrdinal() does is let us serialize the corresponding fields ourselves.
a, first define the serial number int classid,classnameid;
b, and then the new sequence number is given through the GetOrdinal() method;
c, the corresponding field can be found through the GetSqlSring(serial number) method.
The sample code is as follows:
 
SqlConnection con = new SqlConnection(connString); 
string strcmd = "select top 1 classid,classname where cms_class"; 
SqlCommand cmd = new SqlCommand(strcmd, con); 
con.Open(); 
SqlDataReader dr = cmd.ExecuteReader(); 
int numberId, numberName; 
numberId = Convert.ToInt32(dr.GetOrdinal("classid")); 
numberName = Convert.ToInt32(dr.GetOrdinal("classname")); 
con.Close(); 
if (dr.Read()) 
{ 
string result = dr.GetSqlString(numberId) + " | " + dr.GetSqlString(numberName); 
} 
dr.Close(); 

Related articles: