Coldairarrow / EFCore.Sharding

Database Sharding For EFCore
Apache License 2.0
694 stars 143 forks source link

取模分表算法存在问题 #75

Closed jing8956 closed 3 years ago

jing8956 commented 3 years ago

https://github.com/Coldairarrow/EFCore.Sharding/blob/2bfd589038e611fd1367ac284d84fe8b2c7cf5c0/src/EFCore.Sharding/Primitives/ShardingRule.cs#L12-L32

如果使用任何非int非long字段(例如uint ulong string等)将会通过这种方式计算 Math.Abs(fieldValue.GetHashCode()) % Mod

https://docs.microsoft.com/zh-CN/dotnet/api/system.string.gethashcode?view=net-5.0#System_String_GetHashCode 中有说明

哈希代码本身不一定是稳定的。 相同字符串的哈希代码可跨 .net 实现、跨 .NET 版本和跨 .NET 平台 (例如,.NET 的单个版本32位和64位) 。 在某些情况下,它们甚至不同于应用程序域。 这意味着,同一程序的两次后续运行可能返回不同的哈希代码。 因此,哈希代码决不能在创建它们的应用程序域的外部使用,它们永远不应用作集合中的键字段,它们永远不会保留。

此算法导致包含分表谓词的查询概率性无法查到。

Coldairarrow commented 3 years ago

已修复