• subscribe
June 01, 2002 12:00 AM

Just in Time

SQL Server Pro
InstantDoc ID #25174

H.H. Munro, writing under the pseudonym of Saki in late 19th-century Britain, opined, "A little inaccuracy sometimes saves tons of explanation." Unfortunately, a discussion about datetime data's inaccuracy is necessary, even though the data is only a little inaccurate. Although a datetime data type's time can be precise to milliseconds, SQL Server can't represent every millisecond. In fact, within the millisecond value of the datetime data type, SQL Server rounds to within 0.00333 seconds, a period of roughly 3.33 milliseconds. After you run Web Listing A (available at http://www.tsqlsolutions.com, InstantDoc ID 25174) to create and populate a sample table, look at the output that the listing's SELECT statement returns. Notice that the values that the query returns aren't identical to the values inserted. This example is significant because it shows that returning duplicates is possible—even when explicit values are supplied. Additionally, when database inserts occur rapidly (e.g., through multiple clients or testing with fast loops), you're even more likely to yield duplicate values. Moreover, you probably can't use a datetime-based column as a unique identifier. To further prove this point, you can download and run this column's DateTimeDataExamples.sql commented script file from http://www.tsqlsolutions.com, InstantDoc ID 25173. This script includes the 5-second loop that Web Listing B uses to rapidly insert data into the TestDates table. The script also uses the getdate() system function to input the current date and time value. After the loop inserts the data, the SELECT query in Web Listing B shows the total number of rows added, the number of distinct values in the datetime column, and the number of distinct values in the smalldatetime column. Notice that the datetime column has few unique values, and the smalldatetime column has only one or two unique values. Also, when you review the data, you'll immediately see that the smalldatetime data is rounded to the nearest minute.



ARTICLE TOOLS

Comments
  • Marcos
    3 years ago
    Mar 18, 2009

    Again, this information is available on BOL. Kimberly, please, stop writing.

  • Phil
    5 years ago
    Apr 12, 2007

    I know this is an older article, but I wanted to know what anyone thought about using a datetime column as a clustered index.

    I realize the overhead of using a non-unique field, but the only other key fields are all GUIDs (using NEWID() because this was originally designed for SQL 2000).

    The GUIDs are have unique indexes on them and are set up as Foreign Keys in other tables.

    The apps using this design is about to be rewritten so I've been asked to help redesign the schema. My instinct says to add an Identity column and make that Primary Key and the Clustered Index, but I'm at odds about whether I should stick with the current design.

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