Criando e atualizando estatísticas

No SMO, as informações estatísticas sobre o processamento de consultas no banco de dados podem ser coletadas através do objeto Statistic.

É possível criar estatísticas para qualquer coluna através dos objetos Statistic e StatisticColumn. O método Update pode ser executado para atualizar as estatísticas no objeto Statistic. Os resultados podem ser exibidos no Otimizador de Consulta.

Exemplo

Para usar qualquer exemplo de código fornecido, será necessário escolher o ambiente de programação, o modelo de programação e a linguagem de programação para criar o seu aplicativo. Para obter mais informações, consulte Como criar um projeto SMO do Visual Basic no Visual Studio .NET ou Como criar um projeto SMO do Visual C# no Visual Studio .NET.

Criando e atualizando estatísticas no Visual Basic

Este exemplo de código cria uma nova tabela em um banco de dados existente para o qual são criados os objetos Statistic e 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()

Criando e atualizando estatísticas no Visual C#

Este exemplo de código cria uma nova tabela em um banco de dados existente para o qual são criados os objetos Statistic e 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(); 
}