我有一个删除查询,可以很好地处理少量行。但是现在大约有80K的行由于“超时”而失败了
我的两个桌子是这样的:
TableA
------
| Column | type |
|------------|------------|
| link_id | bigint(40) |
| product_id | int(10) |
| parent_id | int(10) |
Table B
-------
| Column | type |
|-----------|---------|
| id | int(11) |
| parent_id | int(11) |
| child_id | int(11) |
我这样查询以进行删除
DELETE FROM TABLEA
WHERE link_id IN (
SELECT link_id FROM (
SELECT link_id, parent_id, product_id FROM TABLEA
UNION ALL
SELECT id, parent_id, child_id FROM TABLEB
) tbl
GROUP BY parent_id, product_id
HAVING count(*) = 1
ORDER BY parent_id
) ;
但谁不是最优化的人。
目标是从表A中删除表B中不存在的所有记录 为“ parent_id / child_id”夫妇。
在表A中,“ product_id”列为“ child_id”。
谢谢
答案 0 :(得分:0)
解决此问题的一种方法
DELETE A FROM TABLEA A
INNER JOIN
( SELECT link_id FROM
(
SELECT link_id, parent_id, product_id FROM TABLEA
UNION ALL
SELECT id, parent_id, child_id FROM TABLEB
) tbl
GROUP BY parent_id, product_id
HAVING count(*) = 1)B
ON A.link_id=B.link_id
where A.link_id is not null;
答案 1 :(得分:0)
我将使用NOT EXISTS
来获取与tableb没有任何关系的link_id
DELETE FROM TABLEA
WHERE link_id IN (SELECT link_id
FROM TABLEA a
where not exists (select link_id
from tableb b
where b.parent_id=a.parent_id and b.child_id=a.product_id))