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

problem with order by !!! Expand / Collapse
Author
Message
Posted Friday, October 25, 2013 1:12 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, November 7, 2014 2:28 AM
Points: 268, Visits: 680
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 ?
Post #1508343
Posted Friday, October 25, 2013 1:26 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 7:06 AM
Points: 225, Visits: 1,753
You didn't specify next criteria so there will be none.
Post #1508348
Posted Friday, October 25, 2013 1:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 13,545, Visits: 11,358
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1508354
Posted Friday, October 25, 2013 2:57 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, November 4, 2014 11:29 PM
Points: 466, Visits: 1,925
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
Post #1508366
Posted Friday, October 25, 2013 3:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 13,545, Visits: 11,358
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1508370
Posted Monday, October 28, 2013 1:10 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, November 4, 2014 11:29 PM
Points: 466, Visits: 1,925
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
Post #1508758
Posted Monday, October 28, 2013 4:50 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 5:03 AM
Points: 2,840, Visits: 3,975
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
Post #1508802
Posted Monday, October 28, 2013 7:08 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 13,545, Visits: 11,358
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1508854
Posted Monday, October 28, 2013 7:24 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: Today @ 6:50 AM
Points: 40,425, Visits: 36,875
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 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 #1508867
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse