asp.net中SP的估计成本

时间:2011-01-24 22:01:43

标签: asp.net sql sql-server sql-server-2005 time-estimation

在我的asp.net应用程序中,我想向用户显示他在提交表单时该存储过程将花费多少时间。有什么办法可以从asp.net应用程序中找到答案吗?就像我们在sql server中估计了估计成本的查询计划一样。我们可以在asp.net网页上显示它吗?

2 个答案:

答案 0 :(得分:4)

您只能通过记录过去的执行次数,平均值并告诉用户平均花费的时间来估算时间。

您还可以使用Dynamic Management Views and Functions向SQL Server查询此SP。

答案 1 :(得分:0)

您可以使用命令

set showplan_xml on

然后运行查询(不会运行它但会返回XML。然后,您可以解析XML以获取您所追求的Estimated属性。

的XML示例
select top 10 * from master..spt_values

输出

<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.0" Build="9.00.3080.00">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementText="select top 10 * from master..spt_values&#xD;&#xA;" StatementId="1" StatementCompId="1" StatementType="SELECT" StatementSubTreeCost="0.00333664" StatementEstRows="10" StatementOptmLevel="TRIVIAL">
          <StatementSetOptions QUOTED_IDENTIFIER="false" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="false" ANSI_NULLS="false" ANSI_PADDING="false" ANSI_WARNINGS="false" NUMERIC_ROUNDABORT="false" />
          <QueryPlan CachedPlanSize="9" CompileTime="0" CompileCPU="0" CompileMemory="56">
            <RelOp NodeId="0" PhysicalOp="Top" LogicalOp="Top" EstimateRows="10" EstimateIO="0" EstimateCPU="1e-006" AvgRowSize="68" EstimatedTotalSubtreeCost="0.00333664" Parallel="0" EstimateRebinds="0" EstimateRewinds="0">
              <OutputList>
                <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="name" />
                <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="number" />
                <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="type" />
                <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="low" />
                <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="high" />
                <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="status" />
              </OutputList>
              <Top RowCount="0" IsPercent="0" WithTies="0">
                <TopExpression>
                  <ScalarOperator ScalarString="(10)">
                    <Const ConstValue="(10)" />
                  </ScalarOperator>
                </TopExpression>
                <RelOp NodeId="1" PhysicalOp="Clustered Index Scan" LogicalOp="Clustered Index Scan" EstimateRows="10" EstimateIO="0.0142361" EstimateCPU="0.0027376" AvgRowSize="68" EstimatedTotalSubtreeCost="0.00333564" Parallel="0" EstimateRebinds="0" EstimateRewinds="0">
                  <OutputList>
                    <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="name" />
                    <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="number" />
                    <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="type" />
                    <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="low" />
                    <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="high" />
                    <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="status" />
                  </OutputList>
                  <IndexScan Ordered="0" ForcedIndex="0" NoExpandHint="0">
                    <DefinedValues>
                      <DefinedValue>
                        <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="name" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="number" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="type" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="low" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="high" />
                      </DefinedValue>
                      <DefinedValue>
                        <ColumnReference Database="[master]" Schema="[dbo]" Table="[spt_values]" Column="status" />
                      </DefinedValue>
                    </DefinedValues>
                    <Object Database="[master]" Schema="[dbo]" Table="[spt_values]" Index="[spt_valuesclust]" />
                  </IndexScan>
                </RelOp>
              </Top>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>

以上是对您的请求的字面解释,但“费用”没有重大意义。它是一个统计值,没有计量单位,也没有与时间的任何相关性。

最好使用新连接,以免其他查询(更糟糕的是连接池)停止工作,因为他们开始返回XML计划......