Création et mise à jour des statistiques

Dans SMO, les informations statistiques à propos du traitement de requêtes dans la base de données peuvent être recueillies en utilisant l'objet Statistic.

Il est possible de créer des statistiques pour n'importe quelle colonne en utilisant l'objet Statistic et StatisticColumn. La méthode Update peut être exécutée pour mettre à jour les statistiques dans l'objet Statistic. Les résultats peuvent être affichés dans l'optimiseur de requête.

Exemple

Pour utiliser un exemple de code fourni, vous devez sélectionner l'environnement, le modèle et le langage de programmation à utiliser pour créer votre application. Pour plus d'informations, consultez Procédure : créer un projet SMO Visual Basic dans Visual Studio .NET ou Procédure : créer un projet SMO Visual C# dans Visual Studio .NET.

Création et mise à jour des statistiques en Visual Basic

Cet exemple de code crée une table sur une base de données existante pour laquelle l'objet Statistic et l'objet StatisticColumn sont créés.

'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()

Création et mise à jour des statistiques en Visual C#

Cet exemple de code crée une table sur une base de données existante pour laquelle l'objet Statistic et l'objet StatisticColumn sont créés.

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