Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Van toepassing op:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
SQL-database in Microsoft Fabric Preview
Gegevens kunnen worden opgeslagen met behulp van de opslagalgoritmen die worden geleverd door gepartitioneerde tabellen en indexen. Partitionering kan grote tabellen en indexen beter beheersbaar en schaalbaar maken.
Indexering en tabelpartitionering
Met de functie kunnen index- en tabelgegevens worden verdeeld over meerdere bestandsgroepen in partities. Een partitiefunctie definieert hoe de rijen van een tabel of index worden toegewezen aan een set partities op basis van de waarden van bepaalde kolommen, aangeduid als partitioneringskolommen. Een partitieschema wijst elke partitie die door de partitiefunctie is opgegeven toe aan een bestandsgroep. Hiermee kunt u archiveringsstrategieën ontwikkelen waarmee tabellen kunnen worden geschaald in bestandsgroepen en daarom fysieke apparaten.
Het Database object bevat een verzameling PartitionFunction objecten die de geïmplementeerde partitiefuncties vertegenwoordigen en een verzameling PartitionScheme objecten die beschrijven hoe gegevens worden toegewezen aan bestandsgroepen.
Elk Table object Index geeft aan welk partitieschema wordt gebruikt in de PartitionScheme eigenschap en geeft de kolommen in de PartitionSchemeParameterCollection.
Example
Voor de volgende codevoorbeelden moet u de programmeeromgeving, de programmeersjabloon en de programmeertaal selecteren om uw toepassing te maken. Zie Een Visual C# SMO-project maken in Visual Studio .NETvoor meer informatie.
Een partitieschema instellen voor een tabel in Visual C#
In het codevoorbeeld ziet u hoe u een partitiefunctie en een partitieschema maakt voor de TransactionHistory tabel in de voorbeelddatabase AdventureWorks2022. De partities worden gedeeld op datum met de bedoeling oude records in de TransactionHistoryArchive tabel te scheiden.
{
//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();
}
Een partitieschema instellen voor een tabel in PowerShell
In het codevoorbeeld ziet u hoe u een partitiefunctie en een partitieschema maakt voor de TransactionHistory tabel in de voorbeelddatabase AdventureWorks2022. De partities worden gedeeld op datum met de bedoeling oude records in de TransactionHistoryArchive tabel te scheiden.
# 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()