您好,欢迎光临! 推荐您使用Chrome浏览器访问本站。

mysql嵌套查询in的优化,where执行顺序

举个例子:
select id from temp where id in (select id from temp1 where id>1000) 
这个查询在数据量大时是相当慢的,因为mysql把in里的select当成相关的语句从外到内执行查询,而不是从内到外。

我们可通过临时表来让mysql先执行里面的查询,从而避免巨大的性能开销。
select id from temp where id in (select id from (select id from temp1 where id>1000) as temp3) 

其实我们在生产环境中因尽量少使用嵌套查询,尤其是WHERE...IN() 和 WHERE...NOT IN语句。

注:
mysql where是从左向右来执行的,筛选越多的条件应往前方。
oracel where子句--执行顺序为自下而上、从右到左。

 


 

 

您可能也喜欢