T-SQL optimization tips (2): Limits data size

Source: Internet
Author: User

The less data the T-SQL that SQL Server executes, the less resources it takes to complete the T-SQL. Therefore, the T-SQL statement should only return the required data, which helps improve the overall performance of SQL Server:

    • Full use of indexes to avoid full table scanning;
    • Reduce I/O operations for reading data;
    • Saves cache for other possible SQL server operations;
    • Avoid unnecessary network transmission;
    • Reduce memory usage caused by useless data on the client.

And so on.

 

1) Limit the number of rows in the result set

In actual development, there are very few operations to return data from all rows in a table, so the number of rows in the returned result set should be limited, the where clause, top operator, and set rowcount options are possible options. It is worth noting that when the top operator and the Set rowcount option are used, the T-SQL stops when the number of rows in the result set reaches a certain number (determined by the two selected parameters, that is, the latter two options are more efficient than the WHERE clause, but obviously the WHERE clause applies more situations. If both the top operator and the Set rowcount option are used in the T-SQL, although superfluous, efficiency is not affected, and the Set rowcount option is higher than the top operator in terms of priority.

 

2) Limit the number of columns in the result set

For a two-dimensional table structure, apart from limiting the number of rows, it is to limit the number of columns. Avoid using this function unless all columns are required.Select *Select data.

 

In addition, you should avoid the Select Operation on the view, because many views may return much more data than we need, and the more efficient way is to bypass the view and directly select data from the table.

DOS & donts--
3. Avoid using select *;

[Note: All dos & donts summarized in this series areWith certain prerequisitesThe general premise is "unless necessary" and "unless necessary ".]

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.