Share via


統計の作成と更新

SMO では、Statistic オブジェクトを使用して、データベース内のクエリの処理に関する統計情報を収集することができます。

任意の列に対する統計の作成は、Statistic および StatisticColumn オブジェクトを使用して行うことができます。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 AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'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 AdventureWorks2008R2 database. 
            Database db = default(Database);
            db = srv.Databases["AdventureWorks2008R2"];
            //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 AdventureWorks2008R2

CD AdventureWorks2008R2\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()