0

I assign each mysql row with a random code. Only alphanumeric lowercase characters and numbers are used.

These random codes are used for SELECT and JOIN queries.

Most often I use length of 24 characters which gives roughly 2.2x10^37 available combinations.

What is the perfect random code length that does not require checking for duplicates while being certain no collisions will occur but also keeping query result time to minimum?

Note: On big tables I use INDEX for the random code column of length 24.

John Doe
  • 983
  • 4
  • 14
  • 27

2 Answers2

1

The nature of a RANDOM code is that you can never be certain it will not be duplicated. You can guarantee there wont be a duplicate if you use a sequential number & if you are doing that you might as well use the AUTO INCREMENT option with a sufficiently large integer.

http://dilbert.com/strip/2001-10-25

PaulF
  • 6,673
  • 2
  • 18
  • 29
  • Perhaps I worry too much, yet as I commented to @kittykittybangbang I am not fond of passing incremental IDs in URLs and API requests, even if the worst thing that could happen was someone browsing through user_ID 1 → 1000 simply by increasing the URL value. Do I worry too much? – John Doe Jul 09 '15 at 17:09
  • Lol no, you don't worry too much -- but you ought maybe to focus your energies on securing your site/web app/etc. via other means. Relying on the randomness of your IDs exclusively isn't secure; and on the flip side, enforcing random IDs instead of using incremental IDs adds a negligible amount of security anyway. – kittykittybangbang Jul 09 '15 at 17:16
  • @kittykittybangbang So how do I fix this issue with incremental user_IDs given that I dont use random codes? – John Doe Jul 09 '15 at 17:31
  • Sounds like it's time for a new SO question. :) – kittykittybangbang Jul 09 '15 at 18:05
  • @kittykittybangbang - ironically /questions/31326264 which is incrementally assigned ;) I hope my current solution will not be dominant, so lets see. – John Doe Jul 09 '15 at 19:12
0

It is not possible to generate a random code which might not be duplicated, therefore you must always check for duplicates and there is no "perfect random code length".