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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:53:37+00:00 2026-06-14T09:53:37+00:00

In MYSQL, some code from a WordPress plugin that I don’t want to change

  • 0

In MYSQL, some code from a WordPress plugin that I don’t want to change too much is running the following query:

SELECT * WHERE cond1 AND (field1 = val1 OR field2 = val2)

However, it runs very slowly despite that there are indexes on val1 and val2. (the slow query log confirms that it scans all rows.) Can I hint MYSQL that it should always expand the formula to the following equivalent but faster form?

SELECT * WHERE cond1 AND (field1 = val1)
UNION
SELECT * WHERE cond1 AND (field2 = val2)

This would reduce the number of rows scanned dramatically, so it would yield vastly superior performance. I also wonder the same about

SELECT * WHERE cond1 AND (field1 in (val1, val2))

Thanks!

Edit: some info about the table and the query explanation are here at http://pastebin.com/Qd1ZaVKD but it seems inconsistent. If I run the query from myphpadmin sometimes a slow query log entry is generated and sometimes not, even if it continues to be generated when other users are causing such a row.

  • 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-14T09:53:38+00:00Added an answer on June 14, 2026 at 9:53 am

    It first depends on cond1, and then on the cardinality of field1 and field2 under that condition:

    1. If cond1 involves direct comparisons of columns with constant values (i.e. an index can help to resolve it), then a composite index with field1 and/or field2 may help (see below).

      If cond1 involves manipulations on columns—e.g. applying a function or other operations, such as my_int + 5 = 3 or DATE(my_timestamp) > NOW()—then an index cannot help; however, note that both of those examples can be rewritten to be index-friendly:

      • my_int = 3 - 5, which is obviously equivalent to my_int = -2; and

      • my_timestamp >= CURDATE() + INTERVAL 1 DAY.

    2. It’s only ever worth creating indexes that have relatively high cardinality (i.e. can quickly distinguish between many records), otherwise using it will be little better than a full table scan whilst slowing down table write operations and consuming additional storage and memory space. Consider not only the cardinality of cond1, field1 and field2, but also that of cond1 together with each of the fields.

      Assuming that they all have high cardinality, your best bet would be to achieve an index_merge (union access) with two composite indexes on each of (cond1, field1) and (cond1, field2).

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

Sidebar

Related Questions

I have some code that pulls data from a mysql db everything is pulling
I try to change my code from MYSQL to SQL Server. I have some
I some php code which gets info from a mysql database. The problem is
I have some code to get a group of results from mysql, please see
I have some SQL code that works great and returns the desired results from
I am using $.post to load some js code from a MYSQL database. How
I am pulling some data from a mysql table via the following: $result =
I am trying to port some code from MS-SQL to MySQL and there is
For some reason when I changed my php code from mysql to mysqli everything
I am converting some of my code from the older mysql extension to the

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.