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

The Archive Base Latest Questions

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

An article about Optimizing your SQL queries has suggested to use Union insted of

  • 0

An article about Optimizing your SQL queries has suggested to use Union insted of OR `cause:

Utilize Union instead of OR

Indexes lose their speed advantage when using them in OR-situations in
MySQL at least. Hence, this will not be useful although indexes is
being applied 1 SELECT * FROM TABLE WHERE COLUMN_A = ‘value’ OR
COLUMN_B = ‘value’

On the other hand, using Union such as this will utilize Indexes.
1- SELECT * FROM TABLE WHERE COLUMN_A = ‘value’
2- UNION
3- SELECT * FROM
TABLE WHERE COLUMN_B = ‘value’

How much this suggestion is true? Should I turn my OR queries to Union?

  • 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-02T03:15:26+00:00Added an answer on June 2, 2026 at 3:15 am

    I do not recommend this as a general rule. I don’t have MySQL installed here so can’t check the generated execution plan, but certainly in Oracle the plans are much different and the ‘OR’ plan is more efficient than the ‘UNION’ plan. (Basically, the ‘UNION’ has to perform two SELECT’s and then merge the results – the ‘OR’ only has to do a single SELECT). Even a ‘UNION ALL’ plan has a higher cost than the ‘OR’ plan. Stick with the ‘OR’.

    I very strongly recommend writing the clearest, simplest code you possibly can. To me, that means you should use the ‘OR’ rather than the ‘UNION’. I’ve found over the years that attempting to “pre-optimize” something, or in other words to guess where I’ll encounter performance problems and then to try and code around those problems, is a waste of time. Code tends to spend a lot of time in the darndest places, and you’ll only find them once you’ve got your code running. A long time ago I learned three rules that I’ve found to be useful in this context:

    1. Make if run.
    2. Make it run right.
    3. Make it run right fast.

    Optimization is literally the last thing you should be doing.

    Share and enjoy.


    Followup: I hadn’t noticed that the ‘OR’ was looking at different columns (my bad), but my advice regarding “keep it simple” still holds.

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

Sidebar

Related Questions

First off, I've looked at several other questions about optimizing sql queries, but I'm
I have read this article about 400% boost of your website . This is
An user posts this article about how to use HttpResponse.Filter to compress large amounts
I once saw an article about how to use a specific type of numbering
Below is an article about .net framework's use of patterns. I'm not sure I
After reading an interesting article about unit testing behavior instead of state, I came
I read an article about using CLR integration in sqlserver and was wondering what
Today I encountered this article about decimal expansion and I was instantaneously inspired to
I was reading an article about how query expressions defer executions. Does that mean
I read the msdn article about disposing objects in http://msdn.microsoft.com/en-us/library/ee557362(office.14).aspx now I'm really confused

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.