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


T-SQL


T-SQL

Author
Message
hakan.winther
hakan.winther
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2702 Visits: 601
kapil_kk (7/22/2013)
PRR.DB (7/22/2013)
If table doesn't have clustered index , there is no guarantee for order , if table having clustered index then result will display clustered index column order.

Is my answer correct or not? Can any one explain on it?

--Example query.......

USE tempdb
GO
create table Orderby(ID int)
go
insert into Orderby
select 1
union all
select 2
union all
select 5
union all
select 4
union all
select 3
union all
select 7
union all
select 6
go
select * from Orderby
go
CREATE CLUSTERED INDEX [ClusteredIndex_Order1_ID] ON [dbo].[Orderby]
(
[ID] ASC
)
go
select * FROM Orderby
GO
DROP TABLE Orderby
GO




Yes, if a table has clustered index then it will cause reordering of the physical order of the data...
For more information you can refer an article posted today:

http://www.sqlservercentral.com/articles/clustered+index/100477/


There are no guarantees that the clustered index order is used. If there are any nonclustered index or someone later adds a non clustered index, the order of the nonclusrered index may be used instead. If you can't add a nonclustered index because a lazy developer assumes the data is returned in the clustered index order, then you can't optimize the database without breaking the application.

/Håkan Winther
MCITPBigGrinatabase Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
paul s-306273
paul s-306273
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1925 Visits: 1060
bitbucket-25253 (7/20/2013)
If any one misses this basic question ... woe to the place where they work


Yes, well at this moment we have 40% incorrect.

That's not a great surprise to me.
I've spoken to lots of people in the past about this.
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8339 Visits: 11584
PRR.DB (7/22/2013)
If table doesn't have clustered index , there is no guarantee for order , if table having clustered index then result will display clustered index column order.

Is my answer correct or not? Can any one explain on it?

A very popular misconception. For simple, small queries on small tables, tests will suggest that it's true. But it isn't.

First - order is not guaranteed. This is documented (as far as I know). That alone makes the answer to this question unambiguous.

Second, on large tables, you can have other things influencing the order. For instance, parallel execution. I have seen results that were obviously caused by this - e.g. first rows 6001-8000 (in order), then 10001-12000 (in order), then 1-6000 (in order), etc - obviously, several parallel tasks each processing 2000 rows but finishing out of order. If you add ORDER BY, SQL Server will ensure that they are returned in order. Without ORDER BY, SQL Server won't bother - why include the overhead if you don;t care about order?

