.net - 为什么 Azure Elastic Sc​​ale 不支持字符串数据类型作为分片键?

标签 .net azure azure-sql-database

来自 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 Sc​​ale 不支持字符串数据类型作为分片键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46316170/

相关文章:

c# - 如何在Windows任务栏中制作一个文本框?

c# - 如何使用 Html Agility Pack 使请求超时

python - 如何解决 Azure 文本分析的导入错误

sql-server - SQL Azure 与 SQL Server 有何不同?

sql-server - 用户分配的托管标识的 SQL Azure 连接错误 'Login failed for user'

c# - 如何找出另一个程序集的 ProductName 属性?

multithreading - 使用 ThreadPool.GetAvailableThreads 来限制服务执行的工作量是否可以接受?

Azure Pipeline无法构建Azure Function App

sql-server - 触发器 - 是否需要 BEGIN/COMMIT TRAN

c# - 什么时候可以使用 XML 文件保存信息?