mysql – 如何优化此DB操作?
作者:互联网
我对数据库很草率,无法使用连接,我甚至不确定会更快…
DELETE FROM atable
WHERE btable_id IN (SELECT id
FROM btable
WHERE param > 2)
AND ctable_id IN (SELECT id
FROM ctable
WHERE ( someblob LIKE '%_ID1_%'
OR someblob LIKE '%_ID2_%' ))
Atable包含~19M行,这将删除~3M.目前,我只能使用LIMIT 100000运行查询,我不想整天坐在这里与phpmyadmin,因为每次删除(100.000行)运行约1.5分钟.
有什么方法可以加快/自动化它?
MySQL 5.5
(如果任何表包含20M行,你认为它已经是糟糕的数据库设计吗?)
解决方法:
使用EXISTS或JOIN代替IN来改善性能
使用EXISTS:
DELETE FROM Atable A
WHERE EXISTS (SELECT 1 FROM Btable B WHERE A.Btable_id = B.id AND B.param > 2) AND
EXISTS (SELECT 1 FROM Ctable C WHERE A.Ctable_id = C.id AND (C.someblob LIKE '%_ID1_%' OR C.someblob LIKE '%_ID2_%'))
使用JOIN:
DELETE A
FROM Atable A
INNER JOIN Btable B ON A.Btable_id = B.id AND B.param > 2
INNER JOIN Ctable C WHERE A.Ctable_id = C.id AND (C.someblob LIKE '%_ID1_%' OR C.someblob LIKE '%_ID2_%')
标签:sql,mysql,sql-delete,join,exists 来源: https://codeday.me/bug/20190831/1773697.html