1
我試圖收集一些SQL查詢的統計信息。 我使用RetrieveStatistics()SqlDbConnection類的方法來獲得統計和的ExecuteReader()的方法的SqlCommand運行查詢。 RetrieveStatistics()方法返回填充了執行查詢統計信息的字典。 當我運行常規查詢時,SelectRows字典的實體包含查詢返回的實際行數。但是,當我運行存儲過程時,SelectRows始終爲零,儘管reader肯定包含行。檢索存儲過程的統計信息,SelectRows始終爲0
我打電話給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