怎样选择 uid 生成策略?

2016-06-09 18:41:38 +08:00
 honmaple

我想问一下大家的 uid 是怎么生成的,uuid 有 32 位太长,目前的策略是时间戳+用户 ID+两位随机数

def make_uid():
    a = str(int(time()))
    b = str(1).zfill(6)
    c = str(randint(10, 99))
    return a + b + c
a = make_uid()
print(a)

但看了一下,有 18 位,感觉还是有些长,那么如何生成 10 位以内的不重复 uid 呢?

7303 次点击
所在节点    Python
38 条回复
honmaple
2016-06-09 22:24:05 +08:00
@ethego 能解释一下吗
Syc
2016-06-09 22:26:27 +08:00
@honmaple 只要把索引和缓存做好没有什么是不可能的。
ethego
2016-06-09 22:27:29 +08:00
zhangxiao
2016-06-09 22:30:57 +08:00
@honmaple 如果你目前 18 位是 base 10 的可以生成后转 base 36
ihuotui
2016-06-09 22:33:57 +08:00
参考淘宝订单 id 规则。哈哈。
hxsf
2016-06-09 22:42:16 +08:00
r#18 @honmaple 唯一一种可以在信息量减少的条件下保证不重复的办法就是查重。。。
tabris17
2016-06-09 22:45:32 +08:00
自增 ID + Skip32 加密
zzzhan
2016-06-09 22:51:53 +08:00
优雅简洁地实现短 ID http://www.jianshu.com/p/ee469e1e1f9d
honmaple
2016-06-09 22:52:13 +08:00
@ethego Thank you,正在研读
honmaple
2016-06-09 22:58:23 +08:00
julyclyde
2016-06-09 23:59:08 +08:00
我们公司早年是预先生成一堆,在一个单独保存 uid 的表里
用的时候取一个出来
jsq2627
2016-06-10 01:42:16 +08:00
@honmaple
@ethego
在数据库中应用 UUID/GUID 要关注是否为 primary key 的问题。
很多数据库系统是把 primary key 作为 clustered index 的, UUID/GUID 这类比较长的 ID 不适合作为 clustered index ,但是很适合作为 primary key 。
如果数据库系统支持 clustered index 和 primary key 分离定义的话,应该用 UUID/GUID 列作为 primary key , auto increment 列作为 clustered index ,如果不支持分离,那最好还是别用 UUID/GUID 作为 primary key 。
ethego
2016-06-10 01:48:43 +08:00
@jsq2627 http://inessential.com/2014/04/15/more_on_uuids_and_clustered_indexes 有一定的道理,但是不要盲目优化,确定 uuid 真的在当前业务下会引起足够的性能损失再做决定。
ryd994
2016-06-10 02:25:45 +08:00
谁说 uuid 比 20 位字符长的?
uuid 是 128 位整数, 32 位 hex 字符
128 vs 32*8=256

mysql 官方文档: http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_uuid
“ UUID() returns a value that conforms to UUID version 1 as described in RFC 4122. The value is a 128-bit number represented as a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format ”
9hills
2016-06-10 09:03:10 +08:00
没有 32B ,如果使用常用字符表示的话, uuid 20B 足够了,毕竟只有 128bit
julyclyde
2016-06-10 10:16:17 +08:00
@jsq2627 说的很对, clustered index 很重要
breeswish
2016-06-10 13:30:33 +08:00
直接 uuid 呀
practicer
2016-06-14 17:53:55 +08:00
我们公司用于跟踪用户行为的"uid"是 32 位长。能不能直接用标准库里的 hashlib 呢?
import hashlib
m = hashlib.md5()
m.update('Obama')
m.hexdigest()

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/284656

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX