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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:26:29+00:00 2026-05-13T10:26:29+00:00

I have to JOIN to large tables in a MySQL query and it takes

  • 0

I have to JOIN to large tables in a MySQL query and it takes really long – approximately 180 seconds. Are there any tips for optimizing a merge?

My table has 10 fields. I am only using 4 in the query – all strings. Table has about 600,000 rows and the result should have about 50 rows.

The four used rows are: Title, Variables, Location, Date

Here is my query:

SELECT DISTINCT t1.Title, t1.Variables FROM `MyTABLE` t1 JOIN `MyTABLE` t2  
USING (Title,  Variables) 
WHERE (t1.Location, t1.Date) = ('Location1', 'Date1') 
AND (t2.Location, t2.Date) = ('Location2', 'Date2')
  • 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-13T10:26:30+00:00Added an answer on May 13, 2026 at 10:26 am

    Like others pointed out, you need proper indexes. For this particular query, you can benefit from indexes like:

    (Location, Date) or (Date, Location) (for the WHERE clause)
    and
    (Title, Variables) or (Variables, Title) (for the join condition, ON clause)

    It would be helpful to know exactly the size (that is, datatype) of the location, Date, Title, and Variables columns, as a large index is likely to be slower than a small one.

    Finally, just a tip: I would not use fancy comparison constructs like you do. The

    USING (Title,  Variables) 
    

    is probably ok, but I would certainly check if

    (t1.Location, t1.Date) = ('Location1', 'Date1') 
    

    and

    (t2.Location, t2.Forecast_date) = ('Location2', 'Date2')
    

    are behaving like you expect. SO I would definitely run EXPLAIN on it, and compare the output with a “regular” old fashioned comparison, like so:

        t1.Location      = 'Location1'
    AND t1.Date          = 'Date1'
    AND t2.Location      = 'Location2'
    AND t2.Forecast_date = 'Date2'
    

    You may argue that logically, it is the same and it shouldn’t matter – you’d be right. But then again, MySQL’s optimizer isn’t very smart, and there is always a possibility of bugs, especially with features that aren’t used a lot. I think this is such a feature. So i would at least try to EXPLAIN and see if these alternate notations are evaluated the same.

    But what BenoKrapo pointed out, would it not be easier to do something like this:

    SELECT Title, Variables 
    FROM   MyTABLE
    WHERE  Location = 'Location1' AND Date = 'Date1' 
    OR     Location = 'Location2' AND Date = 'Date2'
    GROUP BY Title, Variables
    HAVING COUNT(*) >= 2
    

    EDIT: I changed HAVING COUNT(*) = 2 to HAVING COUNT(*) >= 2. See comments (thanks again, BenoKrapo)

    EDIT: days after posting this answer, I found this post from Mark Callaghan, MySQL Architect for Facebook: http://www.facebook.com/note.php?note_id=243134480932
    Essentially, he describes how similar-but-different ‘smart’ comparisons deliver abysmal performance due to MySQL optimizer bug. So my point is, try to unfancy your syntax when you suffer, you might have hit a bug.

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

Sidebar

Related Questions

I have two tables in my database table1, table2. I have a join query
I have used a join query for retrieving value from two tables one is
I am looking for some help with a MYSQL query. I have two tables,
I have a join in a oracle query which looks like: FROM eiv.table1 eiv.table2
I have been trying to join two tables (USERS AND USERS_ROLES) based on their
I have 4 tables and i want to join them and extarct 4 values.
I have two tables that I want to join together. Table1 Year, ID, Theme,
I have the following query with many LEFT JOIN clauses that has 7 result
I have an SQL query with a nested join: SELECT rh.host, rh.report, COUNT(results.id), COUNT(results_2.id),
I have 2 related MySQL tables in a one to many relationship. Customers: cust_id,

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.