2

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

4

2 回答 2

4

您只能通过记录过去的执行情况、进行平均并告诉用户平均执行时间来估计时间。

您还可以使用动态管理视图和函数来查询有关此 SP 的 SQL Server。

于 2011-01-24T22:11:01.290 回答
0

You could use the command

set showplan_xml on

and then run the query (which wouldn't run it but would return an XML. You can then parse the XML for the Estimated attributes you are after.

Sample XML for

select top 10 * from master..spt_values

Output

<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>

The above is a literal interpretation of your request, but "cost" bears no significant meaning. It is a statistical value with no unit of measure nor any correlation to time.

Best to use a new connection lest other queries (worse with connection pooling) stop working because they start returning the XML plan...

于 2011-01-24T22:08:03.983 回答