How to generate unique id in MySQL?

前端 未结 16 2645
生来不讨喜
生来不讨喜 2020-11-29 03:17

I\'m programming a script using PHP and MySQL and I want to get a unique id (consisting of a string: capitals and small letters with numbers) like: gHYtUUi5b.

16条回答
  •  时光说笑
    2020-11-29 03:49

    You could use Twitter's snowflake.

    In short, it generates a unique id based on time, server id and a sequence. It generates a 64-bit value so it is pretty small and it fits in an INT64. It also allows for sorting values correctly.

    https://developer.twitter.com/en/docs/basics/twitter-ids

    In sum, it allows multiple servers, highly concurrency, sorting value and all of them in 64 bits.

    Here it is the implementation for MySQL

    https://github.com/EFTEC/snowflake-mysql

    It consists of a function and a table.

提交回复
热议问题