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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 320k
  • Answers 320k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A kind person from the Eclipse SWT mailing list gave… May 14, 2026 at 12:22 am
  • Editorial Team
    Editorial Team added an answer Use two apostrophes '' to get a single apostrophe on… May 14, 2026 at 12:22 am
  • Editorial Team
    Editorial Team added an answer I've not tried it myself but you might want to… May 14, 2026 at 12:22 am

Related Questions

When I start my application I get: The ConnectionString property has not been initialized.
I am trying to convert the following Perl regex I found in the Video::Filename
The ASP.NET MVC 1.0 (final) project template has basic membership built in, but I
I´m having some trouble deciding in which way to go... I've got this application
in webforms I would always use my masterpage to set page titles and meta

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.