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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:01:49+00:00 2026-05-23T15:01:49+00:00

Question pertains to performance and best practice. (sql server 2008 stored procedure) If a

  • 0

Question pertains to performance and best practice. (sql server 2008 stored procedure)

If a stored proc contains code like this

result = select count(*) from tableA where (some condition1)

if (result) -- meaning we got something from above query
   return result
else
   result = select count(*) from tableA where (some other condition2)
and so on until
result = select count(*) from tableA where (some other conditionN)

Is it better to put all the conditions in one big query like this

   resultn = select count(*) from tableA 
   where (condition1 or condition1 or... conditionN)

Will a table scan be done for every condition or is the table scan done once no matter how many conditions. What is an elegant way to handle this type of logic. thanks

  • 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-23T15:01:50+00:00Added an answer on May 23, 2026 at 3:01 pm

    Your two code blocks are not logically equivalent. I’m going to assume that IF(result) really means, IF(@result > 0). In that case the first non-zero count that you find will be returned. In your second code block you will actually get back the total of all of the counts for all of your criteria.

    In your second code block SQL Server can only pick one query plan for the query. If you have indexes on all of your columns it can still only use one of them. I don’t think that it will do parallel processing using an index for each query condition. I haven’t specifically tested this though, so maybe someone can correct me if I’m wrong. You might be able to alleviate some of the problem with this by using the WITH RECOMPILE directive in your stored procedure. This had some bugs in SQL 2005, but since you’re on SQL 2008 you should be OK.

    In your first code block SQL Server can have separate query plans for each query.

    In any case, which one is actually best is going to depend on a lot of factors. Your best bet is going to be to test them side by side with test data that pretty closely mimics your actual production data.

    Also, don’t forget about maintainability and readability of code. If this is code that isn’t run often or if the performance difference is small enough then you might be better off using the code that is most easily maintained and understood, especially if you have a lot of conditions or might be changing them often.

    Finally, do the conditions rely on parameters of the stored procedure? Will some of these parameters often be left out implying that the condition is not relevant? If so, then you should very carefully read this article on dynamic search conditions by Erland Sommarskog. At one client I found dynamic SQL to be the most performant method by far for this situation, but of course a lot of care had to be taken to make this code airtight when it came to security.

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

Sidebar

Related Questions

This is an efficiency/best practice question. Hoping to receive some feed back on performance.
This question pertains primarily to good design. Suppose I have a controller action like
To all: This question pertains to some MS research I am doing. What I
This question pertains to xperf and xperfview, utilities that are part of the Windows
This question pertains to Ruby on Rails and PHP. When looking for a VPS
This question pertains specifically to shell scripts, but could be about any programming language.
This question pertains to query optimization using Linq with the Entity Framework. Is there
I'd like to drop a user in a SQL Server script but I'll need
This question pertains not only to how to accomplish this task, but to whether
This may not really be a Python related question, but pertains to language encoding

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.