PHP short unique ID generation using auto_increment?

后端 未结 8 1500
自闭症患者
自闭症患者 2021-02-04 17:14

I would like to generate a short, unique ID without having to check for collisions.

I currently do something like this, but the ID I currently generate is random and che

8条回答
  •  广开言路
    2021-02-04 18:16

    An MD5 of an incrementing number should be fine, but I worry that if you're truncating your MD5 (which is normally 128 bits) down to 5-8 characters, you will almost certainly be damaging it's capability to act as a unique signature...

    Completely true. Especially if you reach your 80% collision chance a truncated MD5 will be as good as any random number to guarantee uniqueness by itself, i.e. worthless.

    But since you're using a database anyway, why not just use a UNIQUE INDEX ? This way the uniquness check is done (in a much more efficient way than using a loop) by MySQL itself. Just try to do the INSERT with your MD5-generated key, and if it fails, try again...

提交回复
热议问题