建立和更新統計數據
適用於:sql ServerAzure SQL 資料庫 Azure SQL 受控執行個體 Microsoft Fabric 中的 Azure Synapse Analytics SQL 資料庫
在 SMO 中,可以使用 物件收集 Statistic 資料庫中處理查詢的統計數據。
您可以使用和 StatisticColumn 物件,為任何數據行Statistic建立統計數據。 Update您可以執行 方法來更新 物件中的Statistic統計數據。 您可以在查詢優化器中檢視結果。
範例
若要使用提供的任何程式代碼範例,您可以選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱 在Visual Studio .NET 中建立Visual C# SMO 專案。
在 Visual Basic 中建立和更新統計數據
此程式代碼範例會在建立 物件和 StatisticColumn 對象的現有資料庫Statistic上建立新的數據表。
'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()
在 C 中建立和更新統計數據#
此程式代碼範例會在建立 物件和 StatisticColumn 對象的現有資料庫Statistic上建立新的數據表。
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
}
在 PowerShell 中建立和更新統計數據
此程式代碼範例會在建立 物件和 StatisticColumn 對象的現有資料庫Statistic上建立新的數據表。
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}