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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:28:04+00:00 2026-05-26T04:28:04+00:00

I just observed that my query: SELECT X.A, X.B, X.GroupName FROM TableA X INNER

  • 0

I just observed that my query:

SELECT X.A, X.B, X.GroupName
FROM TableA X
INNER JOIN TableB Y -- Huge table
ON (X.A = Y.Name OR X.B = Y.Name)

TableB has a CLUSTERED INDEX ON column Name because of which this query was taking hours to run. So what I did was to rewrite the query as:

SELECT X.A, X.B, X.GroupName
FROM TableA X
INNER JOIN TableB Y -- Huge table
ON X.A = Y.Name
UNION
SELECT X.A, X.B, X.GroupName
FROM TableA X
INNER JOIN TableB Y -- Huge table
ON X.B = Y.Name

This one runs in a few seconds or in the worst case, minutes. While I understand the reason now after burning myself, I was wondering if there is a cleaner way to write this query. I was thinking of a CTE but then the ON X.A = Y.Name and ON X.B = Y.Name are like parameters and am not sure how to deal with this.

My actual query is very big so I want to avoid repeating it two times for the sake of having a UNION. Any suggestions?

  • 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-26T04:28:04+00:00Added an answer on May 26, 2026 at 4:28 am

    In cases such as this it may be acceptable to use the UNION if the two conditions require using the index in different ways. By putting them as OR in a single condition you may be removing the ability to use the index.

    This is the same as the problem:

    SELECT MIN(myCol), MAX(myCol)
    

    By including both you may be borking a query plan’s use of the index as it tries to find the “best of both worlds” query rather than “the best of each world, individually, added together”

    Here is a (outdated) link which illustrates my point:
    http://code.cheesydesign.com/?p=279
    http://richardfoote.wordpress.com/category/index-full-scan-minmax/

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

Sidebar

Related Questions

I have a table x that's like the one bellow: id | name |
http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.min.js http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1-api.min.js Just observed that 2 javascript files are shipped in the distribution, what
I have a simple MVC 4 (Beta) Application and just observed something that I
Just looking for the first step basic solution here that keeps the honest people
Just how much slower are events? I have written a streaming XML parser (that
I was using NSDictionary and I observed that the objects in the dictionary are
While solving a DP related problem, I observed that first works but the second
A site that I'm working on has a ASP.NET webforms page that posts back
I have a question regarding using intptr_t vs. long int . I've observed that
I have a client-server app and i observed that (in a local environment i.e.

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.