0

Context: migrating from one environment to another, in old environment when loading the ASP.NET page that used this stored procedure, it ran quickly, now it's a 20 second wait until the same page loads. Ran SQL Server Profiler while trying to load the page, as soon as page finishes loading I see the stored procedure in profiler and it says it says duration is around 20 seconds, which corresponds to the page load time. When I execute the stored procedure in a SSMS window, it returns nearly instantly, so I'm not sure why the difference...

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[GetBusinessesByOfficeAndLetter] (
@workunit int,
@letter varchar(5)
) AS

SELECT 
    [BusinessActivity].[ident], [fullname], [addr1], [city], [state], 
    [status], ProductType.name, lastactivity, openactivities 
FROM 
    [BusinessActivity] 
INNER JOIN 
    Workunit ON BusinessActivity.workunit = Workunit.ident
LEFT JOIN 
    workunit area ON Workunit.parent = area.ident
LEFT JOIN 
    workunit region ON area.parent = region.ident
LEFT OUTER JOIN 
    ProductType ON BusinessActivity.productdem1 = ProductType.ident
LEFT OUTER JOIN 
    RiskLevel ON BusinessActivity.risklevel = RiskLevel.abbr
 WHERE (Workunit.ident=@workunit OR Area.ident=@workunit OR region.ident=@workunit) AND fullname LIKE @letter+'%' ORDER BY [fullname], [city]

BusinessActivity is a view, and the last two items in the select query of this stored procedure are computed columns in that view, but I can't imagine that's an issue.

Calling Code:

    Protected Sub GetBusinessListData(ByVal sender As Object, ByVal e As System.EventArgs)
    Try
        Dim daLoc As SqlDataAdapter = New SqlDataAdapter()
        Dim dsLoc As New Data.DataSet
        Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
        Dim cmdX As SqlCommand = cn.CreateCommand

        cmdX.CommandText = "GetBusinessesByOfficeAndLetter"
        cmdX.CommandType = Data.CommandType.StoredProcedure
        cmdX.Parameters.AddWithValue("workunit", Me.cboWorkunit.SelectedValue)
        If hdnLetter.Value = "" Then hdnLetter.Value = "XXXX"
        cmdX.Parameters.AddWithValue("letter", Me.hdnLetter.Value)
        daLoc.SelectCommand = cmdX
        daLoc.Fill(dsLoc)
        Me.gvLocations.DataSource = dsLoc.Tables(0)
        Session("LocList") = dsLoc.Tables(0)
        Me.gvLocations.DataBind()
        Me.gvLocations.SelectedIndex = -1

    Catch ex As Exception
        Dim p As String = ex.Message
    End Try

End Sub
Albert
  • 3,639
  • 13
  • 43
  • 58
  • are you not limiting rows with a `where` clause at all? Are there no parameters for your stored procedure? You only posted a query, not a (necessarily complete) stored procedure, which is why I ask – Kritner Nov 10 '15 at 17:16
  • Can you post the calling code from the asp.net page? – Bill Martin Nov 10 '15 at 17:23
  • also there are seemingly some issues with (what I assume) is your example code. `wu.parent` is referenced in your query join, but `wu` is not an aliased table anywhere in the query. – Kritner Nov 10 '15 at 17:24
  • 1
    In your old environment, were your IIS server and database server on the same network? Are you migrating your IIS server to a separate network from your database server? If you're selecting a lot of data and you're on separate networks now, latency could be your problem. – Joel Nov 10 '15 at 17:25
  • Sorry missed some of that SP. Added everything. Fixed the wu issue with example code. Will .NET post calling code soon. – Albert Nov 10 '15 at 17:39
  • Moving from a SQL Cluster and IIS running on Server 2008 to a virtual machine environment running on Server 2012...one VM for DB server one VM for IIS server. Both servers are on the same network. Users were on the same domain in original environment, but now are not. All pages load as they did except this one. – Albert Nov 10 '15 at 17:42
  • 2
    possibly related: http://stackoverflow.com/questions/6585417/stored-procedure-slow-when-called-from-web-fast-from-management-studio – Kritner Nov 10 '15 at 17:44
  • 2
    There are a number of possibilities here. Stale statistics? Parameter sniffing? Are the two environments the same version of sql server? Posting an execution plan would help considerably to identify the bottleneck. – Sean Lange Nov 10 '15 at 17:44
  • Old SQL Server environment was 2008, new SQL Server is 2012. I'll take a look at the execution plan. – Albert Nov 10 '15 at 17:48
  • Did you update statistics and defragment your indexes on the new server? – Sean Lange Nov 10 '15 at 19:26

1 Answers1

0

Turned out to be parameter sniffing. Thanks for the link Kritner. Stored procedure slow when called from web, fast from Management Studio - Answer from Zane

Community
  • 1
  • 1
Albert
  • 3,639
  • 13
  • 43
  • 58