Question

If I run this code, the result is returned in less than a second -

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

However, as soon as I change the WHERE statement to use a Select Parameter, the query times out -

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

Does anyone know what I can do to fix this? I get similar results if I run this in Management Studio, but in Management Studio I can add "OPTION(RECOMPILE)" to the end of the query and that fixes the issue. But, that doesn't seem to work in my SQLDatatSource SelectCommand.

No correct solution

OTHER TIPS

In case someone else runs into this issue, this is how I ended up fixing the performance issue. I declared a variable up top, set the value of the variable to the Select Parameter and used 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top