我有三个表,第一个是电子邮件地址列表:
addresses:
id - integer, this is the primary key<br>
email - varchar(255) field holding the address
sent:
sid - integer, foreign key references id in addresses table
received:
rid - integer, foreign key references id in addresses table
显然“已发送”和“已接收”表还有其他列,但它们对于这个问题并不重要。每次发送或接收电子邮件时都会填充已发送和已接收表,如果地址不在“地址”表中,则会添加该地址。表格可以变得非常大(100,000+)。
“sent”和“received”表的条目会定期清除,并且出于各种原因删除条目,在“addresses”表中留下孤立的条目。
我正在寻找 MySQL 中最有效的方法来清除“地址”表中的孤立条目。到目前为止我的查询是:
delete
from addresses
where id not in
(select rid from received)
and id not in
(select sid from sent);
这行得通,但它可能需要很长时间才能运行,而且绝对不是最有效的方法!我也试过这个:
delete
from addresses
where not exists
(select 'x' from sent where sent.sid=addresses.id)
and not exists
(select 'x' from rceieved where recieved.rid=addresses.id);
这有点快,但仍然需要很长时间,我怀疑我需要使用 JOIN 语法,但此时我的 sql 知识已经用完了!