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 6887649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:37+00:00 2026-05-27T05:54:37+00:00

This question is not so much about finding a solution as about getting an

  • 0

This question is not so much about finding a solution as about getting an explanation for the bizarrest behavior I have ever seen from SQL Server.

I had a stored procedure with the following signature:

alter procedure MySP @param1 uniqueidentifier, 
                     @param2 uniqueidentifier, 
                     @param3 uniqueidentifier

Given a certain set of parameters, this proc was taking a very long time to run from C# (using SqlCommand.ExecuteReader()) – about 2 minutes. Using the same parameters in a direct query session, the SP ran in under 2 seconds.

It took a long time, and I won’t even try to explain how we stumbled upon this solution, but this is what we did:

At the beginning of the SP, we declared 3 local variables and assigned them to the values of the parameters, like so:

declare @param1_copy uniqueidentifier, 
        @param2_copy uniqueidentifier, 
        @param3_copy uniqueidentifier

select @param1_copy = @param1,
       @param2_copy = @param2,
       @param3_copy = @param3

And then, in the rest of the SP we substituted all references to the input parameters with the local copies.

Voila. SP executed in under 2 seconds. And the team here is gobsmacked.

Now, ladies and gentlemen, can anyone explain this behavior?

  • 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-05-27T05:54:38+00:00Added an answer on May 27, 2026 at 5:54 am

    This sounds like parameter sniffing.

    From Microsoft’s definition:

    “Parameter sniffing” refers to a process whereby SQL Server’s execution environment “sniffs” the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word “current” refers to the parameter values present in the statement call that caused a compilation or a recompilation.

    Looks like you’ve already figured out one fix, another would be to use EXEC… WITH RECOMPILE:

    When executing a stored procedure with atypical parameter values, “EXEC … WITH RECOMPILE” can be used to ensure that the fresh query plan does not replace an existing cached plan that was compiled using typical parameter values.

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

Sidebar

Related Questions

For starters, this question is not so much about programming in the NetBeans IDE
Not much to say about this question...
This question is not so much programming related as it is deployment related. I
The current top-voted to this question states: Another one that's not so much a
I'm sorry for this very newbish question, I'm not much given into web development.
This question is NOT about race-conditions, atomicity, or why you should use locks in
This question is not about which is the best, it is about which makes
This question may not directly relate to programming. I have noticed that the technology
This question is not about 'best' barcode library recommendation, we use various products on
I have a simple question about finding a regular expression for a given language.

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.