/以下sql语法为优化前后,查看有何妙处*****/
–前
select n.c1, n.c2,n.c3,n.c4,n.c5 from
(
select count(t.c1), t.c1, t.c2,t.c3,t.c4,t.c5 from tab1 t
where t.c2 not in (‘val1’,’val2’,’val3’,’val4’,’val5’)
group by t.c1, t.c2,t.c3,t.c4,t.c5
) n
where 1=1
and n.c1 is not null
and not exists
(
select * from
(
select count(s.c2), s.c1, s.c2
from (
select m.c1, m.c2,m.c3,m.c4,m.c5 from tab1 m
where exists
(
select c1 from tab2 n
where c2 > sysdate - 14
and m.c1 = n.c1
)
and m.c1 is not null
and m.c2 not in (‘val1’,’val2’, ‘val3’, ‘val4’, ‘val5’)
) s
group by s.c1, s.c2
)
t1 where t1.c2 = n.c2
)
–后
with t1 as(
select count(t.c1), t.c1,t.c2,t.c3,t.c4,t.c5
from tab1 t
where t.c2 not in (‘val1’,’val2’,’val3’,’val4’,’val5’)
and c1 is not null
group by t.c1, t.c2,t.c3,t.c4,t.c5)
select t1.c1,t1.c2,t1.c3,t1.c4,t1.c5
from t1
where not exists(
select /+ use_hash(m,n)/ m.c1, m.c2,m.c3,m.c4,m.c5
from t1 m,tab2 n
where n.c2 > sysdate - 14
and m.c1 = n.c1
and t1.c2 = m.c2);