问题
the following executes fast:
delete from text_blocks
where text_id in (
7684,
7683,
7682,
...);
Note, the above list of values is short e.g. 130 retrieved from a separate query. Table text_blocks has ~ 7,000 rows.
The following executes really slow:
delete from text_blocks
where text_id in (select a_text_id from someTable
where name like "%_SomeSuffix");
The subquery in example 2 is the same as used to get the list of 130 in example 1. Neither text_id or a_text_id are indexed.
Any ideas why it is super slow, and or hanging?
回答1:
MySQL is notorious for having very poor performance for queries/subqueries like
select ...from ... where ... in (select ....);
Try using EXISTS
instead of IN
and you should see a dramatic improvement. See
http://dev.mysql.com/doc/refman/5.0/en/subquery-optimization-with-exists.html
http://stackoverflow.com/questions/6135376/mysql-select-where-field-in-subquery-extremely-slow-why
for more info
来源:https://stackoverflow.com/questions/25777052/mysql-where-in-executes-slow-if-sub-query-is-used-where-as-using-list-executes