创建和更新统计信息

在 SMO 中,有关处理数据库中的查询的统计信息可以使用 Statistic 对象进行收集。

可以使用 StatisticStatisticColumn 对象创建任何列的统计信息。 可以运行 Update 方法以更新 Statistic 对象中的统计信息。 可以在查询优化器中查看结果。

示例

若要使用所提供的任何代码示例,您必须选择创建应用程序所需的编程环境、编程模板和编程语言。 有关详细信息,请参阅在 Visual Studio .NET 中创建 Visual Basic SMO 项目在 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 AdventureWorks2012 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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()

在 Visual C# 中创建和更新统计信息

此代码示例将对为其创建 Statistic 对象和 StatisticColumn 对象的现有数据库创建新表。

{
            //Connect to the local, default instance of SQL Server.
            Server srv = new Server();
            //Reference the AdventureWorks2012 database. 
            Database db = default(Database);
            db = srv.Databases["AdventureWorks2012"];
            //Reference the CreditCard table. 
           
           Table tb = db.Tables["CreditCard", "Sales"];
            //Define a Statistic object by supplying the parent table and name 
            //arguments in the constructor. 
            Statistic stat = default(Statistic);
            stat = new Statistic(tb, "Test_Statistics");
            //Define a StatisticColumn object variable for the CardType column 
            //and add to the Statistic object variable. 
            StatisticColumn statcol = default(StatisticColumn);
            statcol = new StatisticColumn(stat, "CardType");
            stat.StatisticColumns.Add(statcol);
            //Create the statistic counter on the instance of SQL Server. 
            stat.Create();
        }

在 PowerShell 中创建和更新统计信息

此代码示例将对为其创建 Statistic 对象和 StatisticColumn 对象的现有数据库创建新表。

# Example of implementing a full text search on the default instance.
# Set the path context to the local, default instance of SQL Server and database tables

CD \sql\localhost\default\databases
$db = get-item AdventureWorks2012

CD AdventureWorks2012\tables

#Get a reference to the table
$tb = get-item Production.ProductCategory


# Define a FullTextCatalog object variable by specifying the parent database and name arguments in the constructor.

$ftc = New-Object -TypeName Microsoft.SqlServer.Management.SMO.FullTextCatalog -argumentlist $db, "Test_Catalog2"
$ftc.IsDefault = $true

# Create the Full Text Search catalog on the instance of SQL Server.
$ftc.Create()

# Define a FullTextIndex object variable by supplying the parent table argument in the constructor.
$fti = New-Object -TypeName Microsoft.SqlServer.Management.SMO.FullTextIndex -argumentlist $tb

#  Define a FullTextIndexColumn object variable by supplying the parent index 
#  and column name arguments in the constructor.

$ftic = New-Object -TypeName Microsoft.SqlServer.Management.SMO.FullTextIndexColumn -argumentlist $fti, "Name"

# Add the indexed column to the index.
$fti.IndexedColumns.Add($ftic)

# Set change tracking
$fti.ChangeTracking = [Microsoft.SqlServer.Management.SMO.ChangeTracking]::Automatic

# Specify the unique index on the table that is required by the Full Text Search index.
$fti.UniqueIndexName = "AK_ProductCategory_Name"

# Specify the catalog associated with the index.
$fti.CatalogName = "Test_Catalog2"

# Create the Full Text Search Index
$fti.Create()