oracle中的exists 和not exists 用法详解(转)
有两个简单例子,以说明 “exists”和“in”的效率问题
1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;
T1数据量小而T2数据量非常大时,T1<<T2 时,1) 的查询效率高。
2) select * from T1 where T1.a in (select T2.a from T2) ;
T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。
exists 用法:
“select 1 from T2 where T1.a=T2.a” 相当于一个关联表查询,相当于
“select 1 from T1,T2 where T1.a=T2.a”
但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。
“exists(xxx)”如果括号里的语句能查出记录,则true;反之,false。
因此“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where 条件成立。
in 的用法:
继续引用上面的例子
“2) select * from T1 where T1.a in (select T2.a from T2) ”
这里的“in”后面括号里的语句搜索出来的字段的内容一定要相对应,一般来说,T1和T2这两个表的a字段表达的意义应该是一样的,否则这样查没什么意义。
打个比方:T1,T2表都有一个字段,表示工单号,但是T1表示工单号的字段名叫“ticketid”,T2则为“id”,但是其表达的意义是一样的,而且数据格式也是一样的。这时,用 2)的写法就可以这样:
“select * from T1 where T1.ticketid in (select T2.id from T2) ”
Select name from employee where name not in (select name from student);
Select name from employee where not exists (select name from student);
第一句SQL语句的执行效率不如第二句。
通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因
http://blog.sina.com.cn/s/blog_601d1ce30100cyrb.html
Tips:
oracle in的用法限制:
ORA-01975:列表中的最大表达式数为1000 ,解决办法是使用Or连接两个in或拆成两个sql,然后使用union连接
select *
from dba_users u
where u.user_id in (
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,
101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,
121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,
141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,
161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,
181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,
201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,
221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,
241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,
261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,
281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,
301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,
321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,
361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,
381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,
401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,
421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,
441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,
461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,
481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,
501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,
521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,
541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,557,558,559,560,
561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,580,
581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,
601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,
621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,
641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,
661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,
681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,
701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,
721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,
741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,
761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,
781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,
801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,
821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,
841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,
861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,
881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,
901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,
921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,
941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,
961,962,963,964,965,966,967,968,969,970,971,972,973,974,975,976,977,978,979,980,
981,982,983,984,985,986,987,988,989,990,991,992,993,994,995,996,997,998,999,1000,
1001)
oracle中的exists 和not exists 用法详解(转)的更多相关文章
- CSS中伪类及伪元素用法详解
CSS中伪类及伪元素用法详解 伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...
- SVN组成中trunk,branches and tags功能用法详解
SVN组成中trunk,branches and tags功能用法详解 我相信初学开发在SVN作为版本管理时,都估计没可能考虑到如何灵活的运用SVN来管理开发代码的版本,下面我就摘录一篇文章来简单说 ...
- Python中生成器和yield语句的用法详解
Python中生成器和yield语句的用法详解 在开始课程之前,我要求学生们填写一份调查表,这个调查表反映了它们对Python中一些概念的理解情况.一些话题("if/else控制流" ...
- Oracle中的游标的原理和使用详解
游标的简介 逐行处理查询结果,以编程的方式访问数据. 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql. 2,显式游标:显式游标用于处理 ...
- oracle中去掉回车换行空格的方法详解
函数: 1.translate语法:TRANSLATE(char, from, to)用法:返回将出现在from中的每个字符替换为to中的相应字符以后的字符串. 若from比to ...
- 【转】oracle中的游标的原理和使用详解
游标 游标的简介: 逐行处理查询结果,以编程的方式访问数据 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql. 2,显式游标:显式游标用 ...
- 转:Android中的Handler的机制与用法详解
注:Message类的用法: message的几个参数都可以携带数据,其中arg1与arg2可以携带int类型,what是用户自定义的int型,这样接受者可以了解这个消息的信息. 说明:使用Messa ...
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- Python3中strip()、lstrip()、rstrip()用法详解
Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格) lstrip:用来去除开头字符.空白 ...
- SVN中trunk、branches、tags用法详解
Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/. 那么标准的svn布局是:svn://proj/|+-trunk+-branches+-t ...
随机推荐
- js控件位置
function ShowSettingDiv() { var div = document.getElementById('ShowDiv'); //将要弹出的层 div.style.display ...
- qt 工具栏和菜单栏
在前面的QMainWindow的基础之上,我们开始着手建造我们的应用程序.虽然现在已经有一个框架,但是,确切地说我们还一行代码没有写呢!下面的工作就不那么简单了!在这一节里面,我们要为我们的框架添加菜 ...
- 再回首,Java温故知新(九):Java基础之流程控制语句
流程控制语句分为条件语句.循环语句和中断语句. 中断语句包括break和continue,两者的区别在于break会跳出整个循环,而continue则是跳出此次循环,之后还会继续下一次循环. 条件语句 ...
- [转] 使用Git进行小项目代码管理
http://www.uml.org.cn/pzgl/201206155.asp 之前在公司使用过SVN(无甚感觉)和ClearCase(把人恶心死的东西)两种版本控制工具,都不满意.后来想自己写点东 ...
- [转] Linux strace 简介
http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html 简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在L ...
- iOS流布局UICollectionView使用FlowLayout进行更灵活布局
一.引言 前面的博客介绍了UICollectionView的相关方法和其协议中的方法,但对布局的管理类UICollectionViewFlowLayout没有着重探讨,这篇博客介绍关于布局的相关设置和 ...
- Ant配置
首先去官网下载一个ant的文件 http://ant.apache.org/bindownload.cgi
- nopi导入导出
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Oracle实用技巧
一. ORACLE SQL PLUS 使用技巧: ----①查找重复记录: SELECT DRAWING, DSNOFROM EM5_PIPE_PREFABWHERE ROWID!= (SELECT ...
- Sqoop import加载HBase案例详解
简单写一下如何将订单表sqoop到hbase表中的步骤. 下表: 1.通过hbase shell 打开hbase. 2.创建一个hbase表 create 'so','o' 3.将so表的数据导入到h ...