`
UATT
  • 浏览: 2337 次
  • 性别: Icon_minigender_1
  • 来自: 河南
文章分类
社区版块
存档分类
最新评论

oracle中 in和exist

阅读更多
in 和 exists区别

in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。

一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。

如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:

例如:表A(小表),表B(大表)
1:
select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引;
select * from A where exists(select cc from B where cc=A.cc)
效率高,用到了B表上cc列的索引。
相反的
2:
select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引;
select * from B where exists(select cc from A where cc=B.cc)
效率低,用到了A表上cc列的索引。
带in的关联子查询是多余的,因为in子句和子查询中相关的操作的功能是一样的。如:
select staff_name from staff_member where staff_id in
(select staff_id from staff_func where staff_member.staff_id=staff_func.staff_id);

为非关联子查询指定exists子句是不适当的,因为这样会产生笛卡乘积。如:
select staff_name from staff_member where staff_id
exists (select staff_id from staff_func);

not in 和not exists


如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;
而not extsts 的子查询依然能用到表上的索引。
所以无论哪个表大,用not exists都比not in要快。

尽量不要使用not in子句。使用minus 子句都比not in 子句快,虽然使用minus子句要进行两次查询:
select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%');

分享到:
评论

相关推荐

    简述Oracle中in和exists的不同

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

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

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

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

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

    oracle数据库关于exists使用

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

    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的sql优化

     对Oracle共享池和缓冲区中的Sql必须要大小写都完全用上才能够匹配上 2.顺序问题  Oracle按照从右到左的顺序对数据表进行解析。因此From最后面的表为基础表,一般要选择记录数最少的表作为基础表。  对于Where...

    最完整的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 ...

    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...

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

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

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

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

    Oracle 10g 开发与管理

    (2)Exist 53 (3)Some、Any和All 53 五.表的查询练习 54 第七讲 PL/SQL编程基础 56 7.1 PL/SQL 程序结构 56 1.PL/SQL块结构 56 2.PL/SQL块的分类(按照特性划分) 56 7.2变量与常量 56 (一)PL/SQL标识符 (给...

    easyload 9.0

    MapInfo空间数据库建立 MapInfo空间数据库方案: 数据库:Oracle9i 数据上传工具:easyload ...use, this column must exist in order for MapX to correctly identify and apply record-level styles. NULL

    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

    plsqldev15.0.1.2051x64多语言版+sn

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

    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 ...

    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. ...

    SQL21日自学通

    从外部数据源中导入和导出数据169 Microsoft Access 170 Microsoft and Sybase SQL Server 171 Personal Oracle7171 总结172 问与答172 校练场173 练习173 第九天创建和操作表174 目标174 CREATE DATABASE 语句174 ...

    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

    Delphi7.1 Update

    Delphi 7.1 Update Release Notes=======================================================This file contains important supplemental and late-breakinginformation that may not appear in the main ...

Global site tag (gtag.js) - Google Analytics