我有一个大型数据库,在5个表中包含大约4千万行。我需要删除一个表中的行,其中列具有第二个表中不存在的值。
我尝试编写查询来删除某些行,并将我不想删除的行插入到另一个表中:
DELETE FROM user_followersid WHERE followersIds_generatedId IN (SELECT * FROM generatedfollowers)
CREATE TABLE tempfollowersids as SELECT * FROM user_followersid WHERE followersIds_generatedId IN (SELECT generatedId from followersid WHERE followerId IN (SELECT * FROM topusers));
删除需要花费大量时间,并且每次最后都会失败并报告以下错误:
ERROR 1206 (HY000): The total number of locks exceeds the lock table size
我甚至尝试使用LIMIT
一次只删除几行,但这也花费了很多时间。我知道IN
非常慢。但我找不到任何替代方式来编写我的查询。
关于如何删除行的任何想法?
答案 0 :(得分:4)
你说:
哪一列的值不存在于第二个表
中
所以,你的查询不应该是:
DELETE FROM user_followersid
WHERE followersIds_generatedId NOT IN
(SELECT DISTINCT id FROM generatedfollowers)
答案 1 :(得分:0)
您的IN
声明:
DELETE FROM user_followersid
WHERE followersIds_generatedId IN
( SELECT generatedId
FROM generatedfollowers
)
也可以用EXISTS
编写,如:
DELETE FROM user_followersid AS uf
WHERE EXISTS
( SELECT *
FROM generatedfollowers AS gf
WHERE gf.generatedId = uf.followersIds_generatedId
)
和JOIN
,如:
DELETE uf
FROM user_followersid AS uf
JOIN generatedfollowers AS gf
ON gf.generatedId = uf.followersIds_generatedId
您不能将LIMIT x
与第三个(加入)声明一起使用。