Problems to be paid attention to in ASP.net programming program optimization

Source: Internet
Author: User
Tags eval exception handling join requires sql injection first row throw exception

I. Selection of Sqldataread and Datasets


Sqldataread Advantages: 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 out





(SqlDataReader read 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 with just one connection




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





II, ExecuteNonQuery and ExecuteScalar





does not need to return a result set to update the data, it is recommended to 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.





* You only need to update the data with ExecuteNonQuery. Single value queries use ExecuteScalar data binding selection





third, data binding DataBinder





General binding Methods <%# DataBinder.Eval (Container.DataItem, "field name")%> do not have to care about data sources (DataBinder.Eval or datasets) with Dataread bindings. 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.





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





is 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.





the use of 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. (Stored procedures have been precompiled for SQL statements, so they execute much faster than executing SQL statements in a program)





2, program structure: From the scalability of the program, the use of stored procedures will be easy to modify the program later. 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. In this I only provide a way to optimize the order of the tables, (the Optimization and principles of SQL statements will be server2000 in my SQL note-taking learning notes)





SQL statement execution efficiency can be used to view the execution of a statement using SQL server2000 Query Analyzer.





Optimization 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 a table 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

three, page optimization (. 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)

The
(2) page is not passed back to itself.


(3) does not need to handle the control's events.

The
(4) control has no dynamic or data-bound property values (or is handled in code for each postpack)





a single page or each page disables ViewState as follows: Single page: <%@ page enableviewstate= "False"%> each page: Web.config <pages Wstate= "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, 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 is set to true to improve the user's perceived performance. Enabling this property has little impact on the client and server side. It can be smart to rinse new needs to rinse new parts.





four, control selection:





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 (specifically to see the control's optimization)





server Control selection: mainly for a few commonly used data Control description:





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 the data, I'm basically choosing Repeater and DataList the final DataGrid





* Try to select HTML controls. 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.




The optimization of the
control mainly depends on your familiarity 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.





six, abnormal problem





general problems do not need to use exceptions, for example, the user does not exist in the forum, the user password is incorrect, because the instantiation of an exception requires a lot of resources, need to fill the exception information (exception type, throw exception location, etc.), of course, not to avoid using exceptions, for the necessary exception handling or processing, The principle of the exception is: Can not be used without, can use the exception of the system already do not generate their own exceptions.

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.