How to improve website performance (study notes)

Source: Internet
Author: User
Tags website performance
1. the cache can store frequently used data and data that requires a large amount of time to be created in the memory. Later requests are directly used and do not need to be generated. The usage is simple: <% @ outputcache varybyparams = "NONE" Duration = "120" %> Cache principles: 1) Separate static content from dynamic content on the page. You can separate dynamic content into user controls. 2) cache reasonable data. Not all data content can be cached to improve performance. Because the resources of the server are limited, improper data caching reduces performance. Applications should be cached. Program Level data, data used by multiple users, static data, data generation requires a lot of overhead data, dataset and custom objects, and do not cache database connection objects, do not cache datareader. 3) select the appropriate method //////////////////////////////////// //// // 2. View 1) disable a view if you do not need it, View status is allowed by default. If the page is not PostBack and server control events are not processed, you can disable it. 2) minimize the number of objects stored in the view State. Do not store a large number of objects in the view State. //////////////////////////////////////// //// // Page processing 1) minimize the size of the page file. Save CSS and scripts as separate files. Do not place multiple tasks on the same page. do not specify a long string with the ID data bit of the server control. 2) through page. reduced ispostback Code The number of executions. 3) Disable debug = "true" to reduce the page generation of additional debugging information. 4) use server. transfer without using response. redirect5) Try to use client verification, reduce the use of server-side authentication, reduce the round-trip between the server and the client. 6) when appropriate, use the client server control. If you do not need to maintain the status during sending back, if the displayed data is static data, if you do not need to use code to access the control on the server, if read-only data is displayed, you can choose not to use the server control. 7) Avoid nested server controls as much as possible. //////////////////////////////////////// //// // Avoid using page. databind and databinder. evalpage. when databind is executed, all the server controls on the page will execute the databind method once. Do not use it if necessary. The databinder. Eval method uses reflection to obtain parameters. Do not use as few as possible: <itemtemplate> <Tr> <TD> <% # databinder. eval (container. dataitem, "Field 1") %> </TD> <TD> <% # databinder. eval (container. dataitem, "Field 1") %> </TD> </Tr> </itemtemplate> and use: <itemtemplate> <Tr> <TD> <% # (datarowview) container. dataitem ["Field 1"] %> </TD> <TD> <% # (datarowview) container. dataitem ["Field 1"] %> </TD> </Tr> </itemtemplate> //////////////////////////////// ////////////// about application and Session Object 1) try to use the session mode in inproc mode as quickly as possible. 2) store basic types of books in the session to reduce the resources consumed by serialization. 3) If session is not used, use enableviewstate = "false" to disable it. 4) If you do not modify the session variable, please use the readonly attribute to set /////////////////////////////////// /// // for string operation 1) use string when the string is short and few characters exist. the Concat method uses the sringbuilder object when the string length is unknown and relatively long. 2) do not use strvar = "" to determine whether the string is null. This will generate additional strings, use ST Rvar = string. empty or strvar. length = 0 to Judge 3) use string. compare Method /////////////////////////////////// /// // about Data Access 1) use stored procedures to return data as much as possible. 2) return only useful data results in the database. Do not select unused data fields. 3) Use datareader for Data Binding. datareader is one-way read-only. 4) try to return multiple record sets at a time, instead of opening a database connection for each record set for query. 5) Open the database connection as late as possible, close the database connection as early as possible, (use the using statement whenever possible to close the database connection) 6) use the connection pool to improve website performance, do not change the database connection string, do not cache the connection or place it in the application object. When using the connection pool, closing the connection is not a physical connection, Instead, the connection is returned to the connection pool for use by other users. Therefore, close the connection as early as possible. 7) executenonquery is used to return data and do not return data, exectuescalar is used to return a single result. commandbehavior is used. sequentialaccess returns binary or big data. 8) if the same query is performed multiple times, use the command. Prepare method. 9) use the getordinal method to obtain the index value in advance. Using the microcosm value is more efficient than using a string column name to query data, //////////////////////////////////////// /// // about code optimization 1) when parsing basic data types, the tryparse method is better than try. 2) using appendalltext, writeallbytes, writeallline, writealltext, readallbytes, readallline, and readalltext can optimize the file content Reading and Writing Performance. 3) do not use the following method String [] arr = new string {"fly", "Flying "} For (INT I = 0; I <arr. length; I ++) { // Todo } This method calculates the value of arr. length once every loop. And use String [] arr = new string {"fly", "Flying "} Int length = arr. length; For (INT I = 0; length; I ++) { // Todo } 4) Avoid creating objects in a loop For (INT I = 0; I <10; I ++) { Sqlconnection Cn = new sqlconnection (); // Todo } And use Sqlconnection Cn = new sqlconnection (); For (INT I = 0; I <10; I ++) { // Todo } 5) try to reduce the number of packing times. If you want to perform type conversion in multiple places, define a variable first, for example: Int I = 129; Object box; Box = (objiect) I; // Use box multiple times below; 6) do not use the exception Control Program process, Void userexists (string userid) { If (dr. Read ()) { Throw (new exception ("the user name does not exist "); } } The modification code is as follows: Bool userexists (string userid) { Return dr. hasrows; } 7) do not use unchanged objects or fields in a loop, for example: For (INT I = 0; I <customer. Order. Count; I ++) { Printcustomerdata (customer. State, customer. Zip, customer. Order [I]); } In this case, we need to find the property value or field of the object in the loop. String state = Customer. State; String [] Zip = Customer. Zip; Int COUNT = customers. Order. count; For (INT I = 0; I <count; I ++) { Printcustomerdata (State, zip, customer. Order [I]); } 8) Use foreach loop instead of for loop 9) array is the fastest among all sets. If there is no special need, use array instead of set 10) understand the features of each set, select the appropriate type. 11) use the generic type to avoid using less packing and unpacking. ///////////////////////////////////////// Improve website performance. txt

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.