SSRS query performance (dynamic SQL vs. stored proc)

  • In SSRS 2008 R2, I have a query that runs in 5-8 seconds in SSMS. When I run the report it takes about four minutes and when I run the same query in Report Builder's Query Designer it takes about four minutes. (db, query, SSRS, all running locally)

    I turned on profiler to capture the activity between the query run in SSMS vs. the query run in SSRS and I see that SSRS puts my query in a string and does an exec sp_executesql command. Is it the dynamic SQL in the sp_executesql that's giving such a dramatic performance difference?

    If I change the report's data source to a stored procedure would that eliminate this seeming overhead?

    Thanks,

    Rob

  • Are you sure the reason the report runs so long is the query (data retrievel) and not the generation of the report?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Koen,

    No, I'm not positive in what takes the report so long. I'm not sorting or filtering in SSRS -- that's all taken care of in the query; it's a pretty basic rows and columns report. My only clue seems to be when I watch SQL Server with profiler running SSRS seems to wrap the SELECT in an executesql. I can then copy this executesql into SSMS and that takes minutes compared to executing the statements outside the executesql which takes under 10 seconds.

    Rob

  • Maybe parameter sniffing? I noticed some threads where this was the cause of slow SSRS reports.

    T-SQL Best Practices - Parameter Sniffing

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Thank you for the link -- a good explanation of parameter sniffing. Perhaps you're on to something with parameter sniffing. My Confio tool (Ignite) did say that the execution plan had changed for this query -- though when I compare the two execution plans they look exactly the same to me (as viewed in SQL Sentry's Plan Explorer). There has been a schema change -- a bit field was added to one of the tables used in the query, though that particular new field is not used anywhere in this particular query.

    The query brings back customers for open sales orders. I ran it individually for each of our franchises and the number of records returned didn't vary all that much (approx. 10 - 100 records per run).

    Rob

  • I'm running on a dev box and I'm the only one on it, so I can do a check point and flush caches ect. to get reasonable timings.

    I changed the report's dataset from SQL (text) to executing a stored procedure and I still get similar results. When I execute the stored proc in SSMS, it executes in 5-8 seconds, but the report takes 4 minutes to generate.

    That sure makes it seem like it's something in SSRS and dynamic SQL as I first thought.

    Rob

  • We ended up changing the SharePoint "Manage Processing Options" setting to allow the report to run an unlimited time. This didn't speed anything up, but it did stop the report from "timing out".

    Rob

  • Rob --

    Two things:

    1) Did you actually modify the query to handle parameter sniffing? From my own experience, I've had situations where parameter sniffing was the problem -- modifying a stored proc to include a declared variable that gets assigned the parameter value that is initially passed to the stored proc solved my performance problems.

    2) I've also had problems with a slow report if the sql was returning a dataset as "Select * From ...." Changing the "*" to an explicit list of fields has proven beneficial.

    --pete

  • Could be a difference in SET options between SSMS & SSRS? (I'm thinking ARITHABORT)

    You can query the TimeDataRetrieval, TimeProcessing, and TimeRendering fields in ReportServer.dbo.ExecutionLog view to find what's taking all the time.

  • Pete,

    Thanks for your feedback.

    peterzeke (8/14/2013)


    1) Did you actually modify the query to handle parameter sniffing? From my own experience, I've had situations where parameter sniffing was the problem -- modifying a stored proc to include a declared variable that gets assigned the parameter value that is initially passed to the stored proc solved my performance problems.

    Yes; at the top of the stored proc, I set local declared variables equal to the parameters and then use the local variables in the WHERE clause of the query.

    2) I've also had problems with a slow report if the sql was returning a dataset as "Select * From ...." Changing the "*" to an explicit list of fields has proven beneficial.

    No SELECT * in the query either -- I explicitly list the fields.

    Thanks,

    Rob

  • Gazareth (8/14/2013)


    Could be a difference in SET options between SSMS & SSRS? (I'm thinking ARITHABORT)

    In my SSMS, I have the SET ARITHABORT checked to on in Tools,Options. I'm not sure where I check that setting in SSRS.

    You can query the TimeDataRetrieval, TimeProcessing, and TimeRendering fields in ReportServer.dbo.ExecutionLog view to find what's taking all the time.

    We have SSRS integrated with SharePoint, so when I query the dbo.Execution log from the SP_ReportServerIntegrated db. Interesting that the only report that shows a Status <> rsSuccess is this particular one with the query issues. I'm going to do more research into these execution times.

    Thanks,

    Rob

  • Thanks Rob, SSMS does have arithabort on by default, I think SSRS has it off.

    Any easy way to check if it could be responsible is to run this in SSMS:

    SET ARITHABORT OFF

    GO

    EXEC procname

    and see if the performance reflects what you see in SSRS.

    You can also check the SET options of cached plans via this query:

    SELECT s.name, pa.value

    FROM sys.procedures s

    INNER JOIN sys.dm_exec_procedure_stats d ON s.object_id = d.object_id

    OUTER APPLY sys.dm_exec_plan_attributes(d.plan_handle) pa

    WHERE pa.attribute = 'set_options'

    pa.value will give you the set options used in the proc plan as seen here: http://technet.microsoft.com/en-us/library/ms189472(v=sql.105).aspx

    I haven't used SSRS with sharepoint so not sure on grabbing execution data from there.

    Cheers

  • Thanks to Koen, Pete and Gazareth -- it turns out that it was parameter sniffing causing the problem.


    As Paul Harvey used to say, here's the rest of the story:

    SSRS report has run fine for past couple of months (5-10 seconds to come up); after a rollout of an update to our LOB app over the weekend, it's now taking "forever" (2-3 hours) for this same report to run. Tempdb is filling up, users are complaining, dogs and cats living together, ... :w00t: So we yanked the report down. The other 40+ reports are running fine -- just this one seems to have lost it's mind.

    Running the query from SSMS pointing to any environment ran fine. Running the report from Test SharePoint pointing to any environment (including production) ran find (5-10 seconds). Only the combination of running report on production SharePoint pointing to prod database caused horrible delays. Querying the SP_ReportServerIntegrated database was showing a 1000X increase in the report's data retrieval from the previous week.

    I had switched the report from T-SQL text in the report to a stored procedure that specifically mapped the parameters to internally declared variables to get around the parameter sniffing issue. Of course this worked fine and dandy in all of the environments I tested in. So I didn't think initially it was a param sniffing problem. Eventually in a maintenance window, I was able to run the report using the new stored procedure on prod SharePoint pointing to prod db and performance was back to usual speeds. So it was a problem with parameter sniffing.

    The good news is that everything is working fine now.

    Thanks,

    Rob

  • Well done, Rob! (..and nice reference to Paul Harvey)

    I can't imagine a report running from seconds to hours, and of course, only in production (arrg!) Performance problems due to parameter sniffing can suddenly appear in SSRS so keep an eye out for this issue in the future for other reports, especially if the underlying query is altered.

    --Pete

  • Great! Glad that you got it solved and thanks for posting back!

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply