关联子查询的更新语句太慢

时间:2018-06-25 06:51:19

标签: sql oracle performance correlated-subquery

我正在使用下面的update语句,它太慢了(大约需要10-12秒)

UPDATE temp_final t
   SET name = (
        SELECT DISTINCT(nm.name)
          FROM table_name nm,
               temp_final fn
         WHERE nm.id       = fn.senid
           AND fn.senid    = t.senid
           AND fn.rowgroup = t.rowgroup
           AND fn.mid      = t.mid
           AND fn.sid      = t.sid
           AND fn.pid      = t.pid
           AND fn.reid     = t.reid
           AND fn.retype   = t.retype
        )
 WHERE t.reid   = n_reid
   AND t.retype = n_retype
   AND t.sid    = c_sid
   AND t.pid    = n_pid;    

temp_final表的列未编制索引,因为该表包含大量数据更新。我想我无法将相关子查询转换为联接,因为它使用的是不同子句。此查询的计划给出了以下结果:

OPERATION                              OBJECT        CARDINALITY     COST
- UPDATE STATEMENT                                       1925        337046      
  - UPDATE                             temp_final 
    -TABLE ACCESS(FULL)                temp_final        1925        171
      - Filter Predicates
        - AND
          -T.SID='123'
          -T.PID=21
          -T.REID=9
          -T.RETYPE=1
    -SORT(UNIQUE)                                         1          175
      - NESTED LOOPS                                      1          174 
         -TABLE ACCESS(BY INDEX ROWID)  TABLE_NAME        1           2
           -INDEX(UNIQUE SCAN)          NAME_PK           1           1  
             -ACCESS PREDICATES
               NM.ID=:B1
         -TABLE ACCESS(FULL)            temp_final        1           172
           - Filter Predicates
             - AND
               -fn.mid=:B1
               -fn.senid=:B2
               -fn.sid =:B3
               -fn.rowgroup =:B4
               -fn.pid=:B5
               -fn.reid=:B6
               -fn.retype=:B7
               -nm.id = fn.senid

表temp_final包含约8k条记录,表table_name包含约2条lac记录

请让我知道如何改善此查询的性能?

致谢

1 个答案:

答案 0 :(得分:2)

我不完全了解您的自我加入。这句话还不够吗?

UPDATE temp_final t
   SET name = (
        SELECT nm.name
          FROM table_name nm
         WHERE nm.id       = t.senid
        )
 WHERE t.reid   = n_reid
   AND t.retype = n_retype
   AND t.sid    = c_sid
   AND t.pid    = n_pid;