2017-05-29 103 views
1

我有兩個表T1和T2。爲此查詢優化SQL語句?

T1有ID,F1,F2,F3,F4,F5,F6,F7,F8

T2具有ID,T1_ID,F1,F2,F3,F4,F5,F6,F7,F8,SUM

實例數據爲T1和T2

T1 
ID,F1,F2,F3,F4,F5,F6,F7,F8 
    1, 1, 2, 3, 0, 0, 5, 0, 0 
    2, 0, 0, 0, 1, 0, 4, 5, 0 
    3, 4, 1, 3, 2, 0, 0, 0, 5 
    4, 1 ,3, 4, 0, 0 ,0, 0, 0 
    5, 7, 2, 1, 3, 0, 0, 0, 0 
    . 
    . 
    . 
T2 
ID,T1_ID,F1,F2,F3,F4,F5,F6,F7,F8,SUM 
    1, 1, 2, 3, 5, 0, 0, 3, 0, 0,100 
    2, 5, 9, 8, 8, 1, 0, 0, 0, 0,200 
    3, 2, 0, 0, 0, 5, 0, 6, 6, 0,300 
    4, 1 ,3, 4, 2, 0 ,0, 3, 0, 0,255 
    5, 4, 8, 8, 8, 0, 0, 0, 0, 0,155 
    . 
    . 

SELECT * FROM T 2,其中T1.F1 .... T1.F8具有(1和2和3)

查詢必須返回記錄1, 2,4

1, 1, 2, 3, 5, 0, 0, 3, 0, 0,100 
    2, 5, 9, 8, 8, 1, 0, 0, 0, 0,200 
    4, 1 ,3, 4, 2, 0 ,0, 3, 0, 0,255 

我創建這個查詢

Select T2.ID,T2.F1,T2.F2,T2.F3,T2.F4.T2.F5,T2.F6,T2.F7,T2.F8,T2.SUM,T1.ID 
from T2 
join T1 on T1.ID = T2.T1_ID 
    where 
    (CASE WHEN (T1_ID.F1 = 1) THEN T2.F1 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 2) THEN T2.F1 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    or 
    (CASE WHEN (T1_ID.F1 = 1) THEN T2.F1 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F2 = 2) THEN T2.F2 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    or 
    (CASE WHEN (T1_ID.F1 = 1) THEN T2.F1 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F3 = 2) THEN T2.F3 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    or 
    (CASE WHEN (T1_ID.F1 = 1) THEN T2.F1 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F4 = 2) THEN T2.F4 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    . 
    . 
    . 
    or 
    (CASE WHEN (T1_ID.F2 = 1) THEN T2.F2 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 2) THEN T2.F1 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    or 
    (CASE WHEN (T1_ID.F2 = 1) THEN T2.F2 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F2 = 2) THEN T2.F2 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    or 
    (CASE WHEN (T1_ID.F2 = 1) THEN T2.F2 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F3 = 2) THEN T2.F3 between 0 and 1000 end) 
and(CASE WHEN (T1_ID.F1 = 3) THEN T2.F1 between 0 and 1000 end) 
    . 
    . 
    . 

這是太大的語句。 如何優化語句?

+0

您已有類似問題。 (即使是一樣)在那裏有兩個答案,你對他們沒有任何反應。請編輯您之前的問題。 –

+1

這個表最好的SQL語句的可能的重複?(https://stackoverflow.com/questions/44179634/best-sql-statement-for-this-table) –

+0

這些問題是類似的是,但不相等。在第二部分中,我比較了兩張表格,併爲此尋找更好的說法。 – user2531567

回答

0
SELECT * FROM T2 
WHERE EXISTS ( SELECT N.ID 
       FROM( SELECT T1.ID , T1.F1 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F2 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F3 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F4 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F5 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F6 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F7 AS F 
          FROM T1 
          UNION ALL 
          SELECT T1.ID , T1.F8 AS F 
          FROM T1 
          UNION ALL 
         ) N 
       WHERE N.F IN (1, 2, 3) 
         AND N.ID = T2.T1_ID 
       GROUP BY N.ID 
       HAVING COUNT(DISTINCT N.F) = 3 
     );