Mysql Select Distinct w TRIM() 和 Subselect 优化性能

Mysql Select Distinct w TRIM() and Subselect Optimize Performance

我有一个 table mailing_list

id, optout, email

电子邮件有重复,但即使电子邮件是 相同的。所以我们可以得到这样的数据...

id optout email

1   0     test1@email.com
2   1     test1@email.com
3   0     test1@email.com
4   0     test2@email.com
5   1     test3@email.com
6   0     test4@email.com
7   1     test4@email.com
8   0     test4@email.com

我试图达到的最终结果是两个列表,optin 和 optout

到目前为止,我发现执行此操作的最佳方法是使用以下查询, 但是性能很慢,所以试图找到更好的方法。 TRIM 功能需要在那里,因为一些电子邮件有空格 有些人在选择不同的时候不会这样,如果我 不要使用 TRIM.

(选择)

select distinct TRIM(email) as emails from mailing_list where optout = 0
and email not in (select email from mailing_list where optout = 1) order by emails

(退出)

select distinct TRIM(email) as emails from mailing_list where optout = 1
and email not in (select email from mailing_list where optout = 0) order by emails

如有任何帮助,我们将不胜感激。

谢谢

对于那些选择退出的人:

select trim(email) as trimmed_email, sum(optout) as qty
from mailing_list
group by trimmed_email
having qty > 0;

对于那些选择加入的人:

select trim(email) as trimmed_email, sum(optout) as qty
from mailing_list
group by trimmed_email
having qty = 0;