日期:2014-05-16  浏览次数:21318 次

oracle联合查询优化问题
假设:(只是假设)
现在比如我有三张表test1 ,test2,test3
SQL code

create table test1(
id number primary key,
name1 varchar2(20),
t_test2_id number foreign key--------指向test2
)
create table test2(
id number primary key,
name2 varchar2(20),
t_test3_id number foreign key--------指向test3
)
create table test3(
id number primary key,
name3 varchar2(20)
)


我现在需要每个表中的name*那个字段,并且排除表2中的一些记录,比如id为1,3,5,7,9,10这些的。

直接用
SQL code

select t1.id,t1.name1,t2.name2,t3.name3
from test1 t1,test2 t2,test3,t3
where t1.t_test2_id = t2.id and t2.t_test3_id = t3.id and t2.id not in(1,3,5,7,9,10);


这样貌似很低的效率,假设数据量过千万


------解决方案--------------------
from t1,t2,t3 按表的大小从大到小排列..
not in 改成 not exists
------解决方案--------------------
在t2.id,t3.id上建唯一索引..
在t1.t_test2_id,t2.t_test3_id 建非唯一索引