我有三個表查找缺失值,第一個是電子郵件地址列表:MySQL的最有效的方法來從多個表
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+)。
定期清除「已發送」和「已接收」表的條目,並因各種原因刪除條目,在「地址」表中留下孤立條目。
我正在尋找最有效的方法在MySQL中清除「地址」表中的孤立條目。查詢我至今是:
delete
from addresses
where id not in
(select rid from received)
and id not in
(select sid from sent);
這工作,但它可以採取looong時間運行,絕對不是這樣做的最有效的方法!我也試過這樣:
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知識已經用完了我在這一點!
表中有哪些索引可用?它可能是沒有被使用。 – 2012-07-12 09:57:40
爲3個表的查詢和'SHOW CREATE TABLE'輸出提供'EXPLAIN'。 – 2012-07-12 10:03:34
嘗試我的查詢一次,我已經刪除了在條款和加入QUERIES – 2012-07-12 10:22:04