- 浏览: 241870 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
nodonkey:
貌似还是不行,再等等吧,amfphp要出2.0了
amfphp1.9与php5.3.X版本不兼容 -
live711:
请问amfphp与php5.3.X搭配能用了吗?
amfphp1.9与php5.3.X版本不兼容 -
zhousheng193:
非常感谢!
安装flash player debug版本遇到的一些问题 -
sp42:
谢谢提示,我遇到的也是不能加密,用MAC地址代替之。
DI-624+A路由器韧体升级解决经常掉线的问题(转) -
心似海:
不错,要挖去了,哈哈
深入sql之merge into
Functionally, they are the same (as compared to NOT IN vs NOT EXISTS which are functionally different in one scenario - read this post for the differences between NOT IN and NOT EXISTS clauses : http://decipherinfosys.wordpress.com/2007/01/21/32/ ). However, there are performance implications of using one over the other that one needs to be aware of. Assume that we have two tables : TABLE_A and TABLE_B and the match is being done on TABLE_A.col1 = TABLE_B.col2. In that scenario, an in statement like:
select <select column list> from TABLE_A where col1 in (Select col2 from TABLE_B)
will get processes in this way:
1) The sub-query gets evaluated first and the results are distinct’ed and indexed,
2) The output from it is then joined with TABLE_A.
Re-writing the above query using the EXISTS clause will give:
Select <select column list> from TABLE_A
where exists (select 1 from Table_B where Table_B.col2 = Table_A.col1)
This gets evaluated in this order:
1) For every value of Table_A.col1, loop through and match the values in Table_B.col2.
2) If we get a match, select that value and move on to the next one. If there is no match, discard that value.
So, where should one use an IN vs the EXISTS clause? If the result of the sub-query “Select col2 from TABLE_B” is huge and the TABLE_A is a relatively small set and executing “select 1 from Table_B where Table_B.col2 = Table_A.col1″ is very fast because of proper index on Table_B.col2, then an exists clause will be better since the optimizer can do a FTS on Table_A and then use the index to do the probe/seek operations for Table_B.
If the result of the sub-query is small, then the IN clause is much faster. If the results of the both the sub-query as well as the outer query is large, then either IN or EXISTS would work the same - it depends upon your indexing scheme.
Please do note that the example used above is a very simplistic one in order to illustrate the point - in real world, you would have queries that have additional filter criteria on those tables that narrows down the result sets. As a generic rule, if the result of the outer query is small and the result set of the inner sub-query is large, then use EXISTS - if it is the other way around, then use the IN clause.
Tips:
SQL: Where column > (subquery)
1.This is known as a correlated subquery because the subquery references the outer query in the subqueries WHERE clause. Internally, correlated subqueries are very expensive to process because the inner query must be executed for every row returned by the outer query.
Where exists (subquery)
2.The WHERE EXISTS subquery is used when we want to display all rows where we have a matching column in both tables. In most cases, this type of subquery can be re-written with a standard join to improve performance.
Where not exists (subquery)
3.As a general rule, the use of the NOT EXISTS subqueries are discouraged because the query can often be re-written as a standard join with much faster performance.
Where column not in (subquery)
4.There are serious problems with subqueries that may return NULL values. It is a good idea to discourage the use of the NOT IN clause (which invokes a subquery) and to prefer NOT EXISTS (which invokes a correlated subquery), since the query returns no rows if any rows returned by the subquery contain null values.
not exists不存在,也就是说后面的括号中只要返回了数据那么这个条件就不存在了,可以理解为括号前的not
exists是一个左表达式 ,括号后的查询是一个右表达式,只有当右表达式返回的也是not exists(即后面的查询出来的结果是非空的)时,等式才成立。
IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。
发表评论
-
alter table move 与shrink space的区别
2012-03-06 13:51 2177转自:http://hi.baidu.co ... -
mysqlsla来分析MYSQL的性能及索引
2011-01-17 19:56 1263— Slow log: mysqlsla -lt slow ... -
六款常用mysql slow log分析工具的比较
2011-01-17 19:06 1236转自:http://www.iteye.com/topi ... -
MySQL的大小写敏感性
2011-01-12 14:05 1017转自: http://www.zeali.net/ent ... -
如何查看mysql的版本
2010-05-22 11:52 21977如果我们想要查看mysql数据库的版本有以下四种方法: ... -
MySQL 数据库的备份和恢复
2010-03-19 13:44 891转自 忧里修斯 http://tec ... -
mysql使用show命令以及replace函数批量修改数据
2010-03-19 13:37 2363一.mysql的show命令 a. show tables或 ... -
MySQL中的ROWNUM的实现
2010-01-26 13:43 2157本文转自 http://blog.csdn.net/ACMA ... -
一个MySQL死锁问题的分析及解决
2010-01-20 12:50 1222转自http://java-guru.iteye.com/bl ... -
MySQL 死锁分析
2010-01-20 12:42 32931.MySQL锁和死锁的理解: ... -
sql 按指定顺序排序
2010-01-19 10:53 21261、在ORACLE中使用Decode Decode实 ... -
MYSQL 事务管理
2009-10-26 19:48 1024mysql_query("BEGIN"); ... -
delete 符合条件的记录中的前几条或者重复记录
2009-09-03 20:04 2019今天写代码,遇到了这个问题,只能删除符合条件的记录中的某几条. ... -
sql update delete 中 使用 inner join
2009-08-24 11:38 7144SQL中使用update inner join和delet ... -
What is the difference between VARCHAR, VARCHAR2 ?
2009-06-01 09:43 899Both CHAR and VARCHAR2 types ar ... -
Oracle index
2009-05-15 10:50 0索引是一种可以提高查 ... -
Views and Materialized Views 整理
2009-04-10 14:29 1106Views and Mat ... -
ORACLE 之 TRUNCATE TABLE
2009-03-30 16:49 1948TRUNCATE Caution: Y ... -
深入sql之merge into
2009-01-08 16:38 4696转自 逆水流沙 http://hi.baidu.com/wen ... -
Oracle日期函数操作(收集整理版)
2008-12-04 16:50 2749经常在平时的开发中要用到oracle的日期函数,每次都要上 ...
相关推荐
关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in...
里面自己根据网上的资源整理出来的一份sql中in,exists,not in,not exists的使用方法以及注意事项等,有助于初学的朋友们借鉴。
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!
NULL 博文链接:https://576017120.iteye.com/blog/1624774
exists 和 not exists的详细解释
sql语句优化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的语句
MySQL优化之in,exists,not in,not exists的区别in与existsin查询过程结论:exists查询过程:结论:not in与not existsnot in查询过程:结论:not exists查询过程:结论: 首先我们使用两个用户表作为实例 insert ...
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!
一个是问in exist的区别,一个是not in和not exists的区别
NOT EXISTS ⇔ NOT IN ⇒ NOT NULL 问题 结果不一样 疑问 如果等效
总结:exists 和not exists语句强调是否返回结果集,不要求知道返回什么,与in的区别就是,in只能返回一个字段值,exists允许返回多个字段 提醒:文章中提供了exists和not exists的常用示例,已经经过本人测试,...
1. EXISTS的执行流程 select * from t1 where exists ( select null from t2 where y = x ) 可以理解为: 代码如下: for x in ( select * from t1 ) loop if ( exists ( select null from t2 where y = x.x ) then ...
非上传者作品。本人只是学习的时候百度到的比较的文档,拿出来和大家分享一下,希望对大家有所帮助!
NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not in (select a from B) 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select ...
基本数据插入 except和intersect和exists和not exists和union和union all sql server
使用EXISTS(或NOT EXISTS)关键字引入一个子查询时,子查询实际上不产生任何数据;它只返回TRUE或FALS值。 指定一个子查询,检测行的存在。 如果子查询包含行,则返回 TRUE IN效率要差(返回子查询)
系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not
golang 错误提示 $GOPATH/go.mod exists but should not goland 出现该问题 去掉vendor即可 初出茅庐有错必改 欢迎交流