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
In SMO kan statistische informatie over het verwerken van query's in de database worden verzameld met behulp van het Statistic object.
Het is mogelijk om statistieken te maken voor elke kolom met behulp van het Statistic en StatisticColumn object. De Update methode kan worden uitgevoerd om de statistieken in het Statistic object bij te werken. De resultaten kunnen worden weergegeven in de queryoptimalisatie.
Example
Als u een codevoorbeeld wilt gebruiken dat is opgegeven, kunt u de programmeeromgeving, de programmeersjabloon en de programmeertaal kiezen waarin u uw toepassing wilt maken. Zie Een Visual C# SMO-project maken in Visual Studio .NETvoor meer informatie.
Statistieken maken en bijwerken in Visual Basic
In dit codevoorbeeld wordt een nieuwe tabel gemaakt op een bestaande database waarvoor het Statistic object en het StatisticColumn object worden gemaakt.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'Reference the CreditCard table.
Dim tb As Table
tb = db.Tables("CreditCard", "Sales")
'Define a Statistic object by supplying the parent table and name arguments in the constructor.
Dim stat As Statistic
stat = New Statistic(tb, "Test_Statistics")
'Define a StatisticColumn object variable for the CardType column and add to the Statistic object variable.
Dim statcol As StatisticColumn
statcol = New StatisticColumn(stat, "CardType")
stat.StatisticColumns.Add(statcol)
'Create the statistic counter on the instance of SQL Server.
stat.Create()
Statistieken maken en bijwerken in C#
In dit codevoorbeeld wordt een nieuwe tabel gemaakt op een bestaande database waarvoor het Statistic object en het StatisticColumn object worden gemaakt.
public static void CreatingAndUpdatingStatistics()
{
// Connect to the local, default instance of SQL Server.
var srv = new Server();
// Reference the AdventureWorks2022 database.
var db = srv.Databases["AdventureWorks"];
// Reference the CreditCard table.
var tb = db.Tables["CreditCard", "Sales"];
// Define a Statistic object by supplying the parent table and name
// arguments in the constructor.
var stat = new Statistic(tb, "Test_Statistics");
// Define a StatisticColumn object variable for the CardType column
// and add to the Statistic object variable.
var statcol = new StatisticColumn(stat, "CardType");
stat.StatisticColumns.Add(statcol);
//Create the statistic counter on the instance of SQL Server.
stat.Create();
// List all the statistics object on the table (you will see the newly created one)
foreach (var s in tb.Statistics.Cast<Statistic>())
Console.WriteLine($"{s.ID}\t{s.Name}");
// Output:
// 2 AK_CreditCard_CardNumber
// 1 PK_CreditCard_CreditCardID
// 3 Test_Statistics
}
Statistieken maken en bijwerken in PowerShell
In dit codevoorbeeld wordt een nieuwe tabel gemaakt op een bestaande database waarvoor het Statistic object en het StatisticColumn object worden gemaakt.
Import-Module SQLServer
# Connect to the local, default instance of SQL Server.
$srv = Get-Item SQLSERVER:\SQL\localhost\DEFAULT
# Reference the AdventureWorks database.
$db = $srv.Databases["AdventureWorks"]
# Reference the CreditCard table.
$tb = $db.Tables["CreditCard", "Sales"]
# Define a Statistic object by supplying the parent table and name
# arguments in the constructor.
$stat = New-Object Microsoft.SqlServer.Management.Smo.Statistic($tb, "Test_Statistics")
# Define a StatisticColumn object variable for the CardType column
# and add to the Statistic object variable.
$statcol = New-Object Microsoft.SqlServer.Management.Smo.StatisticColumn($stat, "CardType")
$stat.StatisticColumns.Add($statcol)
# Create the statistic counter on the instance of SQL Server.
$stat.Create()
# Finally dump all the statistics (you can see the newly created one at the bottom)
$tb.Statistics
# Output:
# Name Last Updated Is From Index Statistic Columns
# Creation
# ---- ------------ -------------- -----------------
# AK_CreditCard_CardNumber 10/27/2017 2:33 PM True {CardNumber}
# PK_CreditCard_CreditCardID 10/27/2017 2:33 PM True {CreditCardID}
# Test_Statistics 6/4/2020 8:11 PM False {CardType}