创建和更新统计信息
适用于:SQL Server Azure SQL 数据库 azure Synapse Analytics Azure SQL 托管实例
在 SMO 中,有关处理数据库中的查询的统计信息可以使用 Statistic 对象进行收集。
可以使用该列和StatisticColumn对象为任何列Statistic创建统计信息。 可以运行 Update 方法以更新 Statistic 对象中的统计信息。 可以在查询优化器中查看结果。
示例
若要使用提供的任何代码示例,可以选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 在 Visual Studio .NET 中创建 Visual C# SMO 项目。
在 Visual Basic 中创建和更新统计信息
此代码示例将对为其创建 Statistic 对象和 StatisticColumn 对象的现有数据库创建新表。
'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 中创建和更新统计信息#
此代码示例将对为其创建 Statistic 对象和 StatisticColumn 对象的现有数据库创建新表。
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 中创建和更新统计信息
此代码示例将对为其创建 Statistic 对象和 StatisticColumn 对象的现有数据库创建新表。
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}