我在从select语句更新表时遇到问题...这是命令:
UPDATE CrashReport SET STATUS = '2' WHERE id IN (
SELECT report_id
FROM CrashInfo
WHERE info_title LIKE 'STACK_TRACE_ID'
AND info_value IN (
SELECT info_value
FROM CrashInfo
WHERE report_id = **report_id**
AND info_title = 'STACK_TRACE_ID'
)
)
我传递给此查询的参数将是子子查询中的report_id ... select需要 0.113 秒才能单独执行,但更新查询需要总计 4.868 秒执行。是因为更新查询将在表的每一行上执行select语句吗?我怎样才能让它更快?
谢谢
答案 0 :(得分:1)
这样的事情:
SET @myVal = (
SELECT info_value
FROM CrashInfo
WHERE report_id = **report_id**
AND info_title = 'STACK_TRACE_ID');
UPDATE CrashReport SET STATUS = '2' WHERE id IN (
SELECT report_id
FROM CrashInfo
WHERE info_title LIKE 'STACK_TRACE_ID'
AND info_value IN (@myVal)
)
答案 1 :(得分:0)
IN
有时会很慢。您可以使用join
s(即使在update
中)重新构建查询:
UPDATE CrashReport cr join
(SELECT report_id
FROM CrashInfo ci join
info_value iv
on report_id = **report_id** AND
info_title = 'STACK_TRACE_ID'
) cir
on cr.id = cir.id
SET cr.STATUS = '2';
正如我所看到的,看起来CrashInfo
和info_value
之间的连接条件缺失。这两个表中是否应该有一些相等的字段?这可以解释表现不佳。