【问题标题】:Retrieving statistics for stored procedure, SelectRows is always 0检索存储过程的统计信息,SelectRows 始终为 0
【发布时间】:2012-08-26 10:19:21
【问题描述】:

我正在尝试收集有关某些 SQL 查询的统计信息。 我正在使用 SqlDbConnection 类的 RetrieveStatistics() 方法来获取统计信息,并使用 SqlCommandExecuteReader() 方法来运行查询。 RetrieveStatistics() 方法返回填充了执行查询的统计信息的字典。 当我运行常规查询时,字典的 SelectRows 属性包含查询返回的实际行数。但是当我运行存储过程时,SelectRows 总是为零,尽管 reader 肯定包含行。

我在每次查询之前调用 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

【问题讨论】:

    标签: c# sql sql-server powershell


    【解决方案1】:

    这两个查询的区别在于,存储过程调用读取器保持打开后,行号统计不更新。

    所以正确的代码应该是这样的:

    $connectionn.ResetStatistics()
    $reader = $command.ExecuteReader()
    $reader.Close() # !
    $connection.RetrieveStatistics()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多