How many bytes are unique enough for twitter?

自作多情 提交于 2019-12-12 03:43:32

问题


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);

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


回答1:


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.

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.

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




回答2:


Use PHP's uniqid(), with an added entropy factor. That'll give you plenty of room.




回答3:


Assuming that openssl_random_pseudo_bytes may generate every possible value, N bytes will give you 2 ^ (N * 8) distinct values. For 12 bytes this is 7.923 * 10^28




回答4:


use MySQL UUID

insert into `database`(`unique`,`data`) values(UUID(),'Test');

If your not using MySQL search google for UUID (Database Name) and it will give you an option

Source Wikipedia

In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%



来源:https://stackoverflow.com/questions/12479681/how-many-bytes-are-unique-enough-for-twitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!