使用彈性資料庫工具加入分區

適用於:Azure SQL Database

為新的範圍或索引鍵加入分區

對於已經存在的分區對應,應用程式通常需要新增分區,以處理預期來自新的索引鍵或索引鍵範圍的資料。 例如,以租用戶識別碼分區的應用程式,可能需要為新的租用戶建立新的分區,或者,每月分區的資料可能需要在每個新月份開始之前佈建新的分區。

如果索引鍵值的新範圍尚不屬於現有的對應,您可以新增分區並將新的索引鍵或範圍與該分區產生關聯。

範例:將分區及其範圍加入至現有的分區對應

這個範例會使用 TryGetShard (Java.NET)、CreateShard (Java.NET)、CreateRangeMapping (Java.NET) 方法,並建立 ShardLocation (Java.NET) 類別的執行個體。 在下列範例中,已建立名為 sample_shard_2 的資料庫及其中的所有必要結構描述物件,以保存範圍 [300, 400)。

// sm is a RangeShardMap object.
// Add a new shard to hold the range being added.
Shard shard2 = null;

if (!sm.TryGetShard(new ShardLocation(shardServer, "sample_shard_2"),out shard2))
{
    shard2 = sm.CreateShard(new ShardLocation(shardServer, "sample_shard_2"));  
}

// Create the mapping and associate it with the new shard
sm.CreateRangeMapping(new RangeMappingCreationInfo<long>
                            (new Range<long>(300, 400), shard2, MappingStatus.Online));

為現有範圍的空白部分加入分區

在某些情況下,您可能已將某個範圍對應至分區,並在其某些部分填入資料,但現在您想讓後續的資料導向至不同的分區。 例如,您可以依日期範圍分區,並且已配置 50 天給某個分區,但在第 24 天,您想要將後續資料分配到不同的分區。 彈性資料庫分割合併工具可以執行這項作業,但如果不需要移動資料 (例如,尚不存在日期範圍 [25, 50) 的資料,亦即第 25 天 (含) 到第 50 天 (不含),您可以直接使用分區對應管理 API 完全執行此作業。

範例:分割範圍並將空白部分指派給新增的分區

已建立名為 "sample_shard_2" 的資料庫,及其中所有必要的結構描述物件。

// sm is a RangeShardMap object.
// Add a new shard to hold the range we will move
Shard shard2 = null;

if (!sm.TryGetShard(new ShardLocation(shardServer, "sample_shard_2"),out shard2))
{
    shard2 = sm.CreateShard(new ShardLocation(shardServer,"sample_shard_2"));  
}

// Split the Range holding Key 25
sm.SplitMapping(sm.GetMappingForKey(25), 25);

// Map new range holding [25-50) to different shard:
// first take existing mapping offline
sm.MarkMappingOffline(sm.GetMappingForKey(25));

// now map while offline to a different shard and take online
RangeMappingUpdate upd = new RangeMappingUpdate();
upd.Shard = shard2;
sm.MarkMappingOnline(sm.UpdateMapping(sm.GetMappingForKey(25), upd));

重要

只有在您確定更新對應的範圍是空白時,才可使用這項技術。 上述方法並不會檢查要移動的資料範圍,因此您最好在程式碼中納入檢查。 如果資料列存在於要移動的範圍中,則實際的資料分佈將不會符合更新的分區對應。 在這些情況下,請改用 分割合併工具 執行作業。

其他資源

尚未使用彈性資料庫工具? 請參閱使用者入門指南。 如有疑問,請在 SQL Database 的 Microsoft Q&A 問題頁面上與我們連絡。如有功能要求,請在 SQL Database 意見反應論壇中新增想法或投票支持現有的想法。