开发者

Estimated cost for the SP in asp.net

开发者 https://www.devze.com 2023-02-07 00:46 出处:网络
In my asp.net application, I want to show the u开发者_StackOverflow中文版ser that when he is submitting the form how much time does that stored procedure is going to take. Is there any way I can find

In my asp.net application, I want to show the u开发者_StackOverflow中文版ser that when he is submitting the form how much time does that stored procedure is going to take. Is there any way I can find out from asp.net application? Just like we have estimated query plan with estimated cost in sql server. Can we show it on asp.net webpage?


You can only estimate the time by taking record of past executions, making an average and telling the user the average time that it will take.

You can also use Dynamic Management Views and Functions to query SQL Server about this SP.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消