統計の作成と更新

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 AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'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 = default(Server); 
   srv = new Server(); 
   //Reference the AdventureWorks database. 
   Database db = default(Database); 
   db = srv.Databases("AdventureWorks"); 
   //Reference the CreditCard table. 
   Table tb = default(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(); 
}