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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:44:36+00:00 2026-06-06T03:44:36+00:00

We usually encountered the case that select result that if not input argument or

  • 0

We usually encountered the case that select result that if not input argument or a column equals the argument, the expression like below comes to our brains:

WHERE @Arg IS NULL OR Name = @Arg

Almost all programming languages will ignore the second condition if @Arg is set to NULL. However, I find it was being executed in SQL Server very slowly and looked like the second condition always be went through.

Anyone could give me help?

  • 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-06T03:44:38+00:00Added an answer on June 6, 2026 at 3:44 am

    This is not a question of boolean short circuit (which is not guaranteed in SQL, see On SQL Server boolean operator short-circuit) but a problem of compilation. SQL Server will have to compile a query plan for you and the plan will have to work for any value of @Arg. This requirement will eliminate many possible optimizations (eg. if you have an index on Name it cannot be used), resulting in unnecessarily slow queries.

    This pattern is repeated again and again, typically in search forms, and the recommendation is to create different queries. The underlying problem is an incorrect API design that uses the same call to do different things (search by Name, search by ID etc).

    The article linked by Martin goes to great length explaining pros and cons of various approaches. Erland’s advice is more nuanced (use IF for simple cases, dynamic SQL for complex cases) but it boils down to the same thing: use different queries for each case, whether hard coded (IFs) or code-generated (dynamic SQL).

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

Sidebar

Related Questions

Usually it will ignore C files that are not documented, but I'd like to
Usually I was doing something like that (just a example): using (Stream xmlStream =
I have encountered a problem that I have never seen before. Usually, when I
I encountered a very strange behavior in Python, a behavior that is not consistent.
I have encountered a problem in my program that has me somewhat stumped. I
I am not used to C# (I do C++ usually) and try to debug
When unhandled exceptions are encountered in VStudio usually the debugger highlights the line YELLOW
We usually catch exception in the upper level of a code like the GUI
I encountered this term Hindley-Milner , and I'm not sure if grasp what it
I have a Switch activity that has an expression of escalateEngine.CheckRules() Where escalateEngine is

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.