Share via


통계 생성 및 업데이트

SMO에서 Statistic 개체를 사용하여 데이터베이스의 쿼리 처리에 대한 통계 정보를 수집할 수 있습니다.

StatisticStatisticColumn 개체를 사용하여 임의 열에 대한 통계를 만드는 것도 가능합니다. Statistic 개체에서 통계를 업데이트하려면 Update 메서드를 실행할 수 있습니다. 결과는 쿼리 최적화 프로그램에서 볼 수 있습니다.

제공된 코드 예제를 사용하려면 응용 프로그램을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 방법: 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(); 
}