MySQL where in executes slow if sub query is used, where as using list executes fast

久未见 提交于 2019-12-11 20:42:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!