いくつかのSQLクエリの統計を収集しようとしています。統計を取得するためにSqlDbConnectionクラスのRetrieveStatistics()メソッドを使用し、クエリを実行するためにSqlCommandのExecuteReader()メソッドを使用しています。 RetrieveStatistics()メソッドは、実行されたクエリに関する統計で満たされた辞書を返します。通常のクエリを実行しているとき、辞書のSelectRowsプロパティには、クエリによって返された実際の行数が含まれています。しかし、ストアドプロシージャを実行しているとき、リーダーには間違いなく行が含まれていますが、SelectRowsは常にゼロです。
各クエリの前にResetStatistics()を呼び出し、 StatisticsEnabledをtrueに設定します。
これが私のPowershellコードです:
### Stored procedure
$cn = New-Object system.data.sqlclient.sqlconnection
$cn.ConnectionString = "Data Source=localhost;Initial Catalog=XXXX;Integrated Security=SSPI"
$cn.StatisticsEnabled = $true
$cmd = $cn.CreateCommand()
$cmd.CommandText = "[dbo].[spGetXXXX]"
$cmd.CommandType = "StoredProcedure"
$cn.Open()
$cmd.ExecuteReader()
# several rows returned
$cn.RetrieveStatistics()
Name Value
---- -----
BytesReceived 300
SumResultSets 1
ExecutionTime 5
Transactions 0
BuffersReceived 1
IduRows 0
ServerRoundtrips 1
PreparedExecs 0
BytesSent 132
SelectCount 1
CursorOpens 0
ConnectionTime 51299
Prepares 0
SelectRows 0
UnpreparedExecs 1
NetworkServerTime 3
BuffersSent 1
IduCount 0
### Regular SQL query
$cn2 = New-Object system.data.sqlclient.sqlconnection
$cn2.ConnectionString = "Data Source=localhost;Initial Catalog=XXXX;Integrated Security=SSPI"
$cn2.StatisticsEnabled = $true
$cmd2 = $cn2.CreateCommand()
$cmd2.CommandText = "SELECT * FROM XXXX"
$cn2.Open()
$cmd2.ExecuteReader()
#rows returned
$cn2.RetrieveStatistics()
Name Value
---- -----
BytesReceived 12357
SumResultSets 1
ExecutionTime 12
Transactions 0
BuffersReceived 2
IduRows 0
ServerRoundtrips 1
PreparedExecs 0
BytesSent 98
SelectCount 1
CursorOpens 0
ConnectionTime 11407
Prepares 0
SelectRows 112
UnpreparedExecs 1
NetworkServerTime 0
BuffersSent 1
IduCount 0