MYSQL WHERE-IN Subquery Runs Forever

折月煮酒 提交于 2019-12-05 05:20:51
Martin Smith

There is an issue in MySQL and in where even uncorrelated subqueries are treated as though they were correlated and re-evaluated for each row.

In the explain plan the select type will likely be showing as dependant subquery rather than just subquery as would be desired.

I suggest trying the approach described at the end of this article of using a derived table to materialize the inner result set.

Or alternatively you could look at the constify procedure here to see if it will assist you in getting around this issue.

Using a JOIN risks duplicating results - an EXISTS will work similar to an IN, without the duplication risk:

SELECT x.* 
  FROM widgets x
 WHERE EXISTS (SELECT NULL
                 FROM WIDGETS y
                WHERE y.name = 'doodah'
                  AND y.type_id = x.type_id)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!