select without order by and clustered index
-
2008년 11월 18일 화요일 오후 9:46
is it true that if an order by is not specified in a query and the query is simply a select * from dbo.table that the order returned will always be the same as the clustered index? If the PK is the clustered index, would it follow that the implicit order returned would always be predictable since pk's must be unique?
답변
-
2008년 11월 18일 화요일 오후 11:42중재자
The Beatles versus the Stoneshttp://sqlblog.com/blogs/hugo_kornelis/archive/2006/12/31/Beatles-vs-Stones.aspx
Quaere Verum - Clustered Index Scans - Part I
http://www.sqlmag.com/Article/ArticleID/92886/sql_server_92886.html
Quaere Verum - Clustered Index Scans - Part II
http://www.sqlmag.com/Article/ArticleID/92887/sql_server_92887.html
Quaere Verum - Clustered Index Scans - Part III
http://www.sqlmag.com/Article/ArticleID/92888/sql_server_92888.html
AMB
-
2008년 11월 19일 수요일 오전 8:48답변자
Absolutely not.
Here's an example I wrote a while back to prove this to a colleague
Code SnippetCREATE TABLE dbo.order_by (
id int identity(1,1) PRIMARY KEY CLUSTERED
, some_field char(1)
)INSERT INTO dbo.order_by (some_field) VALUES ('B')
INSERT INTO dbo.order_by (some_field) VALUES ('A')SELECT *
FROM dbo.order_byCREATE NONCLUSTERED INDEX ni_order_by_some_field ON dbo.order_by (some_field ASC)
SELECT *
FROM dbo.order_byGO
DROP TABLE dbo.order_byThanks for the other links hunchback, I will bookmark them as further ammunition

-
2008년 11월 19일 수요일 오후 2:37중재자
No. If you do not use "Order by" the order of the result is "undefined". Which means, it can be anything depending on how the server feels at the time.
모든 응답
-
2008년 11월 18일 화요일 오후 11:42중재자
The Beatles versus the Stoneshttp://sqlblog.com/blogs/hugo_kornelis/archive/2006/12/31/Beatles-vs-Stones.aspx
Quaere Verum - Clustered Index Scans - Part I
http://www.sqlmag.com/Article/ArticleID/92886/sql_server_92886.html
Quaere Verum - Clustered Index Scans - Part II
http://www.sqlmag.com/Article/ArticleID/92887/sql_server_92887.html
Quaere Verum - Clustered Index Scans - Part III
http://www.sqlmag.com/Article/ArticleID/92888/sql_server_92888.html
AMB
-
2008년 11월 19일 수요일 오전 8:48답변자
Absolutely not.
Here's an example I wrote a while back to prove this to a colleague
Code SnippetCREATE TABLE dbo.order_by (
id int identity(1,1) PRIMARY KEY CLUSTERED
, some_field char(1)
)INSERT INTO dbo.order_by (some_field) VALUES ('B')
INSERT INTO dbo.order_by (some_field) VALUES ('A')SELECT *
FROM dbo.order_byCREATE NONCLUSTERED INDEX ni_order_by_some_field ON dbo.order_by (some_field ASC)
SELECT *
FROM dbo.order_byGO
DROP TABLE dbo.order_byThanks for the other links hunchback, I will bookmark them as further ammunition

-
2008년 11월 19일 수요일 오후 2:37중재자
No. If you do not use "Order by" the order of the result is "undefined". Which means, it can be anything depending on how the server feels at the time.

