the different between "in" and "exist "
2008-10-31 14:50
495 查看
sql中exist与in的区别
2008年06月13日 星期五 16:38
2008年06月13日 星期五 16:38
in 和 exists也是很好区别的. in 是一个集合运算符. a in {a,c,d,s,d....} 这个运算中,前面是一个元素,后面是一个集合,集合中的元素类型是和前面的元素一样的. 而exists是一个存在判断,如果后面的查询中有结果,则exists为真,否则为假. in 运算用在语句中,它后面带的select 一定是选一个字段,而不是select *. 比如说你要判断某班是否存在一个名为"小明"的学生,你可以用in 运算: "小明" in (select sname from student) 这样(select sname from student) 返回的是一个全班姓名的集合,in用于判断"小明"是否为此集合中的一个数据; 同时,你也可以用exists语句: exists (select * from student where sname="小明") 这两个涵数是差不多的, 但是由于优化方案的不同, 通常NOT EXISTS要比NOT IN 要快, 因为NOT EXISTS可以使用结合算法而NOT IN 就不行了,而EXISTS则不如IN快, 因为这时候IN可能更多的使用结合算法. select * from 表A where exists(select * from 表B where 表B.id=表A.id) 这句相当于 select * from 表A where id in (select id from 表B) 对于表A的每一条数据,都执行select * from 表B where 表B.id=表A.id的存在性判断,如果表B中存在表A当前行相同的id,则exists为真,该行显示,否则不显示 exits适合内小外大的查询,in适合内大外小的查询 IN 确定给定的值是否与子查询或列表中的值相匹配。 EXISTS 指定一个子查询,检测行的存在。 比较使用 EXISTS 和 IN 的查询 这个例子比较了两个语义类似的查询。第一个查询使用 EXISTS 而第二个查询使用 IN。注意两个查询返回相同的信息。 USE pubs GO SELECT DISTINCT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type = 'business') GO -- Or, using the IN clause: USE pubs GO SELECT distinct pub_name FROM publishers WHERE pub_id IN (SELECT pub_id FROM titles WHERE type = 'business') GO 下面是任一查询的结果集: pub_name ---------------------------------------- Algodata Infosystems New Moon Books (2 row(s) affected) exits 相当于存在量词:表示集合存在,也就是集合不为空只作用一个集合.例如 exist P 表示P不空时为真; not exist P表示p为空时 为真 in表示一个标量和一元关系的关系。例如:s in P表示当s与P中的某个值相等时 为真; s not in P 表示s与P中的每一个值都不相等时 为真 [补充]: 对于in 和 exists的区别: 如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists。其实我们区分in和exists主要是造成了驱动顺序的改变(这是性能变化的关键),如果是exists,那么以外层表为驱动表,先被访问,如果是IN,那么先执行子查询,所以我们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了 ,另外IN时不对NULL进行处理。 现在网上对EXIST和IN的效率谁比较高一直争论不休,其实这个问题我们本身也很难判断,毕竟有时候效率这个问题不是我们轻易能看出来的。 |
相关文章推荐
- the different between call and apply in javascript
- The different between #define and const for a constant in C
- The different between Char in C#.Net and in C++.Net(MFC)
- what is the different between function and method in python
- use noscript html tag when user disable the javascript in browser, guide user how to enable the js in different browser and retu
- What is the difference between provider network and self-service network in OpenStack?
- the different between System.exit(0) and System.exit(1)
- The differences between new(delete) and malloc(free) in c/c++
- What are the differences between a pointer variable and a reference variable in C++?
- The Different Definitions between Concurrent processing and Asynchronous processing
- What is the different between Byte and bit
- the different between crash recovery and media recovery
- The difference between null and isEmpty in Groovy
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- the different between C and C++
- What is the difference between a soft reference and a weak reference in Java?
- What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?
- The difference between EXISTS and IN upon Oracle
- What exactly is the difference between “pass by reference” in C and in C++?
- In C++, what’s the difference between an inline function and a macro?