Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

identity order is not in order Expand / Collapse
Author
Message
Posted Monday, February 11, 2013 5:43 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 1:05 PM
Points: 90, Visits: 315
We have a table in our system that has an identity to one of the field.
One would think when you would do a regular insert into a table, the rows would come in the order of the identity field.
For example, if Table A has (col_name varchar(20), col3 identity(1,1),
the inserted rows should come as:

select * from TableA

'Donna',1
'Jack',2
'David',3
'Sam',4
....,5
,...,6
....,7
....,8
...,9
...,10
'Donna',1
'Jack',2
'David',3
'Sam',4
....,5
,...,6
....,7
....,8
...,9
...,10
...,14

But that is not the case.
We did regular inserts and the tableA didnot come in the order of the identity.

'Jeff',11
'Tom,12
'Maria,13
'Donna',1
'Jack',2
'David',3
'Sam',4
,...,5
....,6
....,7
....,8
...,9
...,10
...,14

What would cause this out of order sequencing? Is there anyway to maintain the ordering of the identity column in the right physical order of the table?

We try to add primary key clustered index to the table and then drop the index. It fixed the ordering, but new inserts will still insert in out of order of the Identity column.
Post #1418696
Posted Monday, February 11, 2013 5:48 PM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 2:57 AM
Points: 704, Visits: 3,270
Is there a clustered index on the table? If so, which columns does it reference?

oops just saw the last line of your post. Why no set your ID column as your PK?


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1418699
Posted Monday, February 11, 2013 7:07 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 12:17 PM
Points: 3,081, Visits: 11,231
The only way to ensure that a query returns results in the order that you want is to have an ORDER BY.

select * from TableA order by col3


Post #1418713
Posted Monday, February 11, 2013 8:02 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 9:25 AM
Points: 7,070, Visits: 12,523
Hollyz (2/11/2013)
What would cause this out of order sequencing? Is there anyway to maintain the ordering of the identity column in the right physical order of the table?

What you see when you select from the table and what is happening internally with respect to storage are two different things. If you want your data logically stored in the order of the IDENTITY column then make the IDENTITY column the clustered index key. Note that the clustered index key and the primary key do not have to be the same, i.e. you can have a nonclustered primary key.

We try to add primary key clustered index to the table and then drop the index. It fixed the ordering, but new inserts will still insert in out of order of the Identity column.

As Michael correctly states, the only way to guarantee order when selecting data is to provide an order by.

If you meant ordering on insert, as in when doing an INSERT...SELECT, you can provide an ORDER BY there too and it will issue the identity values per that ordering although I am not sure why it matters since you must still provide an ORDER BY when selecting data to guarantee order.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato

Believe you can and you're halfway there. --Theodore Roosevelt

Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein

The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein

1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
Post #1418717
Posted Tuesday, February 12, 2013 12:43 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:57 PM
Points: 41,558, Visits: 34,479
Hollyz (2/11/2013)
One would think when you would do a regular insert into a table, the rows would come in the order of the identity field.


Table are unordered sets of rows. The insert order has no effect on the order that rows will be returned.

What would cause this out of order sequencing? Is there anyway to maintain the ordering of the identity column in the right physical order of the table?


Cause is simple, no order by on the retrieving query. If you want data in a specific order, you must put an order by on the outer query. Otherwise you're telling SQL 'I don't care what order the data comes back in', and SQL will honour your lack of request and return the data in whatever order the last query operator left it.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1418774
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse