Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple question

  • 07-01-2004 11:01pm
    #1
    Banned (with Prison Access) Posts: 16,659 ✭✭✭✭


    Christ, I'm only 31 and I'm forgetting basic maths already...

    I want to use an alphanumeric string ([A-Za-z0-9] / 26+26+10=62) as a unique identifier in a database table. I want to cater for the possibility of millions of rows, plus I want to leave plenty of headroom to try and avoid multiple queries when collisions occur. How long should the string be?

    adam


Comments

  • Registered Users, Registered Users 2 Posts: 78,580 ✭✭✭✭Victor


    Originally posted by dahamsta
    How long should the string be?
    Well how long is a string? ;)

    Try adding the date into the identifier and you can go to infinity (and BEYOND!).


  • Moderators, Social & Fun Moderators Posts: 10,501 Mod ✭✭✭✭ecksor


    Originally posted by dahamsta
    I want to use an alphanumeric string ([A-Za-z0-9] / 26+26+10=62) as a unique identifier in a database table. I want to cater for the possibility of millions of rows, plus I want to leave plenty of headroom to try and avoid multiple queries when collisions occur. How long should the string be?

    You're looking for powers of 62, in the same way that our normal number base increases by powers of 10 each time you increment the length of the string that we conventionally use to represent natural numbers.
    62^4 =  14,776,336
    62^5 = 916,132,832
    

    4 or 5 characters I suppose.

    How will collisions occur if it is a unique constraint?


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    Originally posted by Victor
    Try adding the date into the identifier and you can go to infinity (and BEYOND!).
    I'm aiming for the shortest string possible, which is why I was looking at using alphanumeric characters. I'd have to use microtime() in this case because of the risk of two inserts in the same second, which is the opposite direction. Thanks anyway. And less of the bad jokes please. :)
    Originally posted by ecksor
    You're looking for powers of 62, in the same way that our normal number base increases by powers of 10 each time you increment the length of the string that we conventionally use to represent natural numbers.

    62^4 =  14,776,336
    62^5 = 916,132,832
    
    4 or 5 characters I suppose.
    That's the stuff ecksor, thanks very much! I was thinking along the lines of 62 x 62 x $chars and couldn't figure out why the number looked so small. But for the first example above it would be 62 x 62 x 62 x 62 x $chars, right?

    How will collisions occur if it is a unique constraint?

    I'll be generating a string for each record and no doubt the occasional duplicate will occur, so if I try an INSERT and get an error back I'll need to try again. And of course that's more likely to happen if the combinations are limited. I reckon 15m combinations will do fine for the moment though. :)

    Thanks again,
    adam


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    Originally posted by dahamsta
    But for the first example above it would be 62 x 62 x 62 x 62 x $chars, right?
    Gah! Just 62 x 62 x 62 x 62, right?!


  • Moderators, Social & Fun Moderators Posts: 10,501 Mod ✭✭✭✭ecksor


    Yep, 62^4 = 62 * 62 * 62 * 62


  • Advertisement
  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    Cool. In the end I went with [a-z] ^ 6, because the key will be going in a URL and all lowercase looks neater. 31 million combinations should keep me going till at least the end of the week. :)

    Thanks again,
    adam


  • Moderators, Social & Fun Moderators Posts: 10,501 Mod ✭✭✭✭ecksor


    It's a bit more than 308 million according to my calculator.


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    Missed a digit. Windows calculator.


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Adam using Windows, has the world gone mad? Get back to your "KDE" mista*





    I'm only joking ya know :)


Advertisement