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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:15:42+00:00 2026-06-15T19:15:42+00:00

I just read part of an optimization article and segfaulted on the following statement:

  • 0

I just read part of an optimization article and segfaulted on the following statement:

When using SQL replace statements using OR with a UNION:

select username from users where company = ‘bbc’ or company = ‘itv’;

to:

select username from users where company = ‘bbc’ union
select username from users where company = ‘itv’;

From a quick EXPLAIN:

Using OR:

enter image description here

Using UNION:

enter image description here

Doesn’t this mean UNION does in double the work?

While I appreciate UNION may be more performant for certain RDBMSes and certain table schemas, this is not categorically true as the author suggestions.

Question

Am I wrong?

  • 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-15T19:15:43+00:00Added an answer on June 15, 2026 at 7:15 pm

    Either the article you read used a bad example, or you misinterpreted their point.

    select username from users where company = 'bbc' or company = 'itv';
    

    This is equivalent to:

    select username from users where company IN ('bbc', 'itv');
    

    MySQL can use an index on company for this query just fine. There’s no need to do any UNION.

    The more tricky case is where you have an OR condition that involves two different columns.

    select username from users where company = 'bbc' or city = 'London';
    

    Suppose there’s an index on company and a separate index on city. Given that MySQL usually uses only one index per table in a given query, which index should it use? If it uses the index on company, it would still have to do a table-scan to find rows where city is London. If it uses the index on city, it would have to do a table-scan for rows where company is bbc.

    The UNION solution is for this type of case.

    select username from users where company = 'bbc' 
    union
    select username from users where city = 'London';
    

    Now each sub-query can use the index for its search, and the results of the subquery are combined by the UNION.

    An anonymous user proposed an edit to my answer above, but a moderator rejected the edit. It should have been a comment, not an edit. The claim of the proposed edit was that UNION has to sort the result set to eliminate duplicate rows. This makes the query run slower, and the index optimization is therefore a wash.

    UNION does have to eliminate duplicates, and to do that it sorts the result set, so there is some overhead. But it usually only has to sort a small result set.

    There might be cases where the WHERE clauses match a significant portion of the table, and sorting during UNION is as expensive as simply doing the table-scan. But it’s more common for the result set to be reduced by the indexed searches, so the sorting of a small set of rows is much less costly than the table-scan of a large set of rows.

    The difference depends on the data in the table, and the terms being searched. The only way to determine the best solution for a given query is to try both methods in the MySQL query profiler and compare their performance.

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

Sidebar

Related Questions

I've just read about @Resource annotation from this article ( http://www.infoq.com/articles/spring-2.5-part-1 ) and wish
I just read an article on www.songho.ca which indicates that a projection matrix is
I just read Ben Alman 's article on Immediately-Invoked Function Expressions and wondered about
I just read this article and I pretty much got it, although there is
I just read a MSDN article, Synchronization and Multiprocessor Issues , that addresses memory
I am just trying to read some details from an XML file, part of
The first part is now working [ I have the following which just seems
I just read on this answer (answer has since been removed) and I'm sure
I just read in Code Complete that you should not use exceptions for flow
I just read a blog which says ...methods that couldn’t be inlined previously because

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.