通过 SQL 和 C# 创建分区键

Create key via SQL and C# for partition key

我有一组数据,它有 3 个级别的层次结构。每个级别都有一个名称。

我正在考虑将所有这些名称组合成一个字符串,然后创建一个数字散列,该散列可用作服务结构有状态服务的散列键。

我在网上看到很多关于使用键查找数据的信息,但我不确定如何以有效的方式实际创建它们。

理想情况下,我想要一个可以在 SQL Server 2017 和 C# 中快速轻松生成的散列。

任何人都可以指出正确的方向吗?

保罗

SF团队advice将为此使用FNV-1哈希算法。

Select a hash algorithm An important part of hashing is selecting your hash algorithm. A consideration is whether the goal is to group similar keys near each other (locality sensitive hashing)--or if activity should be distributed broadly across all partitions (distribution hashing), which is more common.

The characteristics of a good distribution hashing algorithm are that it is easy to compute, it has few collisions, and it distributes the keys evenly. A good example of an efficient hash algorithm is the FNV-1 hash algorithm.

A good resource for general hash code algorithm choices is the Wikipedia page on hash functions.

此示例中的 C# 实现 here:

public long HashString(string input)
{
    input = input.ToUpperInvariant();
    var value = Encoding.UTF8.GetBytes(input);
    ulong hash = 14695981039346656037;
    unchecked
    {
       for (int i = 0; i < value.Length; ++i)
       {
          hash ^= value[i];
          hash *= 1099511628211;
       }        
       return (long)hash;
    }
}

删除 ToUpperInvariant 以使其区分大小写。