推特有多少字节足够独特? [英] How many bytes are unique enough for twitter?

查看:47
本文介绍了推特有多少字节足够独特?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望我的数据库 id 是连续的,所以我试图用这个代码生成 uid:

I don't want my database id's to be sequential, so I'm trying to generate uids with this code:

$bin = openssl_random_pseudo_bytes(12);
$hex = bin2hex($bin);
return base_convert($hex, 16, 36);

我的问题是:我需要多少字节才能使 ID 足够独特以处理大量记录(如 Twitter)?

My question is: how many bytes would i need to make the ids unique enough to handle large amounts of records (like twitter)?

推荐答案

您可能会考虑类似 tinyurl 和其他缩短服务的工作方式.我使用了类似的技术,这保证了唯一性,直到所有组合都用完为止.所以基本上你选择一个字母表,以及你想要多少个字符作为长度.假设我们使用字母数字、大写和小写,那么字母表中有 62 个字符,让我们每个代码 5 个字符.那是 62^5 = 916,132,832 种组合.

You might considering something like the way tinyurl and other shortening services work. I've used similar techniques, which guarantees uniqueness until all combinations are exhausted. So basically you choose an alphabet, and how many characters you want as a length. Let's say we use alphanumeric, upper and lower, so that's 62 characters in the alphabet, and let's do 5 characters per code. That's 62^5 = 916,132,832 combinations.

您从序列数据库 ID 开始,然后乘以某个质数(选择一个相当大的数,例如 2097593).您要做的就是将其乘以您的数据库 ID,如果超过 62^5,请确保环绕,然后根据您选择的字母表将该数字转换为 base-62.

You start with your sequential database ID and you multiply that be some prime number (choose one that's fairly large, like 2097593). All you do is multiply that by your database ID, making sure to wrap around if you exceed 62^5, and then convert that number to base-62 as per your chosen alphabet.

这使得每个代码看起来都相当独特,但因为我们使用质数,所以在我们已经使用完所有代码之前,我们保证不会两次命中同一个数字.而且很短.

This makes each code look fairly unique, yet because we use a prime number, we're guaranteed not to hit the same number twice until we've used all codes already. And it's very short.

如果长度不是问题,您也可以使用带有较小字母表的较长键.

You can use longer keys with a smaller alphabet, too, if length isn't a concern.

这是我提出的相同问题:Tinyurl-style unique code:防止碰撞的潜在算法

Here's a question I asked along the same lines: Tinyurl-style unique code: potential algorithm to prevent collisions

这篇关于推特有多少字节足够独特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