Efficiency of Top N and set rowcount n

Source: Internet
Author: User

Data paging through stored procedures is commonly used in programming.
Mssql2000 uses set rowcount N for paging, but after mssql2005
Using Top N for paging is much more convenient, but some friends told me that set rowcount N is more efficient.
I don't know much about it, so I went online to find Top N and set rowcount N, and then found the answer on the official website:
The Top N clause is as fast as the set rowcount n statement when the index is correctly performed.
 
However, if the data is not sorted, the Top N is faster. Top N
During the operation, a small intermediate temporary table is used, and only the last row of the table is replaced.
If the input is approximately sorted, the Top N engine must delete or insert the last row, but only a few operations are required.
The approximate sorting means that the heap you are processing can be inserted in sequence during initial build, and
You do not need to perform many operations, such as updating, deleting, and moving the Pointer Forward.
 
Sorting an approximate sorting heap is more efficient than sorting a huge table. In a test, use
Top N is used to sort a table created by the unordered insert operation with the same number of rows,
Topn is not efficient. Generally, if you have performed an index or have not performed an index,
I/O time is the same, but if no index has been performed, sqlserver must perform
A full table scan. Processor time and actual consumption time indicate that the approximate sorting of stacks should be more
Efficiency. However, the I/O time is the same, because no matter how SQL Server reads all rows.

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.