Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:06:29+00:00 2026-06-10T00:06:29+00:00

I am using a stored procedure to that returns a set of rows about

  • 0

I am using a stored procedure to that returns a set of rows about the data in a varbinary column. I cannot, for the life of me, figure out why the query times always increase at the same rate when I use sp_executesql as opposed to exec.

Query with sp_executesql times go: <1s, 1s, 2s, 4s, 8s, 24s (etc) until, eventually, the problem “goes away” for a few minutes and the query is always ~0.006s

Query times with exec are always <500ms.

At first, I thought this was a problem with Linq2Sql, but when we ran the query Linq produces in SQL Management Studio, we got the same result.

Here’s the body of the sproc (heavily censored)

Note that the varbinary column is not being pulled

select DF.FileID, 
       DF.DerpFkGuid, 
       DF.DerpName, 
       DF.[FileName],
       DF.FileSize as 'FileSizeBytes', 
       ISNULL(DFA.File_Size_Bytes_String,'Unknown') as 'FileSizeFriendly', 
       CONVERT(nvarchar(30),DF.FoundDate,120) as 'FileDateUploaded', 
       CONVERT(nvarchar(30),DF.FileDateCreated,120) as 'FileDateCreated',
       CONVERT(nvarchar(30),DF.FileDateLastModified,120) as 'FileDateModified',
       CASE WHEN ISNULL(dfa.Derp_ID,'00000000-0000-0000-0000-000000000000')='00000000-0000-0000-0000-000000000000' THEN 0 ELSE 1 END as 'AttachedToDerp', 
       ISNULL(dfa.Derp_ID,'{00000000-0000-0000-0000-000000000000}') as 'Derp_ID'
from DerpFiles DF   
inner join DFDerpDerp DFdd      on DFdd.DerpName = DF.DerpName
left outer join Derp_Files_Attachments dfa      on dfa.FileID =  DF.FileID
where WR.UserName = @UserName and DF.DuplicateFileDetected=0

Here is the code that runs consistently < 500ms

USE [DerpDatabase]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[DerpSproc]
        @UserName = N'derp',
        @DomainName = N'derp'

SELECT  'Return Value' = @return_value

GO

Here is the code that runs 1s ,2s ,4s, 8s , 24s, etc

declare @p5 int
set @p5=0
exec sp_executesql N'EXEC   @RETURN_VALUE = [dbo].[DerpSproc] 
                            @UserName = @p0, 
                            @DomainName = @p1',                         
                            N'@p0 nvarchar(4000),
                            @p1 nvarchar(4000),
                            @RETURN_VALUE int output',
                            @p0=N'derp',
                            @p1=N'derp',
                            @RETURN_VALUE=@p5 output
select @p5

As far as I can tell, the 2 ways the sprocs are being called are essentially the same. The only difference is sp_executesql vs exec

It also appears that exec will produce the same increasing query times without the go keyword at the end. I can’t tell for sure because I don’t know if the query just happened to cache when I tried adding go?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:06:30+00:00Added an answer on June 10, 2026 at 12:06 am

    This might be a parameter sniffing issue. Sometimes when you pass parameters into a procedure, SQL has a hard time figuring out how to use them efficiently. Try setting your parameters to local variables in the procedure, and then use the local variables in the where clause.

    Parameter Sniffing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that returns a result set (4 columns x n
We've been given a stored procedure from our RPG folks that returns six data
I have a stored procedure that, depending on the input, returns multiple rows of
I'm trying to call a stored procedure that returns multiple result sets using SQLAlchemy.
Using Linq to call a stored procedure that passes a single string, The stored
I have a stored procedure that updates a table using linq, eg: (this is
I am using SQL Server. I'm writing a stored procedure that executes a series
I m using a stored procedure and there i m having a column Name
I have a Stored Procedure that returns three record sets. Here it is being
I have imported a stored procedure that returns a dataset into my Entity Framework.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.