2013-08-07 177 views
-2

我有一個更新查詢需要超過2小時才能執行。查詢根據某些情況同時更新三個表。根據業務需求更新查詢並不複雜。如何優化這個查詢:查詢 是如下:MySQL更新查詢太慢

UPDATE 
     atmissuerdata a1 
inner join 
     cbsissuerdata c1 ON ROW(a1.transactionnumber,a1.cardnumber) =ROW(c1.approvalnumber,c1.cardnumber) 
INNER JOIN 
     switchissuerdata s1 ON ROW(a1.transactionnumber,a1.cardnumber) =ROW(s1.approvalnumber,s1.cardnumber) 
INNER JOIN 
     atmlevel1 al1 ON al1.id=a1.id INNER JOIN cbslevel1 cl1 ON cl1.id=c1.id 
INNER JOIN 
     switchlevel1 sl1 ON sl1.id=s1.id 
SET 
     a1.reconstatus = 
         CASE 
       WHEN ( ROW(a1.transactionnumber,a1.cardnumber) = ROW(c1.approvalnumber,c1.cardnumber) AND 
         ROW(a1.transactionnumber,a1.cardnumber) = ROW(s1.approvalnumber,s1.cardnumber)) AND 
       ( a1.TransactionAmount = c1.TransactionAmountDr AND a1.TransactionAmount = s1.TransactionAmount1 ) 
       THEN 'Y' 
       WHEN 
         (ROW(a1.transactionnumber,a1.cardnumber) = ROW(c1.approvalnumber,c1.cardnumber) 
         AND ROW(a1.transactionnumber,a1.cardnumber) = ROW(s1.approvalnumber,s1.cardnumber)) 
         AND ( a1.TransactionAmount <> c1.TransactionAmountDr OR a1.TransactionAmount <> s1.TransactionAmount1) 
       THEN 'D' 
       ELSE 
         a1.reconstatus END, 
     c1.reconstatus= 
       CASE WHEN (ROW(a1.transactionnumber,a1.cardnumber) = ROW(c1.approvalnumber,c1.cardnumber) 
         AND ROW(a1.transactionnumber,a1.cardnumber) = ROW(s1.approvalnumber,s1.cardnumber)) 
         AND (a1.TransactionAmount = c1.TransactionAmountDr AND a1.TransactionAmount = s1.TransactionAmount1) 
       THEN 'Y' 
       WHEN ( ROW(a1.transactionnumber,a1.cardnumber) = ROW(c1.approvalnumber,c1.cardnumber) AND 
         ROW(a1.transactionnumber,a1.cardnumber) = ROW(s1.approvalnumber,s1.cardnumber)) AND 
       ( a1.TransactionAmount <> c1.TransactionAmountDr OR a1.TransactionAmount <> s1.TransactionAmount1 ) 
       THEN 'D' 
       ELSE 
         c1.reconstatus END , 
     s1.reconstatus= 
       CASE WHEN (ROW(a1.transactionnumber,a1.cardnumber) = ROW(c1.approvalnumber,c1.cardnumber) 
       AND ROW(a1.transactionnumber,a1.cardnumber) = ROW(s1.approvalnumber,s1.cardnumber)) 
       AND ( a1.TransactionAmount = c1.TransactionAmountDr AND a1.TransactionAmount = s1.TransactionAmount1) 
       THEN 'Y' 
       WHEN (ROW(a1.transactionnumber,a1.cardnumber) = ROW(c1.approvalnumber,c1.cardnumber) 
         AND ROW(a1.transactionnumber,a1.cardnumber) = ROW(s1.approvalnumber,s1.cardnumber)) 
         AND ( a1.TransactionAmount <> c1.TransactionAmountDr OR a1.TransactionAmount <> s1.TransactionAmount1) 
       THEN 'D' 
       ELSE s1.reconstatus END; 

請幫我..

回答

1

我會移動的業務邏輯出的SQL語句。 在代碼中執行檢查,然後執行「UPDATE atmissuerdata SET reconstats ='Y'WHERE id IN(1,2,5,4,...)」,「UPDATE atmissuerdata SET reconstats ='N'WHERE id IN(1,2,5,4,...)「

另外你是否正確使用索引? 在我腦海中,「內連接cbsissuerdata c1 ON ROW(a1.transactionnumber,a1.cardnumber)= ROW(c1.approvalnumber,c1.cardnumber)」可能很慢。嘗試在連接上使用ID並在其上有一個索引。

+0

我不能使用三次更新,因爲所有狀態都需要同時更新。我們只選擇N個數據,如果下一次將一個表中的數據更新爲Y,則相應的N個數據將不會被使用。所以,查詢將不得不使用相同的。 –