将用户账户分配到N张表
有数百万个用户帐户,我想将它们的数据分发到数据库的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以获取前 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.
目前尚不清楚您是在寻找字符串哈希函数,还是基于字符串的分区方法。
好的字符串哈希函数使用每个字符,并考虑字符的位置。 例如,djb2 使用类似这样的东西(伪代码):
无论您的散列是什么,使用模运算按表的数量进行分区:
我建议使用数据库的内置分区功能(如果有的话)。
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):
Whatever your hash is, partition by the number of tables using a modulo operation:
I recommend using the built-in partitioning capability of your database, if it has one.