共用方式為


建立和更新統計資料

在 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 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(); 
}