Advice on creating SQL indexes needed

Cbpro Ads :

This SQL query takes more than 13 secondss to execute as there are 40K records in master and 5000 rows in the second table.

Can any one tell the indexes to be created? I have created expiry-date and email as a combination index but does not work.

SELECT email,first_name,last_name,userid,PASSWORD,cons FROM MASTER 
WHERE expirydate <='2020-02-27' 
AND email NOT IN (SELECT (email) FROM bounce_emails_amazon)



EXPLAIN
1   PRIMARY MASTER  \N  ALL expdate_email,expemail.userid   \N  \N  \N  38228   60.75   Using where

2   SUBQUERY    bounce_emails_amazon    \N  index   email   email   103 \N  3567    100.00  Using index



Bill Karwin :

I would suggest:

ALTER TABLE `master` ADD INDEX (expirydate);
ALTER TABLE `bounce_emails_amazon` ADD INDEX (email);

Then use the exclusion join query that @Alex suggested (I'll include it here):

SELECT 
  m.email,
  m.first_name,
  m.last_name,
  m.userid,
  m.PASSWORD,
  m.cons 
FROM MASTER m
LEFT JOIN bounce_emails_amazon b
ON b.email = m.email
WHERE m.expirydate <='2020-02-27' 
AND b.email IS NULL

The reason this is better is that the original query you showed using the subquery may scan the whole bounce_emails_amazon for every row of the outer query. By using a join, and adding the index to the email column, it will only look up the matching row.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8057&siteId=1