来自 Microsoft Azure 文档:
Elastic Scale support the following .Net framework types as sharding keys:
- integer
- long
- guid
- byte[]
- datetime
- timespan
- datetimeoffset
为什么不支持字符串?
最佳答案
正如上面的海报所暗示的,不受约束的字符串是在数据库中索引内容的低效方法(分片映射存储在 SQL 数据库中)。在底层,弹性数据库客户端库 (EDCL) 将所有支持的类型规范化为 byte[] 并在数据库的分片映射中使用它。如果将字符串转换为受支持的类型之一,则可以有效地使用字符串作为键。
您可以在 WingtipSaaS sample application 中看到此操作已完成,它使用 field 名称作为键。我们选择将名称转换为整数键(出于演示和探索目的,更容易在代码中跟踪整数值),但可能会停在 byte[] 处。该转换是使用名称的 UTF8 编码的 MD5 哈希值完成的。下面的 PowerShell 代码来自 Get-TenantKey 函数,由多个管理脚本使用。您可以在客户端应用程序中找到 C# 等效项。示例在这里:( https://github.com/Microsoft/WingtipSaaS )。以下函数位于 ...\Learning Modules\Common\CatalogAndDatabaseManagement.psm 模块中。
$normalizedTenantName = $TenantName.Replace(' ', '').ToLower()
# Produce utf8 encoding of tenant name
$utf8 = New-Object System.Text.UTF8Encoding
$tenantNameBytes = $utf8.GetBytes($normalizedTenantName)
# Produce the md5 hash which reduces the size
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$tenantHashBytes = $md5.ComputeHash($tenantNameBytes)
# Convert to integer for use as the key in the catalog
$tenantKey = [bitconverter]::ToInt32($tenantHashBytes,0)
关于.net - 为什么 Azure Elastic Scale 不支持字符串数据类型作为分片键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46316170/