SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


problem with order by !!!


problem with order by !!!

Author
Message
Shadab Shah
Shadab Shah
SSC-Addicted
SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)SSC-Addicted (443 reputation)

Group: General Forum Members
Points: 443 Visits: 798
Consider i have the table as follow,

Id Name1 Name2 Value1 Value2
1 Abc Xyz 10 20
1 Def qwe 70 80
1 rty uiio 30 70
1 dbf kio 70 35

Suppose now if i write the query as
select * from AboveTable Order By Id



Now over here i understand that the table would be sorted on Id. But all the column have same Id so what is the next creteria on which the records would be sorted ?
e4d4
e4d4
SSC-Addicted
SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)SSC-Addicted (465 reputation)

Group: General Forum Members
Points: 465 Visits: 2399
You didn't specify next criteria so there will be none.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27547 Visits: 13268
It might depend on indexes, the physical location of the rows in the pages and how the query plan is generated.
Let's assume it's "random" unless you specify another column in your ORDER BY.

By the way, that's a pretty lousy ID :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
chandan_jha18
chandan_jha18
SSC Eights!
SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)

Group: General Forum Members
Points: 895 Visits: 2134
Koen Verbeeck (10/25/2013)
It might depend on indexes, the physical location of the rows in the pages and how the query plan is generated.
Let's assume it's "random" unless you specify another column in your ORDER BY.

By the way, that's a pretty lousy ID :-)


I second that the ID is pretty lousy:-)

However, I do have a question here. Eve though a clustered index sorts the data physically but still it is recommended to use an Order by clause always if the data needs to be sorted. so what do you mean by 'depends on indexes'. Kindly explain.

Thanks
Chandan Jha
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27547 Visits: 13268
chandan_jha18 (10/25/2013)
Koen Verbeeck (10/25/2013)
It might depend on indexes, the physical location of the rows in the pages and how the query plan is generated.
Let's assume it's "random" unless you specify another column in your ORDER BY.

By the way, that's a pretty lousy ID :-)


I second that the ID is pretty lousy:-)

However, I do have a question here. Eve though a clustered index sorts the data physically but still it is recommended to use an Order by clause always if the data needs to be sorted. so what do you mean by 'depends on indexes'. Kindly explain.


A clustered index does not sort the rows physically in the pages, but logically.

Does a Clustered Index really physically store the rows in key order?

What I meant with my statement was that if no ORDER BY is specified, the order of the rows fetched can be influenced by the indexes that are used in the query plan.
If you want a specific order for some reason, use ORDER BY. If you don't, or don't specify enough columns in the ORDER BY, do not expect any specific order.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
chandan_jha18
chandan_jha18
SSC Eights!
SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)SSC Eights! (895 reputation)

Group: General Forum Members
Points: 895 Visits: 2134
Koen Verbeeck (10/25/2013)
chandan_jha18 (10/25/2013)
Koen Verbeeck (10/25/2013)
It might depend on indexes, the physical location of the rows in the pages and how the query plan is generated.
Let's assume it's "random" unless you specify another column in your ORDER BY.

By the way, that's a pretty lousy ID :-)


I second that the ID is pretty lousy:-)

However, I do have a question here. Eve though a clustered index sorts the data physically but still it is recommended to use an Order by clause always if the data needs to be sorted. so what do you mean by 'depends on indexes'. Kindly explain.


A clustered index does not sort the rows physically in the pages, but logically.

Does a Clustered Index really physically store the rows in key order?

What I meant with my statement was that if no ORDER BY is specified, the order of the rows fetched can be influenced by the indexes that are used in the query plan.
If you want a specific order for some reason, use ORDER BY. If you don't, or don't specify enough columns in the ORDER BY, do not expect any specific order.


Agreed. My mistake in mentioning that.

thanks
Chandan
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5294 Visits: 4076
Koen Verbeeck (10/25/2013)
A clustered index does not sort the rows physically in the pages, but logically.

Does a Clustered Index really physically store the rows in key order?.
I also read it many places but does microsoft(msdn) say the same ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27547 Visits: 13268
Bhuvnesh (10/28/2013)
Koen Verbeeck (10/25/2013)
A clustered index does not sort the rows physically in the pages, but logically.

Does a Clustered Index really physically store the rows in key order?.
I also read it many places but does microsoft(msdn) say the same ?


Does it matter?
The blog I pointed to is from Wayne, an MCM.
I'd rather trust him than the persons who write the MSDN documentation.
MSDN is great, but not perfect (and not written by MCMs).


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87996 Visits: 45277
Bhuvnesh (10/28/2013)
I also read it many places but does microsoft(msdn) say the same ?


Books Online states that the clustered index enforces physical storage order. Books Online is wrong, on this and a number of other issues. It's easy to prove too.

Index fragmentation is defined (in BoL) as the % by which the physical order of the index differs from the logical order.
Books Online states that a clustered index enforces physical storage order of the index (to match logical order)
Therefore (based on the prior two premises), a clustered index always has 0% logical fragmentation.

Now we know that to be false, clustered indexes can and do have non-zero fragmentation, so following the rules of logic one of the premises must be false. The definition of logical fragmentation is correct (and can be proven), so it must be the physical storage order premise that is wrong.

Following logically from that, one has to conclude that clustered indexes do not enforce physical storage order and any time the logical fragmentation is non-zero the logical and physical order of the clustered index doesn't match, hence the rows are not physically stored in clustered index order.

Q.E.D.

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


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