oracle 如何优化update语句的执行时间?

6yoyoihd  于 2023-05-22  发布在  Oracle
关注(0)|答案(2)|浏览(141)

我正在尝试更新一个包含137459条记录的表。我的查询正在使用Cost运行:4,但它不执行。
我的执行查询如下:

UPDATE TF_IMPORT_BILL_ISSUE N
   SET N.OPERATION_ID =
          (SELECT TRANSACTION_ID
             FROM (  SELECT /*+ INDEX(BT TF_IMP_BILL_TXN_BILLID) */
                           MAX (BT.TRANSACTION_ID) TRANSACTION_ID, BI.BILL_ID
                       FROM TF_IMP_BILL_TRANSACTIONS BT,
                            TF_IMPORT_BILL_ISSUE BI,
                            CONV_BRANCH_INFO BRANCH
                      WHERE     BRANCH.IS_FOR_MIGRATION = 1
                            AND BI.OPERATION_ID IS NULL
                            AND BRANCH.BRANCH_ID = BI.OWNER_BRANCH_ID
                            AND BT.BILL_ID = BI.BILL_ID
                   GROUP BY BI.BILL_ID) v
            WHERE v.bill_id = n.bill_id)
 WHERE N.BILL_ID IN (SELECT bill_id
                       FROM (  SELECT /*+ INDEX(BI) */
                                     MAX (BT.TRANSACTION_ID) TRANSACTION_ID,
                                      BI.BILL_ID
                                 FROM TF_IMP_BILL_TRANSACTIONS BT,
                                      TF_IMPORT_BILL_ISSUE BI,
                                      CONV_BRANCH_INFO BRANCH
                                WHERE     BRANCH.IS_FOR_MIGRATION = 1
                                      AND BI.OPERATION_ID IS NULL
                                      AND BRANCH.BRANCH_ID = BI.OWNER_BRANCH_ID
                                      AND BT.BILL_ID = BI.BILL_ID
                             GROUP BY BI.BILL_ID) V);

执行计划如下:

------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name                      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------------------------
|   0 | UPDATE STATEMENT                           |                           |     1 |    52 |     4  (25)| 00:00:01 |
|   1 |  UPDATE                                    | TF_IMPORT_BILL_ISSUE      |       |       |            |          |
|   2 |   NESTED LOOPS                             |                           |     1 |    52 |     1   (0)| 00:00:01 |
|   3 |    NESTED LOOPS                            |                           |     1 |    52 |     1   (0)| 00:00:01 |
|   4 |     VIEW                                   | VW_NSO_1                  |     1 |    13 |     0   (0)| 00:00:01 |
|   5 |      SORT GROUP BY                         |                           |     1 |    58 |     0   (0)| 00:00:01 |
|   6 |       NESTED LOOPS SEMI                    |                           |     1 |    58 |     0   (0)| 00:00:01 |
|   7 |        NESTED LOOPS                        |                           |     1 |    52 |     0   (0)| 00:00:01 |
|   8 |         INDEX FULL SCAN                    | TF_IMP_BILL_TXN_BILLID    |     1 |    13 |     0   (0)| 00:00:01 |
|*  9 |         TABLE ACCESS BY INDEX ROWID BATCHED| TF_IMPORT_BILL_ISSUE      |     1 |    39 |     0   (0)| 00:00:01 |
|  10 |          INDEX FULL SCAN                   | TF_IMPORT_BILL_ISSUE_ONBR |     1 |       |     0   (0)| 00:00:01 |
|* 11 |        TABLE ACCESS BY INDEX ROWID BATCHED | CONV_BRANCH_INFO          |     2 |    12 |     0   (0)| 00:00:01 |
|* 12 |         INDEX RANGE SCAN                   | CBI_BR_ID                 |     1 |       |     0   (0)| 00:00:01 |
|* 13 |     INDEX UNIQUE SCAN                      | SYS_C0016767              |     1 |       |     0   (0)| 00:00:01 |
|  14 |    TABLE ACCESS BY INDEX ROWID             | TF_IMPORT_BILL_ISSUE      |     1 |    39 |     1   (0)| 00:00:01 |
|  15 |   VIEW                                     |                           |     1 |    26 |     1   (0)| 00:00:01 |
|  16 |    SORT GROUP BY                           |                           |     1 |    71 |     1   (0)| 00:00:01 |
|  17 |     NESTED LOOPS                           |                           |     1 |    71 |     1   (0)| 00:00:01 |
|  18 |      NESTED LOOPS                          |                           |     1 |    45 |     1   (0)| 00:00:01 |
|* 19 |       TABLE ACCESS BY INDEX ROWID          | TF_IMPORT_BILL_ISSUE      |     1 |    39 |     1   (0)| 00:00:01 |
|* 20 |        INDEX UNIQUE SCAN                   | SYS_C0016767              |     1 |       |     1   (0)| 00:00:01 |
|* 21 |       TABLE ACCESS BY INDEX ROWID BATCHED  | CONV_BRANCH_INFO          |     1 |     6 |     0   (0)| 00:00:01 |
|* 22 |        INDEX RANGE SCAN                    | CBI_BR_ID                 |     1 |       |     0   (0)| 00:00:01 |
|  23 |      TABLE ACCESS BY INDEX ROWID BATCHED   | TF_IMP_BILL_TRANSACTIONS  |     1 |    26 |     0   (0)| 00:00:01 |
|* 24 |       INDEX RANGE SCAN                     | TF_IMP_BILL_TXN_BILLID    |     1 |       |     0   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   9 - filter("BI"."OPERATION_ID" IS NULL AND "BT"."BILL_ID"="BI"."BILL_ID")
  11 - filter("BRANCH"."IS_FOR_MIGRATION"=1)
  12 - access("BRANCH"."BRANCH_ID"="BI"."OWNER_BRANCH_ID")
  13 - access("N"."BILL_ID"="BILL_ID")
  19 - filter("BI"."OPERATION_ID" IS NULL)
  20 - access("BI"."BILL_ID"=:B1)
  21 - filter("BRANCH"."IS_FOR_MIGRATION"=1)
  22 - access("BRANCH"."BRANCH_ID"="BI"."OWNER_BRANCH_ID")
  24 - access("BT"."BILL_ID"=:B1)

