共用方式為


使用資料表和索引資料分割

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse Analytics

您可以使用資料分割資料表和索引 所提供的 儲存演算法來儲存資料。 分割作業可讓大型資料表和索引更容易管理及擴充。

索引和資料表資料分割

此功能可讓索引和資料表資料分散到分割區中的多個檔案群組。 資料分割函數會定義資料表或索引的資料列如何根據特定資料行的值對應至一組資料分割,稱為資料分割資料行。 資料分割配置會將資料分割函式所指定的每個分割區對應至檔案群組。 這可讓您開發封存策略,讓資料表能夠跨檔案群組進行調整,因此實體裝置。

Database物件包含 物件的集合 PartitionFunction ,這些物件表示已實作的資料分割函式,以及描述資料如何對應至檔案群組的物件 PartitionScheme 集合。

每個 TableIndex 物件都會指定它在 屬性中使用的 PartitionScheme 資料分割配置,並指定 中的資料 PartitionSchemeParameterCollection 行。

範例

針對下列程式碼範例,您必須選取程式設計環境、程式設計範本和程式設計語言,才能建立您的應用程式。 如需詳細資訊,請參閱 在 Visual Studio .NET 中建立 Visual C# SMO 專案。

在 Visual C 中設定資料表的資料分割配置#

程式碼範例示範如何在 AdventureWorks2022 範例資料庫中建立 TransactionHistory 資料表的資料分割函式和資料分割配置。 分割區會依日期分割,目的是將舊記錄 TransactionHistoryArchive 分成資料表。

{   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Reference the AdventureWorks2022 database.   
Database db;   
db = srv.Databases("AdventureWorks2022");   
//Define and create three new file groups on the database.   
FileGroup fg2;   
fg2 = new FileGroup(db, "Second");   
fg2.Create();   
FileGroup fg3;   
fg3 = new FileGroup(db, "Third");   
fg3.Create();   
FileGroup fg4;   
fg4 = new FileGroup(db, "Fourth");   
fg4.Create();   
//Define a partition function by supplying the parent database and name arguments in the constructor.   
PartitionFunction pf;   
pf = new PartitionFunction(db, "TransHistPF");   
//Add a partition function parameter that specifies the function uses a DateTime range type.   
PartitionFunctionParameter pfp;   
pfp = new PartitionFunctionParameter(pf, DataType.DateTime);   
pf.PartitionFunctionParameters.Add(pfp);   
//Specify the three dates that divide the data into four partitions.   
object[] val;   
val = new object[] {"1/1/2003", "1/1/2004", "1/1/2005"};   
pf.RangeValues = val;   
//Create the partition function.   
pf.Create();   
//Define a partition scheme by supplying the parent database and name arguments in the constructor.   
PartitionScheme ps;   
ps = new PartitionScheme(db, "TransHistPS");   
//Specify the partition function and the filegroups required by the partition scheme.   
ps.PartitionFunction = "TransHistPF";   
ps.FileGroups.Add("PRIMARY");   
ps.FileGroups.Add("second");   
ps.FileGroups.Add("Third");   
ps.FileGroups.Add("Fourth");   
//Create the partition scheme.   
ps.Create();   
}   

在 PowerShell 中設定資料表的資料分割配置

程式碼範例示範如何在 AdventureWorks2022 範例資料庫中建立 TransactionHistory 資料表的資料分割函式和資料分割配置。 分割區會依日期分割,目的是將舊記錄 TransactionHistoryArchive 分成資料表。

# Set the path context to the local, default instance of SQL Server.  
CD \sql\localhost\default  
  
#Get a server object which corresponds to the default instance  
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server  
$db = $srv.Databases["AdventureWorks"]  
#Create four filegroups  
$fg1 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Filegroup -argumentlist $db, "First"  
$fg2 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Filegroup -argumentlist $db, "Second"  
$fg3 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Filegroup -argumentlist $db, "Third"  
$fg4 = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Filegroup -argumentlist $db, "Fourth"  
  
#Define a partition function by supplying the parent database and name arguments in the constructor.  
$pf =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.PartitionFunction -argumentlist $db, "TransHistPF"  
$T = [Microsoft.SqlServer.Management.SMO.DataType]::DateTime  
$T  
$T.GetType()  
#Add a partition function parameter that specifies the function uses a DateTime range type.  
$pfp =  New-Object -TypeName Microsoft.SqlServer.Management.SMO.PartitionFunctionParameter -argumentlist $pf, $T  
  
#Specify the three dates that divide the data into four partitions.   
#Create an array of type object to hold the partition data  
$val = "1/1/2003"."1/1/2004","1/1/2005"  
$pf.RangeValues = $val  
$pf  
#Create the partition function  
$pf.Create()  
  
#Create partition scheme  
$ps = New-Object -TypeName Microsoft.SqlServer.Management.SMO.PartitionScheme -argumentlist $db, "TransHistPS"  
$ps.PartitionFunction = "TransHistPF"  
  
#add the filegroups to the scheme   
$ps.FileGroups.Add("PRIMARY")  
$ps.FileGroups.Add("Second")  
$ps.FileGroups.Add("Third")  
$ps.FileGroups.Add("Fourth")  
  
#Create it at the server  
$ps.Create()  

另請參閱

分割資料表與索引