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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:16:14+00:00 2026-05-23T12:16:14+00:00

I have 3 tables that I need to join, these join together fine using

  • 0

I have 3 tables that I need to join, these join together fine using indexes. However, we are transitioning over from using one legacy field as the identifier to another one in another table.
LEGACYID is that legacy field, while NEWID is the new field. Both fields are varchars.
Both fields are indexed exclusively with a btree index, both tables are MyISAM.

SELECT Username  
  FROM CUST C use index(primary,NEWID)
  JOIN TBLSHP S ON S.CUSID = C.CUSID
  JOIN TBLQ Q ON Q.SHPID = S.SHPID
 WHERE C.LEGACYID = '692041' 
    OR Q.NEWID = '692041'

This query takes 5.147 seconds, that’s 5 seconds longer than I expect.

When doing an EXPLAIN EXTENDED query the index type for NEWID is ALL i.e. full table scan , possible keys are (primary,NEWID) and key(null). If I remove the LEGACYID from the Or statement, explain says key (NEWID) will now be used. If I remove NEWID from the OR statement changes occur as following:

  • the type of the table joins for (S,C) change from type ref to eq_ref
  • key_len changes from 4 to 5 (on both)
  • extra changes from empty to “Using where” .
    With either one of the statements removed from the the OR statement the query runs at expected speeds.

Table Q has 183k records; C:115k; S:169k.
One last point. if I move the query placement:

   SELECT Username  
     FROM CUST C use index(primary,NEWID)
     JOIN TBLSHP  S ON S.CUSID = C.CUSID
LEFT JOIN TBLQ Q ON Q.SHPID = S.SHPID 
                AND Q.NEWID = '692041'
    WHERE C.LEGACYID = '692041' 

Although its not the same query, for the way the data works, it will provide the results I need, and the speed is down to under a .1 of a second again.

I did want to clarify that I don’t really need a query that works solution. Thanks to Ponies below that already has provided one. What I need to know is if anyone else has run into this problem and can explain why this is happening and what I can do for this simple or statement to use both indexes.

  • 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-23T12:16:15+00:00Added an answer on May 23, 2026 at 12:16 pm

    If you know there won’t be duplicates, change UNION to UNION ALL (UNION ALL is faster because it doesn’t remove duplicates). Otherwise, use:

    SELECT Username  
      FROM CUST C use index(primary,NEWID)
      JOIN TBLSHP S ON S.CUSID = C.CUSID
      JOIN TBLQ Q ON Q.SHPID = S.SHPID
     WHERE C.LEGACYID = '692041' 
    UNION
    SELECT Username  
      FROM CUST C use index(primary,NEWID)
      JOIN TBLSHP S ON S.CUSID = C.CUSID
      JOIN TBLQ Q ON Q.SHPID = S.SHPID
     WHERE Q.NEWID = '692041'
    

    ORs are notoriously bad performers, because it splinters the execution path. The UNION alleviates that splintering, and the combines the two results sets. That said, IN is preferable to ORs because though being logically the same, the execution of IN is generally more optimized.

    UNION isn’t always the answer

    Investigate many options, comparing the EXPLAIN PLAN output before determining a solution. I’ve come across a couple recently that perform better using a cursor than a single query using esoteric functionality.

    Also, make sure foreign key columns (what you’re using in the ON clause when JOINing) are indexed. MySQL has started (v5.5+?) to automatically do this when a foreign key constraint is made, but that’s only for InnoDB tables.

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

Sidebar

Related Questions

I have 2 tables that I need to load together all the time, the
I have the following code that will join two different tables together and provide
I have a few tables that I need to link together to get a
i have these two queries, that i want to join together, but am not
I have 2 tables in a DB with very similar schemas that need to
I have a few huge tables on a production SQL 2005 DB that need
I have a couple of tables in a SQL 2008 server that I need
I have a database two tables and a linking table that I need a
I'd like a query that at its simplest will join 2 tables together with
I have a mysql problem. I have two tables like this that I need

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.