Click here to Skip to main content
15,900,461 members
Home / Discussions / Database
   

Database

 
GeneralRe: duplicating table data while handeling primary key column Pin
Blue_Boy31-Mar-09 7:13
Blue_Boy31-Mar-09 7:13 
QuestionData Base Authication Mode Pin
Isaac Gordon30-Mar-09 21:12
Isaac Gordon30-Mar-09 21:12 
AnswerRe: Data Base Authication Mode Pin
Eddy Vluggen30-Mar-09 23:07
professionalEddy Vluggen30-Mar-09 23:07 
QuestionTemporary Table and Order By Pin
swjam30-Mar-09 18:26
swjam30-Mar-09 18:26 
AnswerRe: Temporary Table and Order By Pin
sam#30-Mar-09 20:55
sam#30-Mar-09 20:55 
GeneralRe: Temporary Table and Order By Pin
swjam30-Mar-09 21:09
swjam30-Mar-09 21:09 
GeneralRe: Temporary Table and Order By Pin
Mycroft Holmes30-Mar-09 21:35
professionalMycroft Holmes30-Mar-09 21:35 
GeneralRe: Temporary Table and Order By Pin
ButtonMoon31-Mar-09 11:36
ButtonMoon31-Mar-09 11:36 
The first thing to clear up is that you cannot control the insertion order of rows using ORDER BY, so the assumption on which the question is based is inaccurate. This is well documented in Books Online and elsewhere (see refs below).

If you insert rows one at a time then it is true that the insertion order determines the allocation order represented by IAM pages. It will not determine the eventual ordering of data pages however, or even necessarily the logical ordering of rows within a page, especially if page splits subsequently occur.

In a query, there is no way to specify how rows should be sorted other than by using ORDER BY. This is an important feature because it allows the optimiser to choose the most efficient path to the data based on requirements. By leaving out ORDER BY you signal to the optimiser "I don't care about order" and the optimiser chooses an execution plan accordingly. Check this for yourself by looking at the execution plan of a query without ORDER BY. You will typically see a scan showing "Ordered=False". Depending on various factors the optimiser can choose different strategies to retrieve the data for an unordered scan, which may or may not mimic the order in which rows were first inserted. Some relevant factors that influence the actual plan and sorting are: fragmentation, covering indexes and whether an "Advanced" scan is used (Enterprise Edition only).

Of course it is easy to contrive examples where the query order matches the insertion order and just as easy to make up examples where it doesn't. Just as surely, those nice safe examples could return different results if the conditions under which they ran were changed in some subtle way.

The correct and documented behaviour is that the sort order of a query without ORDER BY is undefined. If you ever assume otherwise then you are in unsupported territory and you must decide for yourself whether it is worth the possible risk of leaving out ORDER BY.

Hope this helps.


References

Insertion order not guaranteed for SELECT INTO with ORDER BY:
http://msdn.microsoft.com/en-us/library/ms188385.aspx

Insertion order not guaranteed for INSERT with ORDER BY (SQL Engine Team Blog):
http://blogs.msdn.com/sqltips/archive/2005/07/20/441053.aspx

Advanced Scan:
http://msdn.microsoft.com/en-us/library/ms191475.aspx

Ordered and Unordered Scans:
Inside Microsoft SQL Server 2005 T-SQL Querying, Chapter 3
http://www.sql.co.il/books/insidetsql2005/

Queries without ORDER BY are unordered:
http://technet.microsoft.com/en-us/library/cc917540.aspx
http://msdn.microsoft.com/en-us/library/ms187956.aspx
GeneralRe: Temporary Table and Order By Pin
Mycroft Holmes31-Mar-09 13:58
professionalMycroft Holmes31-Mar-09 13:58 
AnswerRe: Temporary Table and Order By Pin
Ashfield31-Mar-09 1:30
Ashfield31-Mar-09 1:30 
QuestionReading Columns Pin
ffowler30-Mar-09 10:00
ffowler30-Mar-09 10:00 
AnswerRe: Reading Columns Pin
sam#30-Mar-09 20:52
sam#30-Mar-09 20:52 
AnswerRe: Reading Columns Pin
Mycroft Holmes30-Mar-09 21:42
professionalMycroft Holmes30-Mar-09 21:42 
GeneralRe: Reading Columns Pin
Jörgen Andersson30-Mar-09 23:37
professionalJörgen Andersson30-Mar-09 23:37 
AnswerRe: Reading Columns Pin
anup keshari30-Mar-09 21:47
anup keshari30-Mar-09 21:47 
GeneralRe: Reading Columns Pin
anup keshari30-Mar-09 22:02
anup keshari30-Mar-09 22:02 
AnswerRe: Reading Columns Pin
anup keshari30-Mar-09 22:33
anup keshari30-Mar-09 22:33 
GeneralRe: Reading Columns Pin
ffowler31-Mar-09 2:05
ffowler31-Mar-09 2:05 
QuestionLooking for a Web/SQL reporting subsystem Pin
Max Stayner30-Mar-09 9:47
Max Stayner30-Mar-09 9:47 
AnswerRe: Looking for a Web/SQL reporting subsystem Pin
Mycroft Holmes30-Mar-09 21:45
professionalMycroft Holmes30-Mar-09 21:45 
QuestionSumming Amount by Group field Pin
BijayaSharma30-Mar-09 9:36
BijayaSharma30-Mar-09 9:36 
AnswerRe: Summing Amount by Group field Pin
Anubhava Dimri31-Mar-09 1:09
Anubhava Dimri31-Mar-09 1:09 
GeneralRe: Summing Amount by Group field Pin
BijayaSharma31-Mar-09 4:19
BijayaSharma31-Mar-09 4:19 
GeneralRe: Summing Amount by Group field Pin
BijayaSharma1-Apr-09 6:28
BijayaSharma1-Apr-09 6:28 
QuestionHow do i display Values Horizonatally In Crystal Reports Pin
Veerkumar Patil30-Mar-09 4:31
Veerkumar Patil30-Mar-09 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.