SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query is slow from stored procedure but faster outside the procedure


Query is slow from stored procedure but faster outside the procedure

Author
Message
praveen_vejandla
praveen_vejandla
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 763
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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26119 Visits: 17538
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 Modens 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)
Erland Sommarskog
Erland Sommarskog
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2113 Visits: 872
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
praveen_vejandla
praveen_vejandla
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 763
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.
Attachments
plans.rar (26 views, 15.00 KB)
Erland Sommarskog
Erland Sommarskog
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2113 Visits: 872
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
praveen_vejandla
praveen_vejandla
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 763
I have sent the stored procedure and SQL query to you in an e-mail.

Thanks.
Erland Sommarskog
Erland Sommarskog
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2113 Visits: 872
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
praveen_vejandla
praveen_vejandla
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 763
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search