MySQL Update查询太慢了

时间:2013-08-07 11:47:58

标签: mysql performance

我有一个更新查询,执行时间超过2小时。查询根据某些情况同时更新三个表。根据业务需求,更新查询不复杂。如何优化此查询: 查询如下:

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 个答案:

答案 0 :(得分:1)

我会将业务逻辑移出sql语句。 在代码中执行检查,然后执行“UPDATE atmissuerdata SET reconstats ='Y'WOERE 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并在其上加上索引。