2017-09-11 6 views
1

Ich verwende Teradata, um Folgendes zu tun. Sagen, ich habe die folgende Tabelle 1Optimieren Teradata Abfrage mehrere Tabelle bedingte Join

col1 col2 col3 col4 col5 
1  A NULL NULL D 
2  B NULL NULL C  
3  A B  NULL D 
4  A B  C  D 

und Table2

col1 col2 col3 col4 
1  A D 27 
2  B C 334  
3  A B 434 
4  B D 100 
5  C D 200 

Ich möchte Tabelle 1 und Tabelle 2 verbinden (3 Mal sein), so dass ich

col1 col2 col3 col4 col5 col_val_1 col_val_2 col_val_3 
1  A NULL NULL D  27   NULL  NULL 
2  B NULL NULL C  334   NULL  NULL 
3  A B  NULL D  434   100  NULL 
4  A B  C  D  434   334  200 
eine Tabelle wie diese erstellen

Ich kann diese Tabelle mit dem folgenden Code erstellen

select tab1.*, tab2_1.col4 as col_val_1, tab2_2.col4 as col_cal_2, tab2_3.col4 as col_val_3 
from Table1 tab1 
left outer join Table2 tab2_1 
on tab2_1.col2 = tab1.col2 
and tab2_1.col3 = coalesce(tab1.col3,tab1.col5) /* if col3 is Null then join on col5. I want to calculate pair wise value. If col3 is NULL, the pair is col2-col5.*/ 
left outer join Table2 tab2_2 
on tab2_2.col2 = coalesce(tab1.col3,0) 
and tab2_2.col3 = coalesce(tab1.col4, tab1.col5) 
left outer join Table2 tab2_3 
on tab2_3.col2 = coalesce(tab1.col4,0) 
and tab2_3.col3 = tab1.col5 

Die Daten von Table1 sind so, dass col4 null ist, wenn col3 null ist. col2 und col5 sind niemals null. Wenn also col3 null ist, werde ich col2-col5 haben. Wenn col3 nicht null ist und col4 null ist, dann habe ich col2-col3, col3-col5. Wenn nichts null ist, dann habe ich col2-col3, col3-col4, col4-col5.

Diese Abfrage wird für eine kleine Tabelle ausgeführt und gibt die gewünschte Ausgabe aus. Dies ist jedoch eine komplexe Abfrage. Ich lief EXPLAIN auf diese und die geschätzte Laufzeit ist in >10^5 Stunden. Ich habe mich gefragt, ob es einen Weg gibt. Diese Abfrage kann optimiert werden.

+1

Bitte bearbeiten Sie Ihre Frage, um zu erklären, was Sie mit Ihren ON-Klauseln erreichen möchten. Sie zu vereinfachen wird wahrscheinlich sehr viel zur Performance beitragen. Besonders der Zweck von ON ... tab2_3.col2 = coalesce (tab1.col4,0) ist mysteriös. –

+0

Verwenden Sie MySQL oder Teradata? –

+0

Ich habe die Frage mit einer Erklärung der Joins aktualisiert. Die Tabelle1-Daten sind so, dass col4 null ist, wenn col3 null ist. col2 und col5 sind niemals null. Wenn also col3 null ist, werde ich col2-col5 haben. Wenn col3 nicht null ist und col4 null ist, dann habe ich col2-col3, col3-col5. Wenn nichts null ist, dann habe ich col2-col3, col3-col4, col4-col5. – deepAgrawal

Antwort

2

In der Regel Sie wirklich wollen Funktionen in Join-Bedingungen zu vermeiden. Es kann symptomatisch für Designfehler sein, wenn Sie es nicht umgehen können. Dies ist eine bizarre Art, zwei Tische zu verbinden.

Wenn jedoch dieser Ball nicht in Ihrem Gerichtssaal ist und Sie damit zu kämpfen haben, könnte dies eine bessere Leistung haben. Meine Vermutung ist, dass ein Produkt beitreten irgendwo passiert.

Es ist wirklich schwer handed though.

CREATE MULTISET VOLATILE TABLE Table1 AS tab1 
    (SELECT col1, 
      col2, 
      col3, 
      col4, 
      col5, 
      COALESCE(tab1.col3,tab1.col5) AS col_35, 
      COALESCE(tab1.col4,tab1.col5) AS col_45, 
      COALESCE(tab1.col3,0) AS col_30, 
      COALESCE(tab1.col4,0) AS col_40 
     FROM Table1 
) 
WITH DATA PRIMARY INDEX (col2) ON COMMIT PRESERVE ROWS; 

SELECT tab1.*, 
     tab2_1.col4 AS col_val_1, 
     tab2_2.col4 AS col_cal_2, 
     tab2_3.col4 AS col_val_3 
    FROM tab1 
    LEFT 
    JOIN Table2 AS tab2_1 
    ON tab2_1.col2 = tab1.col2 
    AND tab2_1.col3 = tab1.col_35 
    LEFT 
    JOIN Table2 AS tab2_2 
    ON tab2_2.col2 = tab1.col_30 
    AND tab2_2.col3 = tab1.col_45 
    LEFT 
    JOIN Table2 AS tab2_3 
    ON tab2_3.col2 = tab1.col_40 
    AND tab2_3.col3 = tab1.col_5; 
+0

Netter Benutzername. :) –

+0

Dank Rob ... schien für Stack Overflow geeignet. –