SQLDatasource选择参数会导致超时

时间:2014-03-04 00:29:15

标签: c# asp.net parameters sqldatasource

如果我运行此代码,结果将在不到一秒的时间内返回 -

<asp:SqlDataSource ID="OrdersSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:OperationsPortal.Properties.Settings.JDEProd %>" SelectCommand="
                SELECT A.Test
                FROM PRODDTA.F5547510 AS B WITH(NOLOCK)
                INNER JOIN PRODDTA.F4211 AS A WITH(NOLOCK) ON B.SCDOCO = A.SDDOCO
                INNER JOIN PRODDTA.F0101 C WITH(NOLOCK) ON A.SDAN8 = C.ABAN8
                **WHERE (A.SDMCU = '         CHP')**
                ORDER BY ORDERDATE">
                <SelectParameters>
                    <asp:ControlParameter ControlID="drpBranch" DefaultValue="CHP" Name="BranchPlant" PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>

但是,只要我将WHERE语句更改为使用选择参数,查询就会超时 -

<asp:SqlDataSource ID="OrdersSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:OperationsPortal.Properties.Settings.JDEProd %>" SelectCommand="
                SELECT A.Test                      
                FROM PRODDTA.F5547510 AS B WITH(NOLOCK)
                INNER JOIN PRODDTA.F4211 AS A WITH(NOLOCK) ON B.SCDOCO = A.SDDOCO
                INNER JOIN PRODDTA.F0101 C WITH(NOLOCK) ON A.SDAN8 = C.ABAN8
                **WHERE (A.SDMCU = '         ' + @BranchPlant)**
                ORDER BY ORDERDATE">
                <SelectParameters>
                    <asp:ControlParameter ControlID="drpBranch" DefaultValue="CHP" Name="BranchPlant" PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>

有谁知道我能做些什么来解决这个问题?如果我在Management Studio中运行它,我会得到类似的结果,但在Management Studio中我可以添加&#34; OPTION(RECOMPILE)&#34;到查询结束并修复问题。但是,这似乎不适用于我的SQLDatatSource SelectCommand。

1 个答案:

答案 0 :(得分:0)

如果其他人遇到此问题,这就是我最终解决性能问题的方法。我将变量声明为top,将变量的值设置为Select Parameter并使用OPTION(OPTIMIZE) -

<asp:SqlDataSource CancelSelectOnNullParameter="False"  ID="OrdersSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:OperationsPortal.Properties.Settings.JDEProd %>" SelectCommand="
                DECLARE @Branch varchar(12)
                SET @Branch = @BranchPlant
                SELECT A.Test
                FROM PRODDTA.F5547510 AS B WITH(NOLOCK)
                INNER JOIN PRODDTA.F4211 AS A WITH(NOLOCK) ON (A.SDDOCO = B.SCDOCO AND A.SDLNID = B.SCLNID)
                INNER JOIN PRODDTA.F0101 C WITH(NOLOCK) ON A.SDSHAN = C.ABAN8
                WHERE (A.SDMCU = '         ' + @Branch) 
                OPTION (OPTIMIZE FOR (@Branch = 'CHP'))">
                <SelectParameters>
                    <asp:ControlParameter ControlID="drpBranch" Name="BranchPlant" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>