请帮助我如何优化此查询执行。

**注意:**查询的引用表记录为TF_IMPORT_BILL_ISSUE(137,459)、TF_IMP_BILL_TRANSACTIONS(239,712)和CONV_分支_INFO(2)。

41zrol4v

41zrol4v1#

merge怎么样?

MERGE INTO tf_import_bill_issue n
     USING (  SELECT bi.bill_id,
                     MAX (bt.transaction_id) transaction_id
                FROM tf_imp_bill_transactions bt
                     JOIN tf_import_bill_issue bi ON bt.bill_id = bi.bill_id
                     JOIN conv_branch_info branch ON branch.branch_id = bi.owner_branch_id
               WHERE     branch.is_for_migration = 1
                     AND bi.operation_id IS NULL
            GROUP BY bi.bill_id) x
        ON (x.bill_id = n.bill_id)
WHEN MATCHED
THEN
   UPDATE SET n.operation_id = x.transaction_id;
fzsnzjdm

fzsnzjdm2#

你可以使用MERGE语句:

MERGE INTO TF_IMPORT_BILL_ISSUE N
USING (
  SELECT /*+ INDEX(BT TF_IMP_BILL_TXN_BILLID) */
         MAX (BT.TRANSACTION_ID) TRANSACTION_ID,
         BI.BILL_ID
  FROM   TF_IMP_BILL_TRANSACTIONS BT
         INNER JOIN TF_IMPORT_BILL_ISSUE BI
         ON (BT.BILL_ID = BI.BILL_ID)
         INNER JOIN CONV_BRANCH_INFO BRANCH
         ON (BRANCH.BRANCH_ID = BI.OWNER_BRANCH_ID)
  WHERE  BRANCH.IS_FOR_MIGRATION = 1
  AND    BI.OPERATION_ID IS NULL
  GROUP BY BI.BILL_ID
) v
ON v.bill_id = n.bill_id
WHEN MATCHED THEN
  UPDATE
  SET N.OPERATION_ID = v.TRANSACTION_ID;

相关问题