Third, on enterprise edition SQL Server can perform an "advanced scan" (see http://msdn.microsoft.com/en-us/library/ms191475%28v=sql.105%29.aspx), where a scan first starts fetching rows from another already in-progress, then restarts the scan from the start to retrieve the rows it missed.

And fourth - Google "sqlblog beatles vs stones" for a blog post I wrote many years ago that shows another very common cause of order being different from clustered index in a (hopefully) funny way.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
hakan.winther (7/22/2013)
kapil_kk (7/22/2013)
PRR.DB (7/22/2013)
If table doesn't have clustered index , there is no guarantee for order , if table having clustered index then result will display clustered index column order.

Is my answer correct or not? Can any one explain on it?

--Example query.......

USE tempdb
GO
create table Orderby(ID int)
go
insert into Orderby
select 1
union all
select 2
union all
select 5
union all
select 4
union all
select 3
union all
select 7
union all
select 6
go
select * from Orderby
go
CREATE CLUSTERED INDEX [ClusteredIndex_Order1_ID] ON [dbo].[Orderby]
(
[ID] ASC
)
go
select * FROM Orderby
GO
DROP TABLE Orderby
GO




Yes, if a table has clustered index then it will cause reordering of the physical order of the data...
For more information you can refer an article posted today:

http://www.sqlservercentral.com/articles/clustered+index/100477/


There are no guarantees that the clustered index order is used. If there are any nonclustered index or someone later adds a non clustered index, the order of the nonclusrered index may be used instead. If you can't add a nonclustered index because a lazy developer assumes the data is returned in the clustered index order, then you can't optimize the database without breaking the application.

Yes you are right but user PRR asked a scenario only for clustered index and if a clustered index is used then it will reorder of the data in physical order...

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7492 Visits: 15149
kapil_kk (7/22/2013)
Yes you are right but user PRR asked a scenario only for clustered index and if a clustered index is used then it will reorder of the data in physical order...

You're confusing the way the data is stored with the way it's returned. As Hugo explained, they're not guaranteed to be the same.

John

Edit - corrected typo.
Thomas Abraham
Thomas Abraham
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2347 Visits: 2254
Toreador (7/22/2013)
PRR.DB (7/22/2013)
If table doesn't have clustered index , there is no guarantee for order , if table having clustered index then result will display clustered index column order.

Is my answer correct or not? Can any one explain on it?


It may well display in this order. But there is no guarantee that it always will, or that the behaviour will be the same in the next release. There is nothing in the standard or in the documentation that states what the sequence will be, therefore you cannot rely on it.


Which is why one should never assume that results with appear in any order that is desired, unless you specify that order in your query.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
John Mitchell-245523 (7/22/2013)
kapil_kk (7/22/2013)
Yes you are right but user PRR asked a scenario only for clustered index and if a clustered index is used then it will reorder of the data in physical order...

You're confusing the way the data is stored with the way it's returned. As Hugo explained, they're not guaranteed to be the same.

John

Edit - corrected typo.

When we declare a clustered index on a column then it will gives us the data in a sorted manner or not?

As per the BOl and I read articles its said that after declaring a clustered index it will do reordering of data in a physical order, does this not mean that data will come in sorted manner?

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8339 Visits: 11584
kapil_kk (7/22/2013)
Yes you are right but user PRR asked a scenario only for clustered index and if a clustered index is used then it will reorder of the data in physical order...

For storage, you are right - the physical storage structure of the data is determined by the clustered index (though not necessarily exactly in the way many people think it is).
But this question is about the order in which data is returned from a query that does not use ORDER BY. And that order is not related to either the clustered index, the primary key, the insertion order, or the physical storage structure.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8339 Visits: 11584
kapil_kk (7/22/2013)
John Mitchell-245523 (7/22/2013)
kapil_kk (7/22/2013)
Yes you are right but user PRR asked a scenario only for clustered index and if a clustered index is used then it will reorder of the data in physical order...

You're confusing the way the data is stored with the way it's returned. As Hugo explained, they're not guaranteed to be the same.

John

Edit - corrected typo.

When we declare a clustered index on a column then it will gives us the data in a sorted manner or not?

As per the BOl and I read articles its said that after declaring a clustered index it will do reordering of data in a physical order, does this not mean that data will come in sorted manner?


My previous reply was interrupted by a long phone conversation, so I did not see the new messages.
The answer to your questions is: no. Without ORDER BY, there is no guarantee that the rows will be returned in clustered index order. Or in any other particular order. The only thing in SQL Server that guarantees that rows from a query are returned to the client in a particular order is an explicit ORDER BY clause, on the outermost level of the query. All other situations where you happen to see a particular order are side effects of how stuff is implemented, undocumented, unguaranteed, and prone to change.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 2763
Hugo Kornelis (7/22/2013)
kapil_kk (7/22/2013)
John Mitchell-245523 (7/22/2013)
kapil_kk (7/22/2013)
Yes you are right but user PRR asked a scenario only for clustered index and if a clustered index is used then it will reorder of the data in physical order...

You're confusing the way the data is stored with the way it's returned. As Hugo explained, they're not guaranteed to be the same.

John

Edit - corrected typo.

When we declare a clustered index on a column then it will gives us the data in a sorted manner or not?

As per the BOl and I read articles its said that after declaring a clustered index it will do reordering of data in a physical order, does this not mean that data will come in sorted manner?


My previous reply was interrupted by a long phone conversation, so I did not see the new messages.
The answer to your questions is: no. Without ORDER BY, there is no guarantee that the rows will be returned in clustered index order. Or in any other particular order. The only thing in SQL Server that guarantees that rows from a query are returned to the client in a particular order is an explicit ORDER BY clause, on the outermost level of the query. All other situations where you happen to see a particular order are side effects of how stuff is implemented, undocumented, unguaranteed, and prone to change.

create table Orderby(ID int)
go
insert into Orderby
select 1
union all
select 2
union all
select 5
union all
select 4
union all
select 3
union all
select 7
union all
select 6
go
select * from Orderby
go
CREATE CLUSTERED INDEX [ClusteredIndex_Order1_ID] ON [dbo].[Orderby]
(
[ID] ASC
)
go
select * FROM Orderby
GO
DROP TABLE Orderby

In this example after putting clustered index we will data in ascending sorted order.... while before adding clustered index data was not coming in an order...
I am confused now Doze

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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