关于SQL优化中的In和Exists SQL语句:
select b.text from basetable b , extable e
where b.id=e.id select b.text from basetable b where b.id in (select e.id from extable e) 很多人都说使用in比较快,这是事实,因为笛卡尔积比较小 有人说使用exist更快:
select b.text from basetable b where exists (select 'x' from extable e where b.id=e.id) 经过测试(百万数据),使用exist的确快
但是,如果将b.text修改为count(b.text),也就是统计条数的话,就没有什么优势了 我想问两个问题
1、为什么exists会比in快 2、为什么使用count来统计的时候会没有优势 谢谢
select b.text from basetable b , extable e
where b.id=e.id select b.text from basetable b where b.id in (select e.id from extable e) 很多人都说使用in比较快,这是事实,因为笛卡尔积比较小 有人说使用exist更快:
select b.text from basetable b where exists (select 'x' from extable e where b.id=e.id) 经过测试(百万数据),使用exist的确快
但是,如果将b.text修改为count(b.text),也就是统计条数的话,就没有什么优势了 我想问两个问题
1、为什么exists会比in快 2、为什么使用count来统计的时候会没有优势 谢谢
解决方案 »
- tiptop ERP如何连接到sqlserver数据库?
- oracle查询 求百分比
- 数据库中笛卡儿积是表和表之间的乘法,那除法是什么?
- 100分向高手请教一个存储解决方案,我是学生啊...
- 通过like 对当前用户 方案管理下的表查询
- 写了个分析windows下oracletrace文件的工具。类似sql server 的profiler。大家用用吧
- 谁有oracle 存储过程和触发器的入门教程?
- 如何用svrmgrl进去管理???
- 如何查看表中的内容?
- AB两组数据,如何使用一个sql实现将两个字段所有有关联的数据归类到一起
- 通过SSH连接到linux服务器,如何操作oracle数据库
- 问一个基础的问题,到实例的代理连接的连接问题
下面是oracle文档,:)
In certain circumstances, it is better to use IN rather than EXISTS. In general, if the selective predicate is in the subquery, then use IN. If the selective predicate is in the parent query, then use EXISTS.Sometimes, Oracle can rewrite a subquery when used with an IN clause to take advantage of selectivity specified in the subquery. This is most beneficial when the most selective filter appears in the subquery and there are indexes on the join columns. Conversely, using EXISTS is beneficial when the most selective filter is in the parent query. This allows the selective predicates in the parent query to be applied before filtering the rows against the EXISTS criteria.
个人观点,也是以前看到别人回答的