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 ««12345»»»

T-SQL Expand / Collapse
Author
Message
Posted Monday, July 22, 2013 2:49 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, December 11, 2014 6:27 AM
Points: 2,624, Visits: 583
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
MCITP:Database Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
Post #1475897
Posted Monday, July 22, 2013 3:33 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 9:32 AM
Points: 1,417, Visits: 809
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.
Post #1475908
Posted Monday, July 22, 2013 3:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:36 PM
Points: 6,133, Visits: 8,398
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
Post #1475916
Posted Monday, July 22, 2013 4:35 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:17 AM
Points: 1,963, Visits: 2,407
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/
Post #1475926
Posted Monday, July 22, 2013 4:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:59 AM
Points: 5,489, Visits: 10,343
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.
Post #1475936
Posted Monday, July 22, 2013 4:52 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, December 19, 2014 9:43 AM
Points: 1,925, Visits: 2,205
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
Post #1475937
Posted Monday, July 22, 2013 5:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:17 AM
Points: 1,963, Visits: 2,407
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/
Post #1475944
Posted Monday, July 22, 2013 5:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:36 PM
Points: 6,133, Visits: 8,398
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
Post #1475949
Posted Monday, July 22, 2013 5:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:36 PM
Points: 6,133, Visits: 8,398
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
Post #1475958
Posted Monday, July 22, 2013 5:55 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 4:17 AM
Points: 1,963, Visits: 2,407
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



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1475965
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse