对于一个大表(100万条记录),我有一个像这样的更新查询:
update direct_words set weight = (weight / 4) where knowledge_id = :a
and phrase_id in (select phrase_id from phrases where knowledge_id =:b
and phrase_class <> 6);
我有几个索引,但有一个用于字段:
knowledge_id;phrase_id
在SQLite下运行速度非常快(2秒或更短),但对于MySQL,同样的查询大约需要37秒。我做错了什么?
答案 0 :(得分:7)
尝试使用JOIN
update direct_words a
INNER JOIN phrases b
ON a.phrase_id = b.phrase_id
set a.weight = (a.weight / 4)
where a.knowledge_id = :a AND
b.knowledge_id = :b AND
b.phrase_class <> 6