2017-09-15 1 views
0

Ich stehe vor einem Problem mit einer SQL-Abfrage, die die Werte, die ich will, aber nach einer sehr langen Zeit zurückgibt.SQL NICHT IN große Zeitantwort

was kann ich in meinem Code ändern Optimierung für Timing ?:

select * 
from t1 
where 
(t1.date between '01/01/2016' and '05/01/2016') 
and (t1.section = 'KA') 
and t1.protocol not in (select t2.protocol from t2 where (date between '01/01/2016' and '05/01/2016') and (section = 'KA')) 
+0

Wie viele Zeilen sind in t1 & t2? Wie ist die Struktur von t1 & t2? – SEarle1986

Antwort

1

Versuchen not exists mit:

select t1.* 
from t1 
where t1.date between '2016-01-01' and '2016-05-01' and 
     t1.section = 'KA' and 
     not exists (select 1 
        from t2 
        where t2.protocol = t1.protocol and 
         t2.date between '2016-01-01' and '2016-05-01' and 
         t2.section = 'KA' 
       ); 

Für die Leistung, die Sie wollen Indizes auf t1(section, date, protocol). und t2(protocol, section, date).

+0

Danke Mann! Das hat den Trick gemacht. Warum dauert es so lange mit der anderen Aussage? Danke für die Unterstützung – AnSharp

+0

@AndreaMorello. . . Unterschiedliche Möglichkeiten zur Formulierung einer Abfrage können zu unterschiedlichen Ausführungsplänen führen. "EXISTS"/"NOT EXISTS" erzeugt oft einen besseren Ausführungsplan in MySQL als "IN"/"NOT IN". –

Verwandte Themen