将用户账户分配到N张表

发布于 2024-07-19 03:07:43 字数 228 浏览 5 评论 0原文

有数百万个用户帐户,我想将它们的数据分发到数据库的 N 个表(user_1,user_2,...,user_N)中。 用户帐户由3~8个字符组成。 所以,我想要一个返回表后缀的函数,例如

  int getTableSuffix(String userAccount);

结果是从 1 到 N 的均匀分布。

您知道这项工作有什么便宜的哈希算法吗?

There are millions of user accounts, and I want to distribute their data into N tables(user_1, user_2,..., user_N) of a database. User accounts are comprised of 3~8 characters.
So, I want a function that returns table suffix like

  int getTableSuffix(String userAccount);

The result is a uniform distribution from 1 to N.

Do you know any cheap hash algorithm for this job?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-07-26 03:07:44

您可以获取前 1-3 个字符的 ascii 值并找到这些值的乘积以返回您的号码。

或者,您实际上可以使用这些字符作为表前缀,例如。 Users_AA、Users_AB 等。

但是,您使用什么数据库来存储这些数据? 在大多数现代数据库中,您应该不需要创建多个表来存储相同的数据。 即使有数百万条记录。 表上良好的索引应该足以解决您可能遇到的任何性能问题。

You could take the ascii value of the first 1-3 characters and find a product of those in order to return your number.

Alternatively, you could actually use the characters as your table prefix, eg. Users_AA, Users_AB, etc.

However, what database are you using for this data? In most modern databases you should have no need to create multiple tables to store the same data. Even with millions of records. Good indexing on your table should be more than enough to solve any performance issues you may have.

动次打次papapa 2024-07-26 03:07:44

目前尚不清楚您是在寻找字符串哈希函数,还是基于字符串的分区方法。

好的字符串哈希函数使用每个字符,并考虑字符的位置。 例如,djb2 使用类似这样的东西(伪代码):

hash = 5381
foreach (ch in str) 
  hash = hash * 33 + ch

无论您的散列是什么,使用模运算按表的数量进行分区:

table = hash % count

我建议使用数据库的内置分区功能(如果有的话)。

Its not clear whether you are looking for a string hash function, or a method for partitioning based on strings.

A good string hash function uses each character, and accounts for the position of the characters. For example, djb2 uses something like this (pseudo-code):

hash = 5381
foreach (ch in str) 
  hash = hash * 33 + ch

Whatever your hash is, partition by the number of tables using a modulo operation:

table = hash % count

I recommend using the built-in partitioning capability of your database, if it has one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文