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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:38:59+00:00 2026-05-11T23:38:59+00:00

I am using this condition as part of my where clause: exists (select *

  • 0

I am using this condition as part of my where clause: exists (select * from PRPB PB where PA.mid = PB.mid and (@inpo is null or PB.inpo = @inpo)) order by price.

While testing non-null @inpo values, I noticed the query runs much faster when I instead use this condition: exists (select * from PRPB PB where PA.mid = PB.mid and (PB.inpo = @inpo)) order by price. That this causes a non-neglible speed difference suggests that I will be forced to use two separate queries with an if statement in order to decide whether to filter out by @inpo or not. This strikes me as a bad thing, since it means a lot of code repetition.

Things I have tried:

  • Creating a bit that stores whether @inpo is non-null and comparing with that.
  • Moving the nullity check to the left of the exists statement and doing an or with the whole thing (this slows things down a lot, which surprises me).
  • Moving the nullity check to the leftmost of the where class and oring it with all the clauses (this also slows things down a lot, which doesn’t surprise me at all, since it means the nullity check always happens, regardless of whether or not PA.mid = PB.mid).

My goal is to have it perform this check much faster without having two copies of my query.

Is this possible? If not, why not? If so, how?

Note: See also a 2nd related question here.

  • 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-11T23:38:59+00:00Added an answer on May 11, 2026 at 11:38 pm

    To reuse code without paying performance penalty you can either create a view or create an inline UDF. Both are macros that are expanded by the optimizer. For example instead of the following duplication of code:

    CREATE PROCEDURE MyProc
      @i1 INT,
      @inpo INT
    AS
    BEGIN
    IF @inpo IS NULL BEGIN
      SELECT a,b,c 
        FROM dbo.YourTable 
        WHERE i1 = @i1 
        ORDER BY c;
    END ELSE BEGIN
      SELECT a,b,c 
        FROM dbo.YourTable 
        WHERE i1 = @i1 
          AND inpo = @inpo
        ORDER BY c;
    END
    END
    

    wrap the query in an inline udf and reuse it:

    CREATE FUNCTION dbo.ReuseMyQuery(@i1 INT)
    RETURNS TABLE AS RETURN(
    SELECT a,b,c, inpo FROM dbo.YourTable WHERE i1 = @i1
    )
    GO
    
    ALTER PROCEDURE MyProc
      @i1 INT,
      @inpo INT
    AS
    BEGIN
    IF @inpo IS NULL BEGIN
      SELECT a,b,c 
        FROM dbo.ReuseMyQuery(@i1)
        ORDER BY c;
    END ELSE BEGIN
      SELECT a,b,c 
        FROM dbo.ReuseMyQuery(@i1)
        WHERE inpo = @inpo
        ORDER BY c;
    END
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a small piece of code shown below which is using do while
Inside a compiled LINQ query as a condition part, I was using Datetime.Now .
I am using this in part of my template: {{if !IsDefault}} <a href=# onclick=makeDefault('${Id}');return
Using this pipeline I can play two of the same videos using the videomixer,
Using this program, I'm trying to set a 6x15 array to 0, then place
Using this question as the base is there an alogrithm or coding example to
Using this file as source, I have a situation where I need to retrieve
Using this answer , I created a sample localized app. My question is, is
Using this: Can I use Facebook's fb:friend-selector in an iframe? I created a multi-friend
Using this xslt file found on this blog to pretty print xml using Nokogiri,

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.