3

I'm a beginner in SQL and I have some troubles with this request, which just can't be executed because it's too inefficient. Here it is, it just loads for hours (!)

SELECT my_db_client_total.email as EMAIL_TOTAL, 
    my_db_client_total.name, my_db_client_total.country 
    FROM my_db_client_total, my_db_client_pro
      WHERE (my_db_client_total.email<>'' ) 
    AND (my_db_client_total.country = '120') 
    AND (my_db_client_total.email IN (SELECT my_db_client_pro.email 
    FROM my_db_client_pro));

My tables are organised in this way :

Table 1 :        Table 2 :
   email         email
   name          name
   country       country

The purpose of the request it to test my database, and to process a further request where I want to select email, name and country from Table 1 where table1.email is not in table2.

Sorry for my bad English, and for my level, as I said earlier, I'm a beginner and self-taught.

If anyone can help me or at least enlighten me, I'll be so thankful for your time.

EDIT: I didn't precisely know but I'm working with a table where there is 20 000+ rows.

8
  • up vote for a well presented question appreciation Nov 27, 2015 at 15:09
  • Do you have any indexes on my_db_client_total? If not then you should create one on country at least and possibly (country, email, name). Nov 27, 2015 at 15:17
  • @Strawberry I can't see the typo?
    – Martin
    Nov 27, 2015 at 15:29
  • Maxime, you really should also look into adding indexes on to your SQL table structure, so that you can index columns as that will make your queries run much faster with minimum actual code changes. Read dev.mysql.com/doc/en/mysql-indexes.html
    – Martin
    Nov 27, 2015 at 15:30
  • hah, I went away and then came back to the page and immediately saw it before reading your comment. @Strawberry
    – Martin
    Nov 27, 2015 at 15:32

5 Answers 5

1

This is your query (fixed up a bit with table aliases):

SELECT ct.email as EMAIL_TOTAL, ct.name, ct.country
FROM my_db_client_total ct 
WHERE (ct.email <> '' ) AND
      (ct.country = 120) AND 
      (ct.email IN (SELECT my_db_client_pro.email FROM my_db_client_pro));

(I am assuming that 120 is really an integer.)

In older versions of MySQL, exists is more efficient than in, so rewrite the query as:

SELECT ct.email as EMAIL_TOTAL, ct.name, ct.country
FROM my_db_client_total ct 
WHERE (ct.email <> '' ) AND
      (ct.country = 120) AND 
      EXISTS (SELECT 1 FROM my_db_client_pro cp WHERE cp.email = ct.email);

Next, consider indexes. For this query, good indexes are:

create index idx_my_db_client_total_2 on my_db_client_total(country, email, name);

create index idx_my_db_client_pro_email on my_db_client_pro(email);
2
  • Getting the error : create index idx_my_db_total_2 on my_db_total(country, email, name); MySQL said: Documentation #1170 - BLOB/TEXT column 'country' used in key specification without a key length My field are tinytext... Nov 27, 2015 at 16:11
  • @MaximeRogissart . . . BLOB/TEXT don't really seem appropriate for any of those columns (varchar() seems quite sufficient). However, you can try the index without the offending columns or use a key length. Nov 28, 2015 at 2:31
1

Your question states you are using MySQL, so this will give you an inner (record must exist in both tables) join.

SELECT my_db_client_total.email as EMAIL_TOTAL, my_db_client_total.name, my_db_client_total.country 
FROM my_db_client_total inner join my_db_client_pro on my_db_client_pro.email = my_db_client_total.email
WHERE (my_db_client_total.email<>'' ) AND (my_db_client_total.country = '120');

To select a row that does not exist in the other table do this:

SELECT my_db_client_total.email as EMAIL_TOTAL, my_db_client_total.name, my_db_client_total.country 
FROM my_db_client_total LEFT OUTER JOIN my_db_client_pro on my_db_client_pro.email = my_db_client_total.email
WHERE (my_db_client_total.email<>'' ) AND (my_db_client_total.country = '120') AND my_db_client_pro.email IS NULL;
2
  • THank you very much ! How can i transform this for my final purpose, wich is the exact opposite of the request ? Can i just change join my_db_client_pro on my_db_client_pro.email = my_db_client_total.email to join my_db_client_pro on my_db_client_pro.email != my_db_client_total.email Nov 27, 2015 at 15:46
  • @MaximeRogissart, no that wouldnt work - I updated the answer Nov 27, 2015 at 15:57
0

You should be using a Join.

Check them out here: https://technet.microsoft.com/en-us/library/ms191472(v=sql.105).aspx

Example code:

SELECT my_db_client_total.email as EMAIL_TOTAL, my_db_client_total.name, my_db_client_total.country
FROM my_db_client_total AS ct
    CROSS JOIN my_db_client_pro AS cp
    ON ct.CLIENT_ID = cp.CLIENT_ID
0
SELECT my_db_client_total.email as EMAIL_TOTAL, 
  my_db_client_total.name, my_db_client_total.country 
FROM 
  my_db_client_total, 
WHERE (my_db_client_total.email <> '') 
AND (my_db_client_total.country = '120') 
AND (my_db_client_total.email not IN (SELECT my_db_client_pro.email
  FROM my_db_client_pro));

Basically get all the data from table and on where condition getting the email address not in table 2

0

If you join the 2 tables, why don't you use the right side table in your query instead of making a subquery on it?

Try this (only the join) :

SELECT my_db_client_total.email as EMAIL_TOTAL, my_db_client_total.name, my_db_client_total.country 
  FROM my_db_client_total, my_db_client_pro
 WHERE my_db_client_total.email <> ''
   AND my_db_client_total.country = '120'
   AND my_db_client_total.email = my_db_client_pro.email;

or this (only the subquery) :

SELECT my_db_client_total.email as EMAIL_TOTAL, my_db_client_total.name, my_db_client_total.country 
  FROM my_db_client_total
 WHERE my_db_client_total.email <> ''
   AND my_db_client_total.country = '120' 
   AND my_db_client_total.email IN (SELECT my_db_client_pro.email FROM my_db_client_pro);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.