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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:46:35+00:00 2026-05-14T20:46:35+00:00

I’m executing the following query SELECT COUNT(*) FROM table WHERE field1=’value’ AND (field2 >=

  • 0

I’m executing the following query

SELECT COUNT(*)
FROM table
WHERE field1='value' AND (field2 >= 1000 OR field3 >= 2000)

There is one index over field1 and another composited over field2&field3.

I see MySQL always selects the field1 index and then makes a join using the other two fields which is quite bad because it needs to join 146.000 rows.

Suggestions on how to improve this? Thanks

(EDIT AFTER TRYING SOLUTION PROPOSED)

Based in the solution proposed I’ve seen this on Mysql when playing with this.

SELECT COUNT(*) FROM (SELECT * FROM table WHERE columnA = value1
UNION SELECT * FROM table WHERE columnB = value2) AS unionTable;

is a lot slower than execute:

SELECT COUNT(*)
FROM table
WHERE (columnA = value1 AND columnB = value2)
      OR (columnA = value1 AND columnC = value3)

Having two composited index:

index1 (columnA,columnB)
index2 (columnA,columnC)

Interesting enough is that asking Mysql to “explain” the query it’s taking always index1 on both cases and index2 is not used.

If I change the indexes to:

index1 (columnB,columnA)
index2 (columnC,columnA)

And the query to:

SELECT COUNT(*)
FROM table
WHERE (columnB = value2 AND columnA = value1)
      OR (columnC = value3 AND columnA = value1)

Then it’s the fastest way I’ve found Mysql works.

  • 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-14T20:46:36+00:00Added an answer on May 14, 2026 at 8:46 pm

    The typical way to break up OR predicates is with UNION.

    Note that your example doesn’t fit well with your indexes. Even if you omitted field1 from the predicate, you’d have field2 >= 1000 OR field3 >= 2000, which can’t use an index. If you had indexes on (field1, field2) and (field1,field3) or field2 or field3 separately, you would get a reasonably fast query.

    SELECT COUNT(*) FROM
    (SELECT * FROM table WHERE field1 = 'value' AND field2 >= 1000
    UNION
    SELECT * FROM table WHERE field1 = 'value' AND field3 >= 2000) T
    

    Note that you have to provide an alias for the derived table, which is why the subquery is aliased as T.

    A real-world example. Column and table names have been anonymized!

    mysql> SELECT COUNT(*) FROM table;
    +----------+
    | COUNT(*) |
    +----------+
    |  3059139 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> SELECT COUNT(*) FROM table WHERE columnA = value1;
    +----------+
    | COUNT(*) |
    +----------+
    |     1068 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> SELECT COUNT(*) FROM table WHERE columnB = value2;
    +----------+
    | COUNT(*) |
    +----------+
    |      947 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> SELECT COUNT(*) FROM table WHERE columnA = value1 OR columnB = value2;
    +----------+
    | COUNT(*) |
    +----------+
    |     1616 |
    +----------+
    1 row in set (9.92 sec)
    
    mysql> SELECT COUNT(*) FROM (SELECT * FROM table WHERE columnA = value1
    UNION SELECT * FROM table WHERE columnB = value2) T;
    +----------+
    | COUNT(*) |
    +----------+
    |     1616 |
    +----------+
    1 row in set (0.17 sec)
    
    mysql> SELECT COUNT(*) FROM (SELECT * FROM table WHERE columnA = value1
    UNION ALL SELECT * FROM table WHERE columnB = value2) T;
    +----------+
    | COUNT(*) |
    +----------+
    |     2015 |
    +----------+
    1 row in set (0.12 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 418k
  • Answers 418k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer hopes this code can explain it to you... html <div>… May 15, 2026 at 9:54 am
  • Editorial Team
    Editorial Team added an answer Have a NSView-subclass that represents an oval (say JBOval) that… May 15, 2026 at 9:54 am
  • Editorial Team
    Editorial Team added an answer I don't personally think it too surprising to see what… May 15, 2026 at 9:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.