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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:18:51+00:00 2026-05-13T07:18:51+00:00

Not sure where to start on this one — not sure if the problem

  • 0

Not sure where to start on this one — not sure if the problem is that I’m fooling the query optimizer, or if it’s something intrinsic to the way indexes work when nulls are involved.

One coding convention I’ve followed is to code stored procedures like such:

declare procedure SomeProc
  @ID int = null
as
  select
    st.ID,st.Col1,st.Col2
  from
    SomeTable st
  where
    (st.ID = @ID or @ID is null) --works, but very slow (relatively)

Not very useful in that simple test case, of course, but useful in other scenarios when you want a stored proc to act on either the entire table OR rows that meet some criteria. However, that’s quite slow when used on bigger tables… roughly 3-5x slower than if I replaced the where clause with:

where
    st.ID = @ID --3-5x faster than first example

I’m even more puzzled by the fact that replacing the null with -1 gives me nearly the same speed as that “fixed” WHERE clause above:

declare procedure SomeProc
  @ID int = -1
as
  select
    st.ID,st.Col1,st.Col2
  from
    SomeTable st
  where
    (st.ID = @ID or @ID=-1) --much better... but why?

Clearly it’s the null that’s making things wacky but why, exactly? The answer is not clear to me from examining the execution plan. This is something I’ve noticed over the years on various databases, tables, and editions of SQL Server so I don’t think it’s a quirk of my current environment. I’ve resolved the issue by switching the default parameter value from null to -1; my question is why this works.

Notes

  1. SomeTable.ID is indexed
  2. It may be related to (or may, in fact, be) a parameter sniffing issue
    Parameter Sniffing (or Spoofing) in SQL Server
    For whatever it’s worth, I’ve been
    testing almost exclusively with
    “exec SomeProc” after each
    edit/recompile of the proc, ie, with
    the optional parameter omitted.
  • 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-13T07:18:52+00:00Added an answer on May 13, 2026 at 7:18 am

    You have a combination of issues, most likely

    1. Parameter sniffing
    2. OR is not a good operator to use

    But without seeing the plans, these are educated guesses.

    Parameter sniffing

    … of the default “NULL”. Try it with different defaults, say -1 or no default.

    The @ID = -1 with a default of NULL and parameter sniffing = trivial check, so it’s faster.

    You could also try OPTIMISE FOR UNKNOWN in SQL Server 2008

    The OR operator

    Some ideas..

    If the columns is not nullable, in most cases the optimiser ignores the condition

    st.ID = ISNULL(@ID, st.ID)
    

    Also, you can use IF statement

    IF @ID IS NULL
       SELECT ... FROM...
    ELSE
       SELECT ... FROM... WHERE st.ID
    

    Or UNION ALL in a similar fashion.

    Personally, I’d use parameter masking (always) and ISNULL in most cases (I’d try it first)

    alter procedure SomeProc
      @ID int = NULL
    AS
    declare @maskID int
    select @maskID = @ID
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure where to start with this one but I'm hoping someone can
This one has me stumped. I'm not sure where to start on this one.
This one threw me for a loop - not even sure where to start
Not sure where to start with this one ... I'm thinking about creating a
I'm not really sure where to start with this one, but here is what
I do not know where to start with this one, not sure if it
not too sure where to start with this one, I'm very new to mvc.
Not really sure where to start on this one and a day of google
I'm not sure where to start with this one... my client gets stock figures
Not sure really where to start with this one. Can anyone help/point me in

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.