`
tsinglongwu
  • 浏览: 229304 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

oracle常识(一):in和exist的区别

阅读更多


in 与 exist 的语法比较:           

 

select × from 数据表 t where t.x in (...) 

     括号内可以是符合t.x字段类型的值集合,如('1','2','3'),但如果t.x是number类型的时候,似乎这样的写法会出问题;也可以是通过另外的select语句查询出来的值集合,如(select y from 数据表2 where ...)。 
     

select * from 数据表 t where [...] and exist (...) 

      方括号内为其它的查询条件,可以没有。exist后面的括号内可以是任意的条件,这个条件可以与外面的查询没有任何关系,也可以与外面的条件结合。如:(select * from 数据表2 where 1=1) 或 (select * from 数据表2 where y=t.x)

例子:

in的SQL语句

SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime 
FROM tab_oa_pub WHERE is_check=1 and 
category_id in (select id from tab_oa_pub_cate where no='1') 
order by begintime desc 

 
exists的SQL语句

SELECT id, category_id, htmlfile, title, convert(varchar(20),begintime,112) as pubtime 
FROM tab_oa_pub WHERE is_check=1 and 
exists (select id from tab_oa_pub_cate where tab_oa_pub.category_id=convert(int,no) and no='1') 
order by begintime desc 

 
效率比较:

        先讨论IN和EXISTS。     

select * from t1 where x in ( select y from t2 ) 

 

        事实上可以理解为:     

select * from t1, ( select distinct y from t2 ) t2 where t1.x = t2.y 

    

      如果你有一定的SQL优化经验,从这句很自然的可以想到t2绝对不能是个大表,因为需要对t2进行全表的“唯一排序”,如果t2很大这个排序的性能是不可忍受的。但是t1可以很大,为什么呢?最通俗的理解就是因为t1.x=t2.y可以走索引。但这并不是一个很好的解释。试想,如果t1.x和t2.y都有索引,我们知道索引是种有序的结构,因此t1和t2之间最佳的方案是走merge join。另外,如果t2.y上有索引,对t2的排序性能也有很大提高。 
            

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 
                   OUTPUT THE RECORD! 
                end if 
            end loop 

        

     这个更容易理解,t1永远是个表扫描!因此t1绝对不能是个大表,而t2可以很大,因为y=x.x可以走t2.y的索引。

        综合以上对IN/EXISTS的讨论,我们可以得出一个基本通用的结论:IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况

原文:http://blog.csdn.net/jwisdom/archive/2007/09/27/1803577.aspx

 

分享到:
评论

相关推荐

    oracle中exists_和in的效率问题详解

    oracle中exists_和in的效率问题详解

    简述Oracle中in和exists的不同

    且看接下来的具体分析:in其实是将外表和内表进行hash join,exists是先对外表进行loop操作,然后每次loop后再对内表进行查询。 如果两张表大小差不多,那么exists和in的效率差不多。 例如: 一张大表为A,一张小表B...

    Oracle In和exists not in和not exists的比较分析

    in和exist的区别 从sql编程角度来说,in直观,exists不直观多一个select, in可以用于各种子查询,而exists好像只用于关联子查询 从性能上来看 exists是用loop的方式,循环的次数影响大,外表要记录数少,内表就...

    Oracle安装单机后自启动参数配置.txt

    # if the executables do not exist -- display error if [ ! -f $ORACLE_HOME/bin/dbstart -o ! -d $ORACLE_HOME ] then echo "Oracle startup: cannot start" exit 1 fi # depending on parameter -- startup, ...

    oracle数据库关于exists使用

    oracle数据库关于exists使用方法与in的比较

    最完整的Toad For Oracle使用手册

    Hints and Tips: Table Does Not Exist Errors 1019 Create Support Bundles 1019 Toad Advisor 1019 Contact Quest 1021 Contact Quest Support 1021 Contact Quest Software 1021 About Quest Software, Inc 1021 ...

    oracle的sql优化

     Count(1)和Count(*)差别不大。  多使用Decode函数来作简单的代码和名称间的转换,以减少表关联  使用Truncate替代delete来删除记录,但Truncate数据不记录日志,无法进行回滚  对于复杂的存储过程可以多次提交...

    Expert Oracle Database Architecture 2nd Edition

    I first met the Oracle RDBMS some time in 1988, or possibly 1987, when my manager dumped a small box on my desk and said something like: “There’s this new product called Oracle just coming into the...

    利用 Oracle 和 PHP 管理分布式跟踪文件

    -------------------------------------------------------------------------------------Deployment InstructionsOracle Layer The Oracle JServer must be installed and exist in a valid state. Ensure that ...

    easyload 9.0

    建立空间数据库时,easyload会自动创建一个mapinfo用户以及MAPINFO_MAPCATALOG表,但是easyload自己创建用户和表的时候好像不行,只能创建用户,MAPINFO_MAPCATALOG表创建失败,所以采用手工创建MAPINFO_MAPCATALOG...

    Oracle 10g 开发与管理

    我相信本文会对初学者使用oracle有一个初步的使用印象。右图为我所参 考的书籍。 目录 第一讲 Oacle关系数据库 9 一. Oracle的安装 9 二. 用浏览器进入em 企业管理器 11 三.启动DBCA的方法 11 四.服务设置 11...

    SQL优化之针对count、表的连接顺序、条件顺序、in及exist的优化

    主要介绍了SQL优化之针对count、表的连接顺序、条件顺序、in及exist的优化,有助于读者深入理解Oracle的运行效率及优化策略,需要的朋友可以参考下

    oracle删除已存在的表的实例

    Sql代码 代码如下:select count(*) from user_objects where... create or replace procedure p_drop_table_if_exist_v1( p_table_name in varchar2 ) is v_count number(10); begin select count(*) into v_cou

    Diamond.ADO.v2.00.for.Delphi.n.BCB.incl.sources-SSG

    Currently OLE DB providers exist for SQL Server, Oracle, Access, ODBC, Active Directory Services and the Index Server. Key features: High speed - fast, native access to data without BDE. ...

    MySQL Troubleshooting 原版PDF by Smirnova

    Oracle. During my daily job, I often see users who are stuck with a problem and have no idea what to do next. Well-verified methods exist to find the cause of the problem and fix it effectively, but ...

    SQL21日自学通

    第一周概貌 16 从这里开始 16 第一天SQL 简介 17 SQL 简史 17 数据库简史 17 设计数据库的结构21 SQL 总览23 流行的SQL 开发工具 24 SQL 在编程中的应用 27 第二天查询— — SELECT 语句的使用 30 目标 30 背景 30 ...

    plsqldev15.0.1.2051x64多语言版+sn

    64位版本的 PLSQL 正式版,只能运行在64位系统中,需要你安装 64 位的 Oracle 客户端。 安装请查看说明。 MAY 27, 2022 - VERSION 15.0.1 RELEASED Enhancements Stability improvements Difference Viewer ...

    plsqldev15.0.1.2051x32多语言版+sn

    32位版本的 PLSQL 正式版。 安装请查看说明。 MAY 27, 2022 - VERSION 15.0.1 RELEASED ...Generating a Test Script for a packaged procedure/function did not include the procedure/function name in

    整理后java开发全套达内学习笔记(含练习)

    exist 存在, 发生 [ig'zist] '(SQL关键字 exists) extends (关键字) 继承、扩展 [ik'stend] false (关键字) final (关键字) finally (关键字) fragments 段落; 代码块 ['frægmәnt] FrameWork [java] 结构,...

Global site tag (gtag.js) - Google Analytics