A few days ago, a user developed QQ to find me, as follows:
The free Dolphin. 16:12:01
The island owner, my SQL can not find the results, could you help me see?
Orchid Island Main 16:12:10
How long is the result?
The Free Dolphin 16:12:17
How long has not produced the result, has not seen the result.
Orchid Island Main 16:12:26
Oh, good.
Orchid Island Main 16:12:39
Send the SQL and execution plan.
The Free Dolphin 16:12:55
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 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)
And N.C1 is not null;
Orchid Island Main 16:13:12
Are these two tables large?
The Free Dolphin 16:13:16
TAB1 small, TAB2 billion level above, two weeks data in tens of millions of.
Lan Hua Main 16:13:22
Ok.
Orchid Island Main 16:16:29
So change the SQL:
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);
Orchid Island Main 16:16:43
Remove the execution plan.
The Free Dolphin 16:16:57
Good.
The Free Dolphin 16:17:25
Orchid Island Main 16:17:57
OK, try it.
The Free Dolphin 16:19:28
The result, 37s.
Orchid Island Main 17:20:21
Yes, well.
Orchid Island Main 17:20:34
Is that OK?
The Free Dolphin 17:20:47
Yes, we can.
Orchid Island Main 17:21:11
Well, well, that's the first thing to do.
The Free Dolphin 17:21:30
Well, thank you, Island Master.
Orchid Island Main 17:21:53
You are welcome, busy, have a contact.
The Free Dolphin 17:22:18
Well, you are busy ...
At this point, the user of this SQL optimization, in fact, this statement should also have the optimization of space, but the user said that it is possible, because the optimization is endless, and further optimization may require further information, and sometimes need more changes, in view of various factors, In the text, the statement and plan are processed, recorded in this, mutual encouragement!
SQL optimization (SQL TUNING) 10-minute completion of billions of data volume performance optimization (SQL tuning)