guaranteed unique?

  • Does anyone how unique this is? How many times I would have to run it before I found a duplicate.

    select ABS(CAST(CAST((newid()) AS VARBINARY(5)) AS Bigint))

    www.sql-library.com[/url]

  • would increase the varbinary like so

    select ABS(CAST(CAST((newid()) AS VARBINARY(8000)) AS Bigint))

    help?

    www.sql-library.com[/url]

  • Jules Bonnot (7/19/2011)


    Does anyone how unique this is? How many times I would have to run it before I found a duplicate.

    select ABS(CAST(CAST((newid()) AS VARBINARY(5)) AS Bigint))

    Create a table with PK on it.

    Run inserts into that table untill it fails.

    I'm pretty sure it'll fail but it "should" be a rare occurance.

  • What exactly are you trying to achieve?

    Are you attempting to come up with some sort of key generator?

    Whereas a uniqueidentifier is 16 bytes a bigint is 8 but random keys are still going to cause fragmentation issues.

  • Go here and calculate the probability of it happening

    http://www.vpgenius.com/tools/combin.aspx

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Jules Bonnot (7/19/2011)


    Does anyone how unique this is? How many times I would have to run it before I found a duplicate.

    select ABS(CAST(CAST((newid()) AS VARBINARY(5)) AS Bigint))

    I did the analysis, once. First, it's a pseudorandom number and it won't repeat on the same machine until all of the numbers have been used.

    To give you an idea of how big the number really is, if one "unit" of the number equalled 1 statute mile, there would be enough numbers to stretch across 17 quadrillion Milky Way Galaxies.

    That, notwithstanding, there is no guarantee that the numbers generated from 2 or more machines are unique... and least not anymore because they changed from Type 1 GUID's (partially based on the Mac Address) to Type 4 GUID's which are nothing more than random numbers with a couple of constant columns. A friend of mine from UPS tells me the tale of when two machines started generating more than the chance intersection of two such random numbers... it made a real mess.

    Bottom line? Even though the chances of two machines generating the same number is incredibly remote, make sure you have a UNIQUE constraint on columns that contain GUID information because they not guaranteed to be unique.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • If your intent is to generate unique integer values for use as a surrogate keys, then I'd reccomend IDENTITY over some function that returns a random number. An identity value will be sequential, which is what you want if the key is also clustered. If there are multiple tables or federated servers involved, and you don't want key values repeated across, then you can solve that by defining seed values and check constraints on each.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • If you accept the premise that your surrogate keys are going to be BIGINT values then you could try the following

    The right most 6 characters are just a sequential number

    The 7th character is a key generator number (so you can have a max of10 key generators)

    The left most characters are milliseconds since 1st July 2011.

    Providing no one key generator wants to push in more than 1 million records per millisecond that should be good for 40 years.

  • David.Poole (7/25/2011)


    If you accept the premise that your surrogate keys are going to be BIGINT values then you could try the following

    The right most 6 characters are just a sequential number

    The 7th character is a key generator number (so you can have a max of10 key generators)

    The left most characters are milliseconds since 1st July 2011.

    Providing no one key generator wants to push in more than 1 million records per millisecond that should be good for 40 years.

    Interesting idea. That seems like a nice way to create some unique numbers, though I slightly worry about performance here. Curious how this might perform across a busy system.

Viewing 9 posts - 1 through 8 (of 8 total)

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