SQL statement Optimization

Source: Internet
Author: User

1, do not use "SELECT *" in the query to retrieve unnecessary columns will bring additional overhead, there is a sentence called "the province of the province";

2. Avoid unnecessary columns in the select list and avoid unnecessary tables in the connection conditions;

3. Do not use COUNT () sum in subqueries to perform an existence check

4. Avoid using two different types of columns for table connections

5. Avoid deadlocks

6. Avoid using count (*) to get the number of records in the table

(1) In order to obtain the number of records in the table, we typically use the following SQL statement: SELECT COUNT (*) from Dbo.orders This statement performs a full table scan to get the number of rows. (2) But the following SQL statement does not perform a full table scan as you can get the number of rows: SELECT rows from sysindexes

WHERE id = object_id (' dbo. Orders ') and Indid < 2

7. Avoid the use of dynamic SQL.       (1) Dynamic SQL is difficult to debug and fault diagnosis; (2) If the user provides input to dynamic SQL, there may be a risk of SQL injection.

8, in the write stored procedure, begins to write set NOCOUNT ON, the end writes set NOCOUNT OFF

( when SET NOCOUNT is on, no count is returned (indicating the number of rows affected by the Transact-SQL statement). When SET NOCOUNT is off, the count is returned (default is off).

The @ @ROWCOUNT function is updated even when SET NOCOUNT is on.

When SET NOCOUNT is on, the client is not sent Done_in_proc information for each statement in the stored procedure. When using Microsoft? SQL Server? When a utility executes a query, a Transact-SQL statement (such as SELECT, INSERT, UPDATE, and DELETE) will not display an "NN row affected" in the query results.

If some of the statements contained in a stored procedure do not return much of the actual data, this setting can significantly improve performance because of the significant reduction in network traffic.

Set NOCOUNT settings are set at execution or runtime, not at parse time.

Note When set NOCOUNT is on, no count is returned (indicating the number of rows affected by the Transact-SQL statement). When set NOCOUNT is off, the count is returned. ... Conclusion: We should add set NOCOUNT on the head of the stored procedure so that when we exit the stored procedure, we add set NOCOUNT off so as to achieve the optimization.

SQL statement Optimization

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.