Miguel E
Miguel E

Reputation: 1336

Slow query for MySQL using "IN"

For a large table (1 million records) I have an update query like this:

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);

I have several indexes, but one is for the fields:

knowledge_id;phrase_id

This runs very fast under SQLite (2 seconds or less), but for MySQL the same query takes about 37 seconds. What am I doing wrong?

Upvotes: 1

Views: 75

Answers (1)

John Woo
John Woo

Reputation: 263933

try execututing it using 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

Upvotes: 7

Related Questions