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

When To Use the Order By Clause Expand / Collapse
Author
Message
Posted Monday, May 17, 2004 4:52 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, April 27, 2010 8:31 AM
Points: 56, Visits: 87
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/g

Blog:
Gulappa
Post #116321
Posted Wednesday, July 7, 2004 6:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, November 12, 2008 10:24 AM
Points: 31, Visits: 24
see: http://www.sqlservercentral.com/forums/messages.aspx?forumid=172


Post #125085
Posted Monday, October 18, 2004 12:58 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, December 28, 2007 12:57 AM
Points: 63, Visits: 2

I think you have more want to tell us , but you didnit say there .

we know "where clause " have more effect on indexes and " when to use the order by clause " ,  tell me those ,pls .

 

think a lot

Post #142136
Posted Thursday, May 19, 2005 6:12 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 5:09 PM
Points: 977, Visits: 277

The article is misleading. This is from BOL:

SQL Server 6.x SQL Server 2000
A SELECT statement without an ORDER BY clause returned the rows in an apparent ordered set. An explicit ORDER BY clause for a SELECT statement is required to ensure any useful ordering of data. In addition, the exact results depend upon the collation being used.

Expect different results as compared to earlier versions of SQL Server. Add an explicit ORDER BY clause to all SELECT statements needing to produce ordered rows.




Post #183654
Posted Thursday, May 19, 2005 7:18 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, October 24, 2013 12:12 PM
Points: 72, Visits: 326
The author creates a novelle about the proverbial tip of the iceberg. In almost any life-case the resultset of a query is handled by application or reporting tool, so the order of data is academical anyway... 
Post #183678
Posted Thursday, May 19, 2005 7:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:46 AM
Points: 2,879, Visits: 3,229

The article shows the danger of trying something out with a small set of data, and expecting the same results with a large set of data because of an assumption the only influence on the result is the volume of data.  We have all done this to some extent, and no doubt we will all make unwarranted assumptions again in the future...

In the article, the small set of data was contained in one database extent.  With a large set of data, multiple extents will be used.  When the table grows, the next available extent will be allocated to the table.  If delete or drop activity has taken place to other tables in the database, the 'next available extent' may be physically closer to the start of the file than other extents already allocated to the table.

If we then ask SQL to retrieve all data from the table, the data will be retrieved in physical extent sequence, not in allocated extent sequence.  This could easily result in data that has been inserted in an extent that has been allocated recently being presented earlier than data that is older but which is located in an extent that is physically further away in the file.

The same principle applies to data that is retrieved by an index, regardless of if the index is clustered or unclustered.  All that a clustered index guarantees is that the sequence of rows stored in the table matches the index definition.  It does not grarantee that the sequence of extents making up the table or the index will be stored in a physical order that matches the index definition.  Therefore if you have a large and growing table with a cluster index on colum TABID, and that all tables in the database are subject to insert and delete activity, you can legitimately get a non-ordered result set with a SELECT TABID FROM TABLE.

The likelyhood of result sets being non-ordered greatly increases if you have multiple files in the file group, but can occur with just a single file filegroup.

The only way to guarantee the sequence of rows in a result set is to use a ORDER BY clause.  This forces SQL to return the rows to you in the sequence you desire.  SQL will decide itself if it is more efficient to follow the sequence of an index to get your result set, or if it is more efficient to collect all rows in a scan of the database then sort them before returning them to you.

If you do not specify an ORDER BY, then SQL will assume you do not care about the sequence in the result set, and will get your data in the fastest possible way.



Original author: SQL Server FineBuild 1-click install and best practice configuration of SQL Server 2014, 2012, 2008 R2, 2008 and 2005. 28 July 2014: now over 30,000 downloads.
Disclaimer: All information provided is a personal opinion that may not match reality.
Concept: "Pizza Apartheid" - the discrimination that separates those who earn enough in one day to buy a pizza if they want one, from those who can not.
Post #183687
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse