Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query is slow from stored procedure but faster outside the procedure Expand / Collapse
Author
Message
Posted Tuesday, August 20, 2013 5:49 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:20 PM
Points: 231, Visits: 722
I have a query that joins 3-4 tables out of which there are 2 temp tables. It is a simple join query and there are no parameters/variable passeds to the query.

Query runs faster when run outside the stored procedure. If I put the same query as a stored procedure without any parameters, it is running slower.

Query plan in both cases is same and amount of logical reads also same and set options also same.

Any idea what could be the root cause behind this?

Thanks.
Post #1486195
Posted Tuesday, August 20, 2013 7:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:54 PM
Points: 13,228, Visits: 12,705
praveen_vejandla (8/20/2013)
I have a query that joins 3-4 tables out of which there are 2 temp tables. It is a simple join query and there are no parameters/variable passeds to the query.

Query runs faster when run outside the stored procedure. If I put the same query as a stored procedure without any parameters, it is running slower.

Query plan in both cases is same and amount of logical reads also same and set options also same.

Any idea what could be the root cause behind this?

Thanks.


From this vague description it is impossible to tell. Can you post the query, the actual execution plan, the table ddl including indexes?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1486256
Posted Tuesday, August 20, 2013 3:39 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 816, Visits: 742
I don't believe you when you say that the query plans are identical.

This article on my web site covers this situation and should get you going: http://www.sommarskog.se/query-plan-mysteries.html.


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1486457
Posted Tuesday, August 20, 2013 10:44 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:20 PM
Points: 231, Visits: 722
Please find the query plans attached. Before posting to the forum, I have already gone through your article and spent lot of time but could not find the root cause.

Thanks a lot for the reply.


  Post Attachments 
plans.rar (22 views, 15.96 KB)
Post #1486507
Posted Wednesday, August 21, 2013 2:21 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 816, Visits: 742
The plan files are indeed very similar. However, I disagree when you say that the query is something simple. I don't see the full query, but I see that there are umpteen calls to a scalar function which gives me an uncanny feeling.

Could you post the full text of the stored procedure, as well as the function?


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1486927
Posted Thursday, August 22, 2013 12:41 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:20 PM
Points: 231, Visits: 722
I have sent the stored procedure and SQL query to you in an e-mail.

Thanks.
Post #1487074
Posted Thursday, August 22, 2013 1:34 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 816, Visits: 742
There is something which is amiss here. The code you sent me had no parameters or variables. However, the plans you posted has:

<ColumnReference Column="@inWorksheetPk" ParameterCompiledValue="'87fbac050a07009a3aa818b91059edc9'" ParameterRuntimeValue="'87fbac050a07009a3aa818b91059edc9'" />
<ColumnReference Column="@inExcelLibraryId" ParameterCompiledValue="(24.)" ParameterRuntimeValue="(24.)" />

A quick glance indicates that the plans are for a similar query to the queries you mailed me, so I guess you played around.

Nevertheless, it is difficult to help with conflicting information.

When you ran the code with parameters/variables, did you get a noticeable difference in performance? Or do you only get that when you run without parameters? What difference are we talking about?

Can you post the code to the user-defined function? (It's much better to post the code here than mailing it to individuals, so that others can join in.)


Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1487490
Posted Friday, August 23, 2013 4:08 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 10:20 PM
Points: 231, Visits: 722
It seems I picked up a wrong plan for the e-mail attachment. I tried re-creating the issue but surprisingly, it is not reproducible now.

I will get back to you if issue repeats again.

Thanks.
Post #1487715
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse