Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 271439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:09:46+00:00 2026-05-12T00:09:46+00:00

Is a table intrinsically sorted by it’s primary key? If I have a table

  • 0

Is a table intrinsically sorted by it’s primary key? If I have a table with the primary key on a BigInt identity column can I trust that queries will always return the data sorted by the key or do I explicitly need to add the “ORDER BY”. The performance difference is significant.

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:09:47+00:00Added an answer on May 12, 2026 at 12:09 am

    Data is physically stored by clustered index, which is usually the primary key but doesn’t have to be.

    Data in SQL is not guaranteed to have order without an ORDER BY clause. You should always specify an ORDER BY clause when you need the data to be in a particular order. If the table is already sorted that way, the optimizer won’t do any extra work, so there’s no harm in having it there.

    Without an ORDER BY clause, the RDBMS might return cached pages matching your query while it waits for records to be read in from disk. In that case, even if there is an index on the table, data might not come in in the index’s order. (Note this is just an example – I don’t know or even think that a real-world RDBMS will do this, but it’s acceptable behaviour for an SQL implementation.)

    EDIT

    If you have a performance impact when sorting versus when not sorting, you’re probably sorting on a column (or set of columns) that doesn’t have an index (clustered or otherwise). Given that it’s a time series, you might be sorting based on time, but the clustered index is on the primary bigint. SQL Server doesn’t know that both increase the same way, so it has to resort everything.

    If the time column and the primary key column are a related by order (one increases if and only if the other increases or stays the same), sort by the primary key instead. If they aren’t related this way, move the clustered index from the primary key to whatever column(s) you’re sorting by.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The table doesn't have a last updated field and I need to know when
A table exists that someone else loaded. I need to query against the table,
Table/Entity 1: RetrievedDataRecords Columns: id record_key Table/Entity 2: SourceKeys Columns: id key_name (unique key)
Table structure: +-------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default |
Table: UserId, Value, Date. I want to get the UserId, Value for the max(Date)
table data of 2 columns category and subcategory i want to get a collection
A table row is generated using an asp:Repeater: <asp:repeater ID=announcementsRepeater OnItemDataBound=announcementsRepeater_ItemDataBound runat=Server> <itemtemplate> <tr
The table I'm dealing with it potentially larger than available memory (let's say 10GB)
My table structure looks like this: tbl.users tbl.issues +--------+-----------+ +---------+------------+-----------+ | userid | real_name
create table person ( name varchar(15), attr1 varchar(15), attr2 varchar(1), attr3 char(1), attr4 int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.