What is a proper length of random code used as mys

2019-09-08 08:23发布

问题:

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.

回答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



回答2:

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



标签: php mysql random