Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Clustered indexed primary key not in asc order when selected


Clustered indexed primary key not in asc order when selected

Author
Message
ugo boy
ugo boy
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 27
Hi All,
I have a table that has a primary key which is a clustered index.
However when I select data based on a different field (not the primary key)
the result is n't sorted by the primary key.
I mean, if a table has a clustered indexed primary key (asc) shouldn't a query return results that are sorted by the primary key as long as not sort order is specified?
When I ran the query, I did not specify any sort order so I am assuming the result will automatically be displayed in the sort order of the primary key init?
it was a simple
select * from table_a where column_b = 'Z'
Please do let me know if you need more info

Thanks
Ugoboy
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47427 Visits: 44405
ugo boy (2/20/2013)
When I ran the query, I did not specify any sort order so I am assuming the result will automatically be displayed in the sort order of the primary key init?


Incorrect assumption.

Without an order by, you're telling SQL you don't care about the order of the data, so you get it back in whatever order the last query operation left it. It might be the order of the index used to read the data. It might not.

If you care about order, specify the appropriate ORDER BY. If you don't specify an ORDER BY, then you cannot assume anything about the order of the data.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
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


ugo boy
ugo boy
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 27
Thanks Gail for your response. does it mean that a previous query can distort the indexed order?
I just discovered something unusual...
When I ran the query I was talking about, I used a (nolock) hint and that was when the results came back in the unsorted order but when I reran the query without the (nolock) hint, the results came back in the correct ascending order of the clustered indexed primary key.
does a (nolock) hint affect sort orders, can it?
Andy Hyslop
Andy Hyslop
Say Hey Kid
Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)

Group: General Forum Members
Points: 706 Visits: 3023
Sql Server will scan the table differently for the query's with and without the hints which is why you may be getting a different sort order..

The only way to guarantee the order of the query is to use the ORDER BY regardless of previous results and expectations based upon those..

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47427 Visits: 44405
ugo boy (2/20/2013)
Thanks Gail for your response. does it mean that a previous query can distort the indexed order?


Err, no. Selects don't change indexes.

When I ran the query I was talking about, I used a (nolock) hint and that was when the results came back in the unsorted order but when I reran the query without the (nolock) hint, the results came back in the correct ascending order of the clustered indexed primary key.
does a (nolock) hint affect sort orders, can it?


Again, without an ORDER BY on your query, there's no such thing as 'correct order'. Without an Order By, you're telling SQL that there is no meaning, no importance, no relevance to the order of the data, that any order is as good as any other order.

If you need a specific order, you *must* put an ORDER BY on the query.

btw, on that nolock, see - http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
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


OTF
OTF
Old Hand
Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)

Group: General Forum Members
Points: 316 Visits: 4128
Pick up Itzik Ben Gan's book on TSQL Fundamentals, if you can.
It will go a long way towards plugging a few gaps in your knowledge.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search