October 25, 2013 at 1:12 am
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 ?
October 25, 2013 at 1:26 am
You didn't specify next criteria so there will be none.
October 25, 2013 at 1:34 am
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 🙂
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
October 25, 2013 at 2:57 am
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
October 25, 2013 at 3:04 am
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?[/url]
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.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
October 28, 2013 at 1:10 am
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?[/url]
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
October 28, 2013 at 4:50 am
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?[/url].
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;-)
October 28, 2013 at 7:08 am
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?[/url].
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).
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
October 28, 2013 at 7:24 am
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
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply