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


When To Use the Order By Clause


When To Use the Order By Clause

Author
Message
Vijaya Kumar Gulappa
Vijaya Kumar Gulappa
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 87
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/g

Blog:
Gulappa
Sql DBA
Sql DBA
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 24
see: http://www.sqlservercentral.com/forums/messages.aspx?forumid=172



newdongkui
newdongkui
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
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


vadba
vadba
SSC Eights!
SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)

Group: General Forum Members
Points: 993 Visits: 408

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.





blom0344
blom0344
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 330
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...
EdVassie
EdVassie
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3150 Visits: 3819

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 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005. 1 Dec 2016: now over 39,000 downloads.
Disclaimer: All information provided is a personal opinion that may not match reality.
Quote: "When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist." - Archbishop Hélder Câmara
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