Primary Key Sorting

后端 未结 7 653
醉话见心
醉话见心 2020-12-16 03:48

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 dat

7条回答
  •  情深已故
    2020-12-16 04:29

    A table by default is not 'clustered' , i.e. organized by PK. You do have the option of specifying it as such. So the default is "HEAP" (in no particular order), and the option you are looking for is "CLUSTERED" (SQL Server, in Oracle its called IOT).

    • A table can only have one CLUSTERED (makes sense)
    • Use the PRIMARY KEY CLUSTERED syntax on the DDL
    • Order by PK still needs to be issued on your SELECTS, the fact of it being clustered will cause the query to run faster, as the optimizer plan will know it does not need to do the sorting on a clustered index

    The earlier poster is correct, SQL (and the theoretical basis of it) specifically defines a select as an unordered set/tuple.

    SQL usually tries to stay in the logical-realm and not make assumptions about the physical organization / locations etc. of the data. The CLUSTERED option allows us to do that for practical real-life situations.

提交回复
热议问题