When To Use the Order By Clause

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/g

    Blog: Gulappa

  • 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

  • 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.

  • 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... 

  • 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: https://github.com/SQL-FineBuild/Common/wiki/ 1-click install and best practice configuration of SQL Server 2019, 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005.

    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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply