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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:20:15+00:00 2026-06-02T18:20:15+00:00

I have a query that looks like this: SELECT * FROM A INNER JOIN

  • 0

I have a query that looks like this:

SELECT *
FROM A
INNER JOIN B ON A.AId = B.AId
WHERE A.ADate BETWEEN @Start and @End
     or B.BDate BETWEEN @Start and @End

Both tables A and B are about the same size and have a lot of rows. Execution plan shows a index seek, but looks like it is scanning the entire index.

If I change the or to and then the query is very fast. I think this is due to the fact that the result of the or cannot be known without performing a table scan on both tables to compute the or. The and is easily split into two operations.

I have read some people stating that it is possible to use UNION in place of or, but this would potentially introduce duplicate rows in the case that both conditions in the OR are true.

What solution is there so that I can reduce the size of join and prevent a full join of both tables? I am open to restructuring the query however possible to make this work, but need the logic of the query(give me items where either the date in a matches the range or the date in B matches the range) to remain the same.

  • 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-02T18:20:16+00:00Added an answer on June 2, 2026 at 6:20 pm

    Thanks for the answers, in the end I opted for UNION ALL, and I crafted a query based on the union of two selects that are mutually exclusive, so no duplicates would be introduced in the UNION ALL.

    First, get all the rows where ADate is in the range, and exclude rows where BDate is in the range. Then get all the rows where BDate is in the range. The union of these two sets logically produces the set of rows that covers ADate or BDate, without double counting the middle(so a UNION ALL will not produce duplicates). Let me know if you see a flaw in this logic, I found it helpful to think of a venn diagram.

    This made the query perform the best of the options presented(in my case), and wasn’t overly complicated, so I went with it.

    SELECT *
    FROM A
    INNER JOIN B ON A.AId = B.AId
    WHERE A.ADate BETWEEN @Start and @End
       and B.BDate NOT BETWEEN @Start and @End
    
    UNION ALL
    
    SELECT *
    FROM A
    INNER JOIN B ON A.AId = B.AId
    WHERE B.BDate BETWEEN @Start and @End
    

    Perhaps this could be a query optimization for the OR operator in some scenarios, especially when querying separate, large tables, it works with date ranges, but could work with any other predicates I imagine.

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

Sidebar

Related Questions

I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I have an NHibernate query that looks like this: var query = Session.CreateQuery(@ select
I have a Linq query that looks something like this: var myPosse = from
I have a Linq query that looks something like this: var query = from
I have a general query that looks like this: SELECT DISTINCT pb.id, pb.last, pb.first,
I have a query like this: SELECT c.id, c.name, f.name FROM companies c INNER
I have a query that looks like the following SELECT t1.Name, t2.ID, t2.name FROM
I have a query that looks like this: it takes a list of IDs
Quick question... I have a query that checks for duplicates that looks like this:

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.