Home  >  Q&A  >  body text

mysql - 订单规则id怎么生成?

最近在考虑订单id怎么生成。

全数字但是不要太长,同时orderId需要唯一。

简单的id自增长肯定不行,不仅位数不够,还会暴露业务量。

最近我考虑:

简单的时间戳+随机数+流水号计数器。

不知道大家有什么好的想法。

迷茫迷茫2721 days ago420

reply all(7)I'll reply

  • 迷茫

    迷茫2017-04-17 12:59:33

    It mainly depends on your business volume.

    Generally there are several ways:
    1. User ID + year, month, day, hour, minute and second + random number + serial number. The number of digits in the random number and serial number is set according to the business volume, but within seconds, two 4's are enough. If your business volume is small, neither random numbers nor serial numbers are used.
    Such as: 0424 201505291832 0001 2455

    2. User id + md5 (user id + timestamp). However, using md5() for message digest may cause duplication. Although 36^32 seems large, there is no guarantee that the digested data will not be repeated. Or you can modify it based on md5().
    For example: 0424 + md5(0424 201505291832), (no space in the middle) the generated summary is: 0424e13c2fe2f569da04b2aa411980dbfa28

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 12:59:33

    It’s usually timestamp + random number, and the amount per day won’t be too big

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 12:59:33

    Our order system uses "year, month, day, hour, minute and second" + 4-digit random number. In this way, theoretically, up to 999 orders can be created in 1 second. Of course, after the order number is generated, it needs to be checked for duplication.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 12:59:33

    Simple timestamp + random number + serial number counter + classification id

    The classification id refers to the need to write a mapping table in the database
    For example
    1-》Some kind of order
    2-》Another kind of order
    3-》Another kind of order

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 12:59:33

    What about UUID?

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 12:59:33

    Can I use a dialer?

    reply
    0
  • 阿神

    阿神2017-04-17 12:59:33

    User ID + simple timestamp (accurate to the hour) + (4-digit) serial number counter. For example, it is 6:00 on June 10, 2016, and then add the 4-digit serial number count (on the ID card) 4 digits should be enough, I personally think random numbers are not very useful), write it as 0424-2016-06-10-06-0001.

    reply
    0
  • Cancelreply