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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:10:54+00:00 2026-05-23T02:10:54+00:00

Today noticed one SQL query that was extremely long in my mysql-slow.log I would

  • 0

Today noticed one SQL query that was extremely long in my mysql-slow.log

I would like to ask some SQL experts how to correctly format and perform this SQL.

Idea behind sql:
Return all emails that are not in mailchimp table while doing it from 2 tables and return only DISTINCT values (users and subscribers emails might duplicate). Also including city and language with results.

As you can see query_time is monster long and rows examined are just wtf combined 2 tables there should be only around 20k rows.

Query_time: 113.216544  Lock_time: 0.000180 Rows_sent: 43  Rows_examined: 208280841

SELECT * FROM 
    ( SELECT u.email AS email, u.city, u.language FROM users AS u 
        LEFT JOIN mailchimp AS m ON u.email = m.email WHERE m.email IS NULL GROUP BY u.email 
        UNION SELECT s.email AS email, s.city, s.language FROM subscribers AS s 
        LEFT JOIN mailchimp AS m ON s.email = m.email WHERE m.email IS NULL GROUP BY s.email ) 
    AS sync GROUP BY sync.email ORDER BY sync.email ASC;

EXPLAIN for query

+----+--------------+------------+------+---------------+------+---------+------+-------+---------------------------------+
| id | select_type  | table      | type | possible_keys | key  | key_len | ref  | rows  | Extra                           |
+----+--------------+------------+------+---------------+------+---------+------+-------+---------------------------------+
|  1 | PRIMARY      | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |    23 | Using temporary; Using filesort |
|  2 | DERIVED      | u          | ALL  | NULL          | NULL | NULL    | NULL | 10482 | Using temporary; Using filesort |
|  2 | DERIVED      | m          | ALL  | NULL          | NULL | NULL    | NULL | 11411 | Using where; Not exists         |
|  3 | UNION        | s          | ALL  | NULL          | NULL | NULL    | NULL |  2709 | Using temporary; Using filesort |
|  3 | UNION        | m          | ALL  | NULL          | NULL | NULL    | NULL | 11411 | Using where; Not exists         |
| NULL | UNION RESULT | <union2,3> | ALL  | NULL          | NULL | NULL    | NULL |  NULL |                                 |
+----+--------------+------------+------+---------------+------+---------+------+-------+---------------------------------+
6 rows in set (2 min 1.65 sec)
  • 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-23T02:10:55+00:00Added an answer on May 23, 2026 at 2:10 am

    I guess you have no indexes on the three tables. Add index on field email, on all 3 tables; users, subscribers and mailchimp and run the query – and the EXPLAIN – again.

    Your query:

    SELECT *
    FROM 
      ( SELECT u.email AS email, u.city, u.language
        FROM users AS u 
          LEFT JOIN mailchimp AS m
            ON u.email = m.email
          WHERE m.email IS NULL
          GROUP BY u.email 
      UNION
        SELECT s.email AS email, s.city, s.language
        FROM subscribers AS s 
        LEFT JOIN mailchimp AS m
          ON s.email = m.email
        WHERE m.email IS NULL
        GROUP BY s.email
      ) 
      AS sync
    GROUP BY sync.email
    ORDER BY sync.email ASC;
    

    could be written like this (removing the two inner GROUP BY and turning UNION into UNION ALL ):

    SELECT *
    FROM 
      ( SELECT u.email AS email, u.city, u.language
        FROM users AS u 
          LEFT JOIN mailchimp AS m
            ON u.email = m.email
          WHERE m.email IS NULL
      UNION ALL
        SELECT s.email AS email, s.city, s.language
        FROM subscribers AS s 
        LEFT JOIN mailchimp AS m
          ON s.email = m.email
        WHERE m.email IS NULL
      ) 
      AS sync
    GROUP BY sync.email
    ORDER BY sync.email ASC;
    

    or like this (turning the LEFT JOIN - check IS NULL into NOT EXISTS), which is sometimes faster:

    SELECT *
    FROM 
      ( SELECT u.email AS email, u.city, u.language
        FROM users AS u 
        WHERE NOT EXISTS
          ( SELECT * 
            FROM mailchimp AS m
            WHERE u.email = m.email
          )
      UNION ALL
        SELECT s.email AS email, s.city, s.language
        FROM subscribers AS s 
        WHERE NOT EXISTS
          ( SELECT * 
            FROM mailchimp AS m
            WHERE s.email = m.email
          )
      ) 
      AS sync
    GROUP BY sync.email
    ORDER BY sync.email ASC;
    

    In any case, add indexes to the email fields!

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

Sidebar

Related Questions

Today I installed Visual Studio 2010 Ultimate - RTM. One item that I noticed
I noticed some code of a colleague today that initialized class variables in the
Today I began toying with WCF. One thing I noticed is that, by default,
Today I noticed that a few Google advertisements in an adsence block on one
Today I noticed that dead.letter file is created in my root directory on one
I noticed today that in D2009 (with update 1), TStringStream.ReadString does not move the
I noticed today (after ~8 years of happily hacking away at bash) that there
I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The
I've been using Javascript's Date for a project, but noticed today that my code
Being new to Linq-to-SQL, I ran into one of it's Gotchas today and I

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.