Solved

Stored Procedure takes 30 seconds but Report takes 10 minutes

Posted on 2014-10-08
3
86 Views
Last Modified: 2015-01-13
I am using SQL Server 2005 and I have been trying to fix this for a while now. I have tried the parameter sniffing and with recompile but neither have worked.

When executing the stored procedure, it takes 30 seconds and the report takes around 10 minutes. I'm not going to post the whole stored procedure because it is 300 lines, but I'll post the first bit of it. I am lost, and so are my coworkers as to why this is so slow.

    ALTER PROCEDURE [dbo].[sp_PermitAllData]
        @datey datetime
    with recompile
    AS
    BEGIN
       Set NoCount On
    
       Declare @date datetime
       Set @date = @datey

       --Permit11
       select 
           MDate,
           isnull(NumbBreaker, 0) NumbBreaker,
           isnull(Steel20T, 0) Steel20T,
           isnull(Steel9T, 0) Steel9T,
           isnull(Ductile, 0) Ductile,
           isnull(IMF,0) IMF,
           isnull((select sum(NumbBreaker) from Permit p 
                   where MDate>= dateadd(month,-11,Permit.MDate) and MDate<= dateadd(month,-1,Permit.MDate)),0) NumbBreaker11,
           isnull((select sum(Steel20T) from Permit p 
                   where MDate>= dateadd(month,-11,Permit.MDate) and MDate<=dateadd(month,-1,Permit.MDate) and MDate>='10/1/2008'),0) Steel20T11,
           isnull((select sum(Steel9T) from Permit p where MDate>= dateadd(month,-11,Permit.MDate) and MDate<= dateadd(month,-1,Permit.MDate)and MDate>='10/1/2008'),0) Steel9T11,
           isnull((select sum(Ductile) from Permit p where MDate>= dateadd(month,-11,Permit.MDate) and MDate<= dateadd(month,-1,Permit.MDate)),0) Ductile11,
           isnull((select sum(IMF) from Permit p where  MDate>= dateadd(month,-11,Permit.MDate) and MDate<= dateadd(month,-1,Permit.MDate)),0) IMF11
        Into #Permit11 
        from 
           Permit 
        where 
           MDate between dateadd(month,-2,@date) AND @date
    
        --270
      
       SELECT dateadd(month,datediff(month,0,fdate),0) Month,-1*SUM(fqty) 'A270',
           (select -1*sum(fqty) 
            from M2MDATA01.dbo.intran t  INNER JOIN
                 M2MDATA01.dbo.inmast m ON t.fpartno = m.fpartno INNER JOIN 
                 M2MDATA01.dbo.jomast j ON t.ftojob = j.fjobno INNER JOIN
                 M2MDATA01.dbo.inmast m2 on j.fpartno=m2.fpartno and j.fpartrev=m2.frev and j.fac=m2.fac
                 --PEntries e ON j.fpartno = e.Sand 
            where dateadd(month,datediff(month,0,t.fdate),0) BETWEEN dateadd(month,-11,dateadd(month,datediff(month,0,intran.fdate),0)) and dateadd(month,-1,dateadd(month,datediff(month,0,intran.fdate),0)) AND
                 fdate>='6/1/2008' AND
                  t.ftype = 'I' AND 
                  (m.fdescript in('TECHNISET 6435 PART 2 339749','TECHNISET F6000 PART 1 347944') OR m.fdescript LIKE 'ACTIVATOR%')  AND 
                  m2.fcusrchr2='270') 'A27011'
                  
    Into #270 
    FROM   M2MDATA01.dbo.intran intran  INNER JOIN
           M2MDATA01.dbo.inmast inmast ON intran.fpartno = inmast.fpartno INNER JOIN 
           M2MDATA01.dbo.jomast jomast ON intran.ftojob = jomast.fjobno INNER JOIN
            M2MDATA01.dbo.inmast m2 on jomast.fpartno=m2.fpartno and jomast.fpartrev=m2.frev and jomast.fac=m2.fac
          -- PEntries ON jomast.fpartno = PEntries.Sand 
    WHERE  dateadd(month,datediff(month,0,fdate),0) BETWEEN dateadd(month,-2,@date) and @date AND
           intran.ftype = 'I' AND 
          (inmast.fdescript in('TECHNISET 6435 PART 2 339749','TECHNISET F6000 PART 1 347944') OR inmast.fdescript LIKE 'ACTIVATOR%') AND 
           m2.fcusrchr2='270'
    GROUP BY dateadd(month,datediff(month,0,fdate),0), dateadd(month,-11,dateadd(month,datediff(month,0,intran.fdate),0)), dateadd(month,-1,dateadd(month,datediff(month,0,intran.fdate),0))
    ORDER BY dateadd(month,datediff(month,0,fdate),0)

