1。已经检验的语句和已在共享池中的语句之间要完全一样
2。变量名称尽量一致
3。合理使用外联接
4。少用多层嵌套
5。多用并发
语句的优化步骤一般有:
1。调整sga区,使得sga区的是用最优。
2。sql语句本身的优化,工具有explain,sql trace等
3。数据库结构调整
4。项目结构调整
写语句的经验:
1。对于大表的查询使用索引
2、少用 in,exist等
3、使用集合运算
1.对于大表查询中的列应尽量避免进行诸如To_char,to_date,to_number 等转换
2.有索引的尽量用索引,有用到索引的条件写在前面如有可能和有必要就建立一些索引
3.尽量避免进行全表扫描,限制条件尽可能多,以便更快搜索到要查询的数据
如何让你的SQL运行得更快
交通银行长春分行电脑部
任亮
---- 人们在使用SQL时往往会陷入一个误区,即太关注于所得的结果是否正确,而忽略了不同的实现方法之间可能存在的性能差异,这种性能差异在大型的或是复杂的数据库环境中(如联机事务处理OLTP或决策支持系统DSS)中表现得尤为明显。笔者在工作实践中发现,不良的SQL往往来自于不恰当的索引设计、不充份的连接条件和不可优化的where子句。在对它们进行适当的优化后,其运行速度有了明显地提高!下面我将从这三个方面分别进行总结:
---- 为了更直观地说明问题,所有实例中的SQL运行时间均经过测试,不超过1秒的均表示为(< 1秒)。
---- 测试环境--
---- 主机:HP LH II
---- 主频:330MHZ
---- 内存:128兆
---- 操作系统:Operserver5.0.4
---- 数据库:Sybase11.0.3
一、不合理的索引设计
----例:表record有620000行,试看在不同的索引下,下面几个 SQL的运行情况:
---- 1.在date上建有一非个群集索引
select count(*) from record where date >
'19991201' and date < '19991214'and amount >
2000 (25秒)
select date,sum(amount) from record group by date
(55秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH') (27秒)
---- 分析:
----date上有大量的重复值,在非群集索引下,数据在物理上随机存放在数据页上,在范围查找时,必须执行一次表扫描才能找到这一范围内的全部行
---- 2.在date上的一个群集索引
select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000 (14秒)
select date,sum(amount) from record group by date
(28秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH')(14秒)
---- 分析:
---- 在群集索引下,数据在物理上按顺序在数据页上,重复值也排列在一起,因而在范围查找时,可以先找到这个范围的起末点,且只在这个范围内扫描数据页,避免了大范围扫描,提高了查询速度。
---- 3.在place,date,amount上的组合索引
select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000 (26秒)
select date,sum(amount) from record group by date
(27秒)
select count(*) from record where date >
'19990901' and place in ('BJ, 'SH')(< 1秒)
---- 分析:
---- 这是一个不很合理的组合索引,因为它的前导列是place,第一和第二条SQL没有引用place,因此也没有利用上索引;第三个SQL使用了 place,且引用的所有列都包含在组合索引中,形成了索引覆盖,所以它的速度是非常快的。
---- 4.在date,place,amount上的组合索引
select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000(< 1秒)
select date,sum(amount) from record group by date
(11秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH')(< 1秒)
---- 分析:
---- 这是一个合理的组合索引。它将date作为前导列,使每个SQL都可以利用索引,并且在第一和第三个SQL中形成了索引覆盖,因而性能达到了最优。
---- 5.总结:
---- 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的;合理的索引设计要建立在对各种查询的分析和预测上。一般来说:
---- ①.有大量重复值、且经常有范围查询(between, >,< ,>=,< =)和order by group by发生的列,可考虑建立群集索引;
---- ②.经常同时存取多列,且每列都含有重复值可考虑建立组合索引; ---- ③.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列。
二、不充份的连接条件:
---- 例:表card有7896行,在card_no上有一个非聚集索引,表account有191122行,在 account_no上有一个非聚集索引,试看在不同的表连接条件下,两个SQL的执行情况:
select sum(a.amount) from account a,
card b where a.card_no = b.card_no(20秒)
---- 将SQL改为:
select sum(a.amount) from account a,
card b where a.card_no = b.card_no and a.
account_no=b.account_no(< 1秒)
---- 分析:
---- 在第一个连接条件下,最佳查询方案是将account作外层表,card作内层表,利用card上的索引,其I/O次数可由以下公式估算为:
---- 外层表account上的22541页+(外层表account的191122行*内层表card上对应外层表第一行所要查找的3页)=595907次 I/O
---- 在第二个连接条件下,最佳查询方案是将card作外层表,account作内层表,利用account上的索引,其I/O次数可由以下公式估算为:
---- 外层表card上的1944页+(外层表card的7896行*内层表account上对应外层表每一行所要查找的4页)= 33528次I/O
---- 可见,只有充份的连接条件,真正的最佳方案才会被执行。
---- 总结:
---- 1.多表操作在被实际执行前,查询优化器会根据连接条件,列出几组可能的连接方案并从中找出系统开销最小的最佳方案。连接条件要充份考虑带有索引的表、行数多的表;内外表的选择可由公式:外层表中的匹配行数*内层表中每一次查找的次数确定,乘积最小为最佳方案。
---- 2.查看执行方案的方法-- 用set showplanon,打开showplan选项,就可以看到连接顺序、使用何种索引的信息;想看更详细的信息,需用sa角色执行 dbcc(3604,310,302)。
三、不可优化的where子句
---- 1.例:下列SQL条件语句中的列都建有恰当的索引,但执行速度却非常慢:
select * from record where
substring(card_no,1,4)='5378'(13 秒)
select * from record where
amount/30< 1000(11秒)
select * from record where
convert(char(10),date,112)='19991201'(10秒)
---- 分析:
---- where子句中对列的任何操作结果都是在SQL运行时逐列计算得到的,因此它不得不进行表搜索,而没有使用该列上面的索引;如果这些结果在查询编译时就能得到,那么就可以被SQL优化器优化,使用索引,避免表搜索,因此将SQL重写成下面这样:
select * from record where card_no like'5378%'(< 1秒)
select * from record where amount
< 1000*30(< 1秒)
select * from record where date= '1999/12/01'
(< 1秒)
---- 你会发现SQL明显快起来!
---- 2.例:表stuff有200000行,id_no上有非群集索引,请看下面这个SQL:
select count(*) from stuff where id_no in('0','1')
(23 秒)
---- 分析:
---- where条件中的'in'在逻辑上相当于'or',所以语法分析器会将in ('0','1')转化为id_no ='0' or id_no='1'来执行。我们期望它会根据每个or子句分别查找,再将结果相加,这样可以利用id_no上的索引;但实际上(根据showplan), 它却采用了"OR策略",即先取出满足每个or子句的行,存入临时数据库的工作表中,再建立唯一索引以去掉重复行,最后从这个临时表中计算结果。因此,实际过程没有利用id_no上索引,并且完成时间还要受tempdb数据库性能的影响。
---- 实践证明,表的行数越多,工作表的性能就越差,当stuff有620000行时,执行时间竟达到220秒!还不如将or子句分开:
select count(*) from stuff where id_no='0'
select count(*) from stuff where id_no='1'
---- 得到两个结果,再作一次加法合算。因为每句都使用了索引,执行时间只有3秒,在620000行下,时间也只有4秒。或者,用更好的方法,写一个简单的存储过程:
create proc count_stuff as
declare @a int
declare @b int
declare @c int
declare @d char(10)
begin
select @a=count(*) from stuff where id_no='0'
select @b=count(*) from stuff where id_no='1'
end
select @c=@a+@b
select @d=convert(char(10),@c)
print @d
---- 直接算出结果,执行时间同上面一样快!
---- 总结:
---- 可见,所谓优化即where子句利用了索引,不可优化即发生了表扫描或额外开销。
---- 1.任何对列的操作都将导致表扫描,它包括数据库函数、计算表达式等等,查询时要尽可能将操作移至等号右边。
---- 2.in、or子句常会使用工作表,使索引失效;如果不产生大量重复值,可以考虑把子句拆开;拆开的子句中应该包含索引。
---- 3.要善于使用存储过程,它使SQL变得更加灵活和高效。 ---- 从以上这些例子可以看出,SQL优化的实质就是在结果正确的前提下,用优化器可以识别的语句,充份利用索引,减少表扫描的I/O次数,尽量避免表搜索的发生。其实SQL的性能优化是一个复杂的过程,上述这些只是在应用层次的一种体现,深入研究还会涉及数据库层的资源配置、网络层的流量控制以及操作系统层的总体设计。
缺省情况下建立的索引是非群集索引,但有时它并不是最佳的;合理的索引设计要建立在对各种查询的分析和预测上。一般来说:
①.有大量重复值、且经常有范围查询(between, >,< ,>=,< =)和order by、group by发生的列,可考虑建立群集索引;
②.经常同时存取多列,且每列都含有重复值可考虑建立组合索引;
③.组合索引要尽量使关键查询形成索引覆盖,其前导列一定是使用最频繁的列。
二、不充份的连接条件:
例:表card有7896行,在card_no上有一个非聚集索引,表account有191122行,在 account_no上有一个非聚集索引,试看在不同的表连接条件下,两个SQL的执行情况:select sum(a.amount) from account a,card b where a.card_no = b.card_no(20秒)
将SQL改为:select sum(a.amount) from account a,card b where a.card_no = b.card_no and a.account_no=b.account_no(< 1秒)
分析:
在第一个连接条件下,最佳查询方案是将account作外层表,card作内层表,利用card上的索引,其I/O次数可由以下公式估算为:
外层表account上的22541页+(外层表account的191122行*内层表card上对应外层表第一行所要查找的3页)=595907次 I/O
在第二个连接条件下,最佳查询方案是将card作外层表,account作内层表,利用account上的索引,其I/O次数可由以下公式估算为:
外层表card上的1944页+(外层表card的7896行*内层表account上对应外层表每一行所要查找的4页)= 33528次I/O
可见,只有充份的连接条件,真正的最佳方案才会被执行。
总结:
1.多表操作在被实际执行前,查询优化器会根据连接条件,列出几组可能的连接方案并从中找出系统开销最小的最佳方案。连接条件要充份考虑带有索引的表、行数多的表;内外表的选择可由公式:外层表中的匹配行数*内层表中每一次查找的次数确定,乘积最小为最佳方案。
2.查看执行方案的方法-- 用set showplanon,打开showplan选项,就可以看到连接顺序、使用何种索引的信息;想看更详细的信息,需用sa角色执行 dbcc(3604,310,302)。
三、不可优化的where子句
1.例:下列SQL条件语句中的列都建有恰当的索引,但执行速度却非常慢:
select * from record where substring(card_no,1,4)='5378'(13 秒)
select * from record where amount/30< 1000(11秒)
select * from record where convert(char(10),date,112)='19991201'(10秒)
分析:
where子句中对列的任何操作结果都是在SQL运行时逐列计算得到的,因此它不得不进行表搜索,而没有使用该列上面的索引;如果这些结果在查询编译时就能得到,那么就可以被SQL优化器优化,使用索引,避免表搜索,因此将SQL重写成下面这样:
select * from record where card_no like '5378%'(< 1秒)
select * from record where amount< 1000*30(< 1秒)
select * from record where date= '1999/12/01'(< 1秒)
你会发现SQL明显快起来!
2.例:表stuff有200000行,id_no上有非群集索引,请看下面这个SQL:
select count(*) from stuff where id_no in('0','1') (23秒)
分析:
where条件中的'in'在逻辑上相当于'or',所以语法分析器会将in ('0','1')转化为id_no ='0' or id_no='1'来执行。我们期望它会根据每个or子句分别查找,再将结果相加,这样可以利用id_no上的索引;但实际上(根据showplan), 它却采用了"OR策略",即先取出满足每个or子句的行,存入临时数据库的工作表中,再建立唯一索引以去掉重复行,最后从这个临时表中计算结果。因此,实际过程没有利用id_no上索引,并且完成时间还要受tempdb数据库性能的影响。
实践证明,表的行数越多,工作表的性能就越差,当stuff有620000行时,执行时间竟达到220秒!还不如将or子句分开:
select count(*) from stuff where id_no='0'
select count(*) from stuff where id_no='1'
得到两个结果,再作一次加法合算。因为每句都使用了索引,执行时间只有3秒,在620000行下,时间也只有4秒。或者,用更好的方法,写一个简单的存储过程:
create proc count_stuff as
declare @a int
declare @b int
declare @c int
declare @d char(10)
begin
select @a=count(*) from stuff where id_no='0'
select @b=count(*) from stuff where id_no='1'
end
select @c=@a+@b
select @d=convert(char(10),@c)
print @d
直接算出结果,执行时间同上面一样快!
总结:
可见,所谓优化即where子句利用了索引,不可优化即发生了表扫描或额外开销。
1.任何对列的操作都将导致表扫描,它包括数据库函数、计算表达式等等,查询时要尽可能将操作移至等号右边。
2.in、or子句常会使用工作表,使索引失效;如果不产生大量重复值,可以考虑把子句拆开;拆开的子句中应该包含索引。
3.要善于使用存储过程,它使SQL变得更加灵活和高效。
从以上这些例子可以看出,SQL优化的实质就是在结果正确的前提下,用优化器可以识别的语句,充份利用索引,减少表扫描的I/O次数,尽量避免表搜索的发生。其实SQL的性能优化是一个复杂的过程,上述这些只是在应用层次的一种体现,深入研究还会涉及数据库层的资源配置、 网络 层的流量控制以及操作系统层的总体设计。
oracle 中sql语句的优化(转帖)
一、执行顺序及优化细则
1.表名顺序优化
(1) 基础表放下面,当两表进行关联时数据量少的表的表名放右边
表或视图:
Student_info (30000条数据)
Description_info (30条数据)
select *
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
与
select *
from student_info si--学生信息表
,description_info di
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
以 student_info作为基础表,你会发现运行的速度会有很大的差距。
(2) 当出现多个表时,关联表被称之为交叉表,交叉表作为基础表
select *
from description_info di
,description_info di2
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and si.school_id = di.lookup_co
and di.lookup_type(+) = 'SCHOOL_ID'
与
select *
from student_info si--学生信息表
,description_info di
,description_info di2
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and si.school_id = di.lookup_co
and di.lookup_type(+) = 'SCHOOL_ID'
以 student_info作为基础表,你会发现运行的速度会有很大的差距,
当基础表放在后面,这样的执行速度会明显快很多。
2.where执行顺序
where执行会从至下往上执行
select *
from student_info si --学生信息表
where si.school_id=10 --学院ID
and si.system_id=100--系ID
摆放where子句时,把能过滤大量数据的条件放在最下边
3. is null 和is not null
当要过滤列为空数据或不为空的数据时使用
select *
from student_info si --学生信息表
where si.school_id is null(当前列中的null为少数时用is not null,否则is null)
4.使用表别名
当查询时出现多个表时,查询时加上别名,
避免出现减少解析的时间字段歧义引起的语法错误。
5. where执行速度比having快
尽可能的使用where代替having
select from student_info si
group by si.student_id
having si.system_id!=100
and si.school_id!=10
(select from student_info si
wehre si.system_id!=100
and si.school_id!=10
group by si.student_id)
6. * 号引起的执行效率
尽量减少使用select * 来进行查询,当你查询使用*,
数据库会进行解析并将*转换为全部列。
二、替代优化
1、用>=替代>
select ui.user_name
from user_info ui--员工信息表
where ui.student_id>=10
与
select ui.user_name
from user_info ui--员工信息表
where ui.student_id>9
执行时>=会比>执行得要快
2、用UNION替换OR (适用于索引列)
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=10
union
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=2
上面语句可有效避免全表查询
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=10
or ui.student_id=2
如果坚持要用OR, 可以把返回记录最少的索引列写在最前面
3、用in 代替or
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=10
or ui.student_id=20
or ui.student_id=30
改成
select ui.user_name
from user_info ui--员工信息表
where ui.student_id in (10,20,30)
执行会更有效率
4、 Union All 与Union
Union All重复输出两个结果集合中相同记录
如果两个并集中数据都不一样.那么使用Union All 与Union是没有区别的,
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=10
union All
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=2
与
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=10
union
select ui.user_name
from user_info ui--员工信息表
where ui.student_id=2
但Union All会比Union要执行得快
5、分离表和索引
总是将你的表和索引建立在另外的表空间内
决不要将这些对象存放到SYSTEM表空间里
三、一些优化技巧
1、计算表的记录数时
select count(si.student_id)
from Student_info si(student_id为索引)
与
select count(*) from Student_info si
执行时.上面的语句明显会比下面没有用索引统计的语句要快
2.使用函数提高SQL执行速度
当出现复杂的查询sql语名,可以考虑使用函数来提高速度
查询学生信息并查询学生(李明)个人信息与的数学成绩排名
如
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.description = '李明'
而且我们将上面order_num排名写成一个fuction时
create or replace package body order_num_pkg is
function order_num(p_student_id number) return_number is
v_return_number number;
begin
select res.order_num --排名
into v_return_number
from result res
where res.student_id = di.student_id
order by result_math;
return v_return_number;
exception
when others then
null;
return null;
end;
end order_num_pkg;
执行
select di.description student_name
,order_num_pkg.order_num(di.student_id) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.description = '李明'
执行查询时的速度也会有所提高
3. 减少访问数据库的次数
执行次数的减少(当要查询出student_id=100的学生和student_id=20的学生信息时)
select address_id
from student_info si --学生信息表
where si.student_id=100
与
select address_id
from student_info si --学生信息表
where si.student_id=20
都进行查询.这样的效率是很低的
而进行
(
select si.address_id,si2.address_id
from student_info si --学生信息表
,student_info si2
where si.student_id=100
and si2.student_id=20
与
select decode(si.student_id,100,address_id)
,decode(si.student_id,20,address_id)
from student_info si
)
执行速度是提高了,但可读性反而差了..
所以这种写法个人并不太推荐
4、用Exists(Not Exists)代替In(Not In)
在执行当中使用Exists或者Not Exists可以高效的进行查询
5、Exists取代Distinct取唯一值的
取出关联表部门对员工时,这时取出员工部门时,出现多条..
select distinct di.dept_name
from departments_info di --部门表
,user_info ui --员工信息表
where ui.dept_no = di.dept_no
可以修改成
select di.dept_name
from departments_info di --部门表
where exists (select 'X'
from user_info ui --员工信息表
where di.dept_no = ui.dept_no)
6、用表连接代替Exists
通过表的关联来代替exists会使执行更有效率
select ui.user_name
from user_info ui--员工信息表
where exists (select 'x '
from departments_info di--部门表
where di.dept_no = ui.dept_no
and ui.dept_cat = 'IT');
执行是比较快,但还可以使用表的连接取得更快的查询效率
select ui.user_name
from departments_info di
,user_info ui --员工信息表
where ui.dept_no = di.dept_no
and ui.department_type_co
代码是经测试并进行优化所写,
以上只例子,具体使用还是要针对各个不同的具体的业务使用用Exists(Not Exists)代替In(Not In)
四、索引篇
1、运算导致的索引失效
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and si.student_id+0=100/*student_id索引将失效*/
2、类型转换导致的索引失效
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.student_id='100'
student_id 为number类型的索引,当执行下列语句,
oracle会自动转换成
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.student_id=to_number('100')
所幸,只是解析并转换类型,并没有导到失效,
但要是写成下面,将会使用其失效
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and to_char(di.student_id)='100'
3、在索引列上进行计算引起的问题
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.student_id-2=10
在索引列中进行运算,将会不使用索引而使用全表扫描
而将
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.student_id=10+2
将会得到高效的运行速度
4、 Is not null引起的问题(student_id为索引)
不要把存在空值的列做为索引,否则无法使用索引
select ui.user_name
from user_info ui--员工信息表
where ui.student_id is not null--索引失效
select ui.user_name
from user_info ui--员工信息表
where ui.student_id>=-1--索引有效
5、Order by导致索引失效(student_id为索引)
select ui.user_name
from user_info ui--员工信息表
group by ui.student_id
而使用
select ui.user_name
from user_info ui--员工信息表
where ui.student_id>=-1
将使其有效,
在order by中只存在两种条件下可以使用索引
(ORDER BY中所有的列必须包含在相同的索引中并保持在索引中的排列顺序
ORDER BY中所有的列必须定义为非空. )
6、自动选择索引
如果表中有两个以上(包括两个)索引,其中有一个唯一性索引,而其他是非唯一性.
在这种情况下,ORACLE将使用唯一性索引而完全忽略非唯一性索引.
7、 !=导致索引失效
select ui.user_name
from user_info ui--员工信息表
where ui.student_id!=0
在Where中使用!=将会把索引失效
8、%导致的索引失效
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.look_co
而
select di.description student_name
,(select res.order_num--排名
from result res
where res.student_id = di.student_id
order by result_math) order_num
from description_info di
,student_info si --学生信息表
where si.student_id = di.lookup_co
and di.lookup_type(+) = 'STUDENT_ID'
and di.look_co
以上只例子,具体还是要针对各个不同的具体的业务使用
五、oracle 中的not Exists与Not in的性能巨大差异
Not Exists与Not in的作用同样是排除数据,在oracle 中使用not in并不象mysql中的执行那么快,如(
select jt1.doc_num --单据号码
,oalc.description school_name --学校名称
,oalc2.description system_name --系名称
,oalc.description class_name --班级名称
from java_table1 jt1
,java_table_description oalc
,java_table_description oalc2
,java_table_description oalc3
where oalc.lookup_type(+) = 'JAVA_SCHOOL_NAME'
and jt1.school_id = oalc.lookup_co
and oalc2.lookup_type(+) = 'JAVA_SYSTEM_NAME'
and jt1.system_id = oalc2.lookup_co
and oalc3.lookup_type(+) = 'JAVA_CLASS_NAME'
and jt1.class_id = oalc3.lookup_co
and not exists
(select jt2.header_id
from java_table2 jt2 jt1.header_id = jt2.header_id))
与
select jt1.doc_num --单据号码
,oalc.description school_name --学校名称
,oalc2.description system_name --系名称
,oalc.description class_name --班级名称
from java_table1 jt1
,java_table_description oalc
,java_table_description oalc2
,java_table_description oalc3
where oalc.lookup_type(+) = 'JAVA_SCHOOL_NAME'
and jt1.school_id = oalc.lookup_co
and oalc2.lookup_type(+) = 'JAVA_SYSTEM_NAME'
and jt1.system_id = oalc2.lookup_co
and oalc3.lookup_type(+) = 'JAVA_CLASS_NAME'
and jt1.class_id = oalc3.lookup_co
and jt1.header_id not in (select jt2.header_id from java_table2 jt2)
当jt2表中的数据比较大时,就会出现巨大的差异,以上只能是我的个人理解与测试结果(java_table1 视图测试
数据量为36749,java_table2 为300条),如有其它可相互讨论