• subscribe
October 24, 2001 12:00 AM

Sort Order with the Uniqueidentifier Data Type

SQL Server Pro
InstantDoc ID #22516
Downloads
22516.zip

I just upgraded from SQL Server 6.5 to SQL Server 2000 and have begun to experiment with the uniqueidentifier data type. I applied a clustered index to a table called MyTable on a column defined as uniqueidentifier and ran the statement

SELECT * FROM MyTable

However, the data isn't sorted in the order of my clustered index column as I expected. Did I do something wrong?

The SQL script that Listing 4, page 17, shows creates a test environment you can use to explore this problem. In SQL Server 6.5, issuing a simple SELECT from a table would return data sorted in the order of the clustered index—even if you didn't use an ORDER BY clause. But in SQL Server 2000 and 7.0, you must include an ORDER BY clause for SQL Server to return data sorted in the order of the clustered index.

In this example, you're trying to sort by the RowGUID column, which is defined with a uniqueidentifier data type. However, you still might not get the results you expect, even when you include the appropriate ORDER BY clause. For example, run the following query against the test environment you created earlier, and you'll see that the data doesn't seem to come back sorted by the RowGUID column:

SELECT * FROM NoOrderByClauseOnGUID ORDER BY RowGUID

In fact, the data is sorted; it's just not sorted the way you expect. SQL Server displays globally unique identifier (GUID) data as a series of alphanumeric strings, but the uniqueidentifier data type is equivalent to the binary(16) data type. The GUID value you see is simply the hexadecimal representation of the binary value. So the data is sorted by this hex representation, not alphabetically.



ARTICLE TOOLS

Comments
  • Rex
    3 years ago
    Dec 08, 2009

    no solution to sort correctly. (order created)

  • Dan Frangiamore
    8 years ago
    Apr 23, 2004

    Un-friggin'-believable! Beat my head for hours!

  • Jim Peoples
    10 years ago
    Feb 06, 2002

    We are using GUIDs as our primary keys on user defined tables. As the number of records and volume of transactions increases (inserts), we suspect that we are getting performance problems related to the maintenance of the clustered indexes... should we be using the natural (multiple columns) key or an identity column instead?

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...