Welcome 微信登录

首页 / 数据库 / MySQL / Oracle中Left join的on和where的效率差别

假设有两个表a、b使用onSelect * from a left join b on b.col = a.col and b.col2 = ‘aa’使用 whereSelect * from a left join b on b.col = a.col where b.col2 = ‘aa’ and b.col2 is null// b.col2 is null作用是防止因b表中没有匹配数据,照成a表原有记录无法返回的问题 分析1、on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。 语句测试set serveroutput on ; -- 必须运行,否则打印结果无法显示declareI Number;Starttime Timestamp;Endtime Timestamp;Beginselect current_timestamp(5) into starttime from Dual;I := 1;While I<=10000 Loopdbms_output.put_line(i);Execute Immediate " "; --此处放入sql语句i := i+1;End Loop;Select Current_Timestamp(5) Into Endtime From Dual;dbms_output.put_line("10000条语句运行时间为(毫秒):" || (endtime - starttime)); --打印一个Interval类型数据,显示时间差end; 测试结果On语句, 10000条语句运行时间为(毫秒):+000000000 00:00:01.032850000Where 语句 10000条语句运行时间为(毫秒):+000000000 00:00:01.013420000 结论Where语句的性能优于on语句其实sql语句的运行效率也可以通过查询Oracle的系统视图来查看,但时间关系今后再研究了。 在C#中使用linq进行查询// 写得比较仓促,见谅了var reList = from DataRow a in dtA.Rowsjoin DataRow b in dtB.Rows onnew {t = a["col"], l=’aa’}equalsnew {t = b["col"], l = b["col2"] }into rightRow from rw in rightRow.DefaultIfEmpty()select new{Col1 = a["col"],Col2 = rw["col2"]};在linq中使用into rightRow from rw in rightRow.DefaultIfEmpty()可以保证查询类型left outer join的效果,如果left join中有多个查询条件,使用new两个对象进行比较即可。更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12Oracle 释放flash recovery area的四种方法生成动态前缀且自增号码的Oracle函数相关资讯      Oracle Left join  本文评论 查看全部评论 (0)
表情: 姓名: 字数