Top N and SET rowcount n which is faster?

Source: Internet
Author: User
Tags rowcount
Lazy to translate, to the effect:
Top N and SET rowcount n are as fast when there is an appropriate index. But for a unordered heap, top n is faster.
Principle to see English.
Q. are using the top N clause faster than using SET rowcount N to return a specific number of rows from query?
A. With proper indexes, the top n clause and SET rowcount n statement are equally fast, but with unsorted input from a hea P, top N is faster. With unsorted input, the top N operator uses a small internal sorted temporary table in which it replaces ' last RO W. If the The input is nearly sorted, the top N engine must delete or insert the last row of a few times. Nearly sorted means ' re dealing with a heap with ordered inserts for the initial population and without many, D Eletes, forwarding pointers, and so on afterward.
A nearly sorted heap is more efficient to sort than sorting a huge table. In a test so used top N to sort a table with the same number of rows but with unordered inserts, Top n is not as Effici ENT anymore. Usually, the I/O same both with a index and without; However, without a index SQL Server must do a complete table scan. Processor time and elapsed the efficiency of the nearly sorted heap. The I/O time are the same because SQL Server must read all the rows either way.

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.