Do not specify order by sort in SQL by primary key _mssql

Source: Internet
Author: User

Do not specify an order by in SQL, sort by the primary key? The answer is not necessarily. As an example:

Query Attendanceemprank table, primary key is Attendanceemployeerankid, and is clustered index

Execute the following statement and find that the first sentence does not specify an order by result is not the same as the second sentence.

Looking at the execution plan, we can see that the first sentence uses the date index, and the second sentence uses the primary key index.

Look at another set of SQL and query results:

The indexes used in the execution plan are also different:

So it comes to the conclusion that, without specifying an order BY, SQL Server gets the data based on the actual query method of the execution plan, and the execution plan uses different indexes based on a number of factors in the SQL (query column, where condition, order by, and so on). The end result is likely to be different.

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.