Open in new window

0
Comment
Question by:hscast
3 Comments
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
ID: 40369412
I think you need to rewrite the queries to reduce overhead.

Here's a possible rewrite of the first query to check out:


    ALTER PROCEDURE [dbo].[sp_PermitAllData]
        @datey datetime
    with recompile
    AS
    BEGIN
       Set NoCount On
   
       Declare @date datetime
       Set @date = @datey
       
       If Object_Id('tempdb..#PermitMonths') is not null
           Drop Table #PermitMonths

       Select
            IDENTITY(int, 1, 1) AS id,
           dateadd(month,datediff(month, 0, MDate), 0) AS MDateMonth,
           sum(NumbBreaker) AS NumbBreaker,
           sum(Steel20T) AS Steel20T,
           sum(Steel9T) AS Steel9T,
           sum(Ductile) AS Ductile,
           sum(IMF) AS IMF
        Into #PermitMonths
        from
           Permit
        where
           MDate between dateadd(month,-14,@date) AND @date
        group by
           MDate
       

       --Permit11
       select
           MDate,
           isnull(NumbBreaker, 0) NumbBreaker,
           isnull(Steel20T, 0) Steel20T,
           isnull(Steel9T, 0) Steel9T,
           isnull(Ductile, 0) Ductile,
           isnull(IMF,0) IMF,
           isnull((select sum(NumbBreaker) from #PermitMonths pm
                   where pm.MDate>= dateadd(month,-11,Permit.MDate) and pm.MDate<= dateadd(month,-1,Permit.MDate)),0) NumbBreaker11,
           isnull((select sum(Steel20T) from #PermitMonths pm
                   where pm.MDate>= dateadd(month,-11,Permit.MDate) and pm.MDate<=dateadd(month,-1,Permit.MDate) and pm.MDate>='20081001'),0) Steel20T11,
           isnull((select sum(Steel9T) from #PermitMonths pm where pm.MDate>= dateadd(month,-11,Permit.MDate) and pm.MDate<= dateadd(month,-1,Permit.MDate)and pm.MDate>='20081001'),0) Steel9T11,
           isnull((select sum(Ductile) from #PermitMonths pm where pm.MDate>= dateadd(month,-11,Permit.MDate) and pm.MDate<= dateadd(month,-1,Permit.MDate)),0) Ductile11,
           isnull((select sum(IMF) from #PermitMonths pm where  pm.MDate>= dateadd(month,-11,Permit.MDate) and pm.MDate<= dateadd(month,-1,Permit.MDate)),0) IMF11
        Into #Permit11
        from
           Permit
        where
           MDate between dateadd(month,-2,@date) AND @date
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40370125
How many records returns this SP? And the report's processing data (transformations, calculations,...)?
You may also check for network bottlenecks.
0
 
LVL 9

Accepted Solution

by:
rajeevnandanmishra earned 250 total points
ID: 40449608
Hi,
As earlier explained,
:: the procedure code can be improved further
:: the number of records returned by SP will make a considerable difference
A few more points that you can check
:: lot of formatting in SSRS will make the the report rendering slow (conditional data change will further impact performance, try to change data in SQL SP only)
:: disable the [Print Layout] option in your report and then try to run and see its performance
:: check the trace/profiler, to identify the actual time taken by SP (when run from the SSRS)
:: try to modify the SP to return only top few (e.g. 10) rows, and then run the SSRS report to see, if large number of rows is actually making the performance hit.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL to Update Table Dynamically 2 41
SQL 2005 - Memory Table Column Names 11 69
sql query help 2 45
How can I setup ssrs report  as UTF-8 . 7 18
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now