我有两个MySQL表,一个名为" station"和另一个名为"记录。" "站"与"记录"中列出的记录有关。通过 stations.id = records.stationID 。
我的许多电台"与"记录"中出现的任何数据无关。表,我想删除" station"其对应的id与records.stationID中的任何值都不匹配。
我试过了:
DELETE FROM stations
WHERE stations.id NOT IN (SELECT DISTINCT records.stationID
FROM records);
原则上有效,但我让它在一夜之间运行了8个小时,而且早上还没有完成。我认为这是因为它需要为"站中的每个记录执行子查询,"其中包含9000多行。 (这是正确的吗?)
如何加快删除过程?我也尝试了各种内连接,即:
DELETE FROM stations
INNER JOIN records on stations.id=records.stationID
WHERE stations.id not in records.stationID;
这给了我一个语法错误。我也尝试了下面的内容,它似乎没有做任何事情,真的:
DELETE stations.*
FROM stations
INNER JOIN records ON records.stationID=stations.id
WHERE stations.id NOT IN (SELECT DISTINCT records.stationID
FROM records);
和
DELETE FROM stations
INNER JOIN records on records.stationID=stations.id
WHERE stations.id NOT IN (SELECT DISTINCT records.stationID from records);
有什么想法吗?
已解决:我只需要在"记录"中添加一个索引。表。感谢大家的帮助
答案 0 :(得分:0)
尝试使用它:
DELETE stations FROM stations
LEFT JOIN records on stations.id=records.stationID
WHERE records.stationID IS NULL;
另外,请检查是否为records.stationID
设置了索引。
答案 1 :(得分:-1)
尝试 LEFT OUTER JOIN:
SELECT * FROM stations
LEFT OUTER JOIN records
ON stations.stationID= records.stationID
WHERE records.stationID IS null