-2

我有一个更新查询,执行时间超过 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;

请帮帮我..

4

1 回答 1

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,...)"

你也正确使用索引吗?在我看来,“inner join cbsissuerdata c1 ON ROW(a1.transactionnumber,a1.cardnumber) =ROW(c1.approvalnumber,c1.cardnumber)”可能慢得要命。尝试在连接上使用 id 并在其上设置索引。

于 2013-08-07T11:56:49.990 回答