我正在尝试在MYSQL(Amazon RDS)中运行多表更新,但速度非常慢。
我想做什么?
根据1小时的时间范围删除所有重复的行。
下面我创建了一个临时表来识别表中的重复行。此查询在2秒内运行。
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
CREATE TEMPORARY TABLE tmpIds (id int primary key);
INSERT into tmpIds
SELECT distinct
d.id
FROM api d INNER JOIN api orig
on d.domain_id = orig.domain_id and d.user_id = orig.user_id
WHERE
orig.created_at < d.created_at
AND d.created_at <= DATE_ADD(orig.created_at, Interval 1 hour)
AND d.type = 'api/check-end'
AND d.created_at >= '2016-08-01';
SET TRANSACTION ISOLATION LEVEL READ COMMITTED ;
问题是UPDATE查询需要长时间在生产服务器上运行。它还锁定了api表。
SET @TRIGGER_DISABLED = 1;
UPDATE
api
SET
deleted_at = now()
WHERE type = 'api/check-end' AND created_at >= '2016-08-01'
AND id IN (SELECT id FROM tmpIds);
SET @TRIGGER_DISABLED = 0;
我也试过这个版本:
SET @TRIGGER_DISABLED = 1;
UPDATE
api a,
tmpIds ti
SET
a.deleted_at = now()
WHERE
type = 'api/check-end' AND created_at >= '2016-08-01' AND a.domain_id < 10 AND a.id = ti.id;
SET @TRIGGER_DISABLED = 0;
STATS
答案 0 :(得分:0)
问题是以下声明:
SET @TRIGGER_DISABLED = 1;
没有禁用触发器。我不得不删除api表上的UPDATE触发器,UPDATE在1.3秒内运行。
有关在运行查询时禁用触发器的最佳方法的任何帮助吗?