首页 > 分享 > SQL查询性能:IN与EXISTS用法比较

SQL查询性能:IN与EXISTS用法比较

最新推荐文章于 2025-01-10 23:36:57 发布

tanshi 于 2011-12-16 16:52:24 发布

 

Well, the two are processed very very differently. Select * from T1 where x in ( select y from T2 ) is typically processed as: select * from t1, ( select distinct y from t2 ) t2 where t1.x = t2.y; The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then joined to the original table -- typically. As opposed to select * from t1 where exists ( select null from t2 where y = x ) That is processed more like: for x in ( select * from t1 ) loop if ( exists ( select null from t2 where y = x.x ) then OUTPUT THE RECORD end if end loop It always results in a full scan of T1 whereas the first query can make use of an index on T1(x). So, when is where exists appropriate and in appropriate? Lets say the result of the subquery ( select y from T2 ) is "huge" and takes a long time. But the table T1 is relatively small and executing ( select null from t2 where y = x.x ) is very very fast (nice index on t2(y)). Then the exists will be faster as the time to full scan T1 and do the index probe into T2 could be less then the time to simply full scan T2 to build the subquery we need to distinct on. Lets say the result of the subquery is small -- then IN is typicaly more appropriate. If both the subquery and the outer table are huge -- either might work as well as the other -- depends on the indexes and other factors.

====================================================

http://asktom.oracle.com/pls/asktom/f?p=100:11:3653284370942596::::P11_QUESTION_ID:953229842074

相关知识

SQL查询与索引优化
Hive :sql语法详解
《sql 语言艺术》 概要
【转】SQL语言艺术
使用 SQL 查询编辑器进行查询
SQL语言艺术
全文搜索 (SQL Server)
深入研究 SQL多表关联查询的艺术 从两张到四张表的逻辑构建与专业实践
NodeJS中使用SQLite3
数据库与SQL语言

网址: SQL查询性能:IN与EXISTS用法比较 https://m.huajiangbk.com/newsview1610620.html

所属分类:花卉
上一篇: 双拥公园不同生境访花昆虫多样性及
下一篇: 农残检测南京微测农药残留快速检测