Share via


Verwenden von Tabellen- und Indexpartitionierung

Daten können mit den von Partitionierte Tabellen und Indizes bereitgestellten Speicheralgorithmen gespeichert werden. Die Partitionierung kann bewirken, dass sich große Tabellen und Indizes besser verwalten und skalieren lassen.

Index- und Tabellenpartitionierung

Die Funktion aktiviert Index- und Tabellendaten, die auf mehrere Dateigruppen in Partitionen verteilt werden. Eine Partitionsfunktion definiert, wie die Zeilen einer Tabelle oder eines Index basierend auf den Werten bestimmter Spalten, den so genannten Partitionierungsspalten, einem Satz von Partitionen zugeordnet werden. Ein Partitionierungsschema ordnet die durch die Partitionsfunktion angegebenen Partitionen jeweils einer Dateigruppe zu. Hierdurch können Sie Archivierungsstrategien entwickeln, die es ermöglichen, dass Tabellen, und damit physische Medien, dateigruppenübergreifend skaliert werden können.

Das Database-Objekt enthält eine Auflistung von PartitionFunction-Objekten, die die implementierten Partitionsfunktionen darstellen, und eine Auflistung von PartitionScheme-Objekten, die beschreiben, wie Daten Dateigruppen zugeordnet werden.

Jedes Table- und Index-Objekt legt fest, welches Partitionsschema es in der PartitionScheme-Eigenschaft verwendet, und legt die Spalten in PartitionSchemeParameterCollection fest.

Beispiel

Für das folgende Codebeispiel müssen Sie die Programmierungsumgebung, die Programmiervorlage und die Programmiersprache auswählen, um Ihre Anwendung zu erstellen. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen eines Visual Basic-SMO-Projekts in Visual Studio .NET und Vorgehensweise: Erstellen eines Visual C#-SMO-Projekts in Visual Studio .NET.

Einrichten eines Partitionsschemas für eine Tabelle in Visual Basic

Das Codebeispiel veranschaulicht, wie eine Partitionsfunktion und ein Partitionsschema für die TransactionHistory-Tabelle in der Beispieldatenbank AdventureWorks2008R2 erstellt werden. Die Partitionen sind nach Datum aufgeteilt, um alte Datensätze in die TransactionHistoryArchive-Tabelle auszugliedern.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Reference the AdventureWorks2008R2database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'Define and create three new file groups on the database.
Dim fg2 As FileGroup
fg2 = New FileGroup(db, "Second")
fg2.Create()
Dim fg3 As FileGroup
fg3 = New FileGroup(db, "Third")
fg3.Create()
Dim fg4 As FileGroup
fg4 = New FileGroup(db, "Fourth")
fg4.Create()
'Define a partition function by supplying the parent database and name arguments in the constructor.
Dim pf As PartitionFunction
pf = New PartitionFunction(db, "TransHistPF")
'Add a partition function parameter that specifies the function uses a DateTime range type.
Dim pfp As PartitionFunctionParameter
pfp = New PartitionFunctionParameter(pf, DataType.DateTime)
pf.PartitionFunctionParameters.Add(pfp)
'Specify the three dates that divide the data into four partitions.
Dim val() As Object
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.
Dim ps As PartitionScheme
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()

Einrichten eines Partitionsschemas für eine Tabelle in Visual C#

Das Codebeispiel veranschaulicht, wie eine Partitionsfunktion und ein Partitionsschema für die TransactionHistory-Tabelle in der Beispieldatenbank AdventureWorks2008R2 erstellt werden. Die Partitionen sind nach Datum aufgeteilt, um alte Datensätze in die TransactionHistoryArchive-Tabelle auszugliedern.

{ 
//Connect to the local, default instance of SQL Server. 
Server srv; 
srv = new Server(); 
//Reference the AdventureWorks2008R2 database. 
Database db; 
db = srv.Databases("AdventureWorks2008R2"); 
//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(); 
} 

Einrichten eines Partitionsschemas für eine Tabelle in PowerShell

Das Codebeispiel veranschaulicht, wie eine Partitionsfunktion und ein Partitionsschema für die TransactionHistory-Tabelle in der Beispieldatenbank AdventureWorks2008R2 erstellt werden. Die Partitionen sind nach Datum aufgeteilt, um alte Datensätze in die TransactionHistoryArchive-Tabelle auszugliedern.

# 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()