我有一個查詢,如下優化甲骨文更新選擇
UPDATE CONVERTED T1
SET PARENTID = (SELECT MAX(ID) FROM
CONVERTED T2 WHERE T2.ID < T1.ID
AND T1.PREVOBJNUM = T2.OBID)
它需要幾個小時來運行他們的是100萬條記錄,它是不必爲每一行執行子查詢。有沒有一種方法來優化查詢運行速度更快?
編輯:這裏是xplan
Plan hash value: 3177327108
----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1053K| 31M| 576M (1)|802:56:03 |
| 1 | UPDATE | CONVERTED | | | | |
| 2 | TABLE ACCESS FULL | CONVERTED | 1053K| 31M| 84276 (1)| 00:07:03 |
| 3 | SORT AGGREGATE | | 1 | 17 | | |
|* 4 | TABLE ACCESS BY INDEX ROWID| CONVERTED | 1 | 17 | 547 (1)| 00:00:03 |
|* 5 | INDEX RANGE SCAN | CONVERTED_PK | 9478 | | 22 (0)| 00:00:01 |
----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("T2"."OBID"=:B1)
5 - access("T2"."ID"<:B1)
,這裏是表DLL
--------------------------------------------------------
-- DDL for Table CONVERTED
--------------------------------------------------------
CREATE TABLE "AMS"."CONVERTED"
( "ID" NUMBER(10,0),
"L1" VARCHAR2(255 CHAR),
"L1_DESC" VARCHAR2(255 CHAR),
"L2" VARCHAR2(255 CHAR),
"L2_DESC" VARCHAR2(255 CHAR),
"L3" VARCHAR2(255 CHAR),
"L3_DESC" VARCHAR2(255 CHAR),
"L4" VARCHAR2(255 CHAR),
"L4_DESC" VARCHAR2(255 CHAR),
"L5" VARCHAR2(255 CHAR),
"L5_DESC" VARCHAR2(255 CHAR),
"L6" VARCHAR2(255 CHAR),
"L6_DESC" VARCHAR2(255 CHAR),
"L7" VARCHAR2(255 CHAR),
"L7_DESC" VARCHAR2(255 CHAR),
"L8" VARCHAR2(255 CHAR),
"L8_DESC" VARCHAR2(255 CHAR),
"L9" VARCHAR2(255 CHAR),
"L9_DESC" VARCHAR2(255 CHAR),
"L10" VARCHAR2(255 CHAR),
"L10_DESC" VARCHAR2(255 CHAR),
"L11" VARCHAR2(255 CHAR),
"L11_DESC" VARCHAR2(255 CHAR),
"L12" VARCHAR2(255 CHAR),
"L12_DESC" VARCHAR2(255 CHAR),
"L13" VARCHAR2(255 CHAR),
"L13_DESC" VARCHAR2(255 CHAR),
"L14" VARCHAR2(255 CHAR),
"L14_DESC" VARCHAR2(255 CHAR),
"L15" VARCHAR2(255 CHAR),
"L15_DESC" VARCHAR2(255 CHAR),
"OBID" VARCHAR2(255 CHAR),
"OBDESC" VARCHAR2(255 CHAR),
"MATTYPE" VARCHAR2(255 CHAR),
"NUMOF" VARCHAR2(255 CHAR),
"UOM" VARCHAR2(255 CHAR),
"OBTYPE" VARCHAR2(255 CHAR),
"PREVOBJNUM" VARCHAR2(255 CHAR),
"PARENTID" NUMBER(10,0),
"POSITION" NUMBER(10,0),
"LEVEL_" NUMBER(10,0)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "AMS_DATA" ;
--------------------------------------------------------
-- DDL for Index CONVERTED_PK
--------------------------------------------------------
CREATE UNIQUE INDEX "AMS"."CONVERTED_PK" ON "AMS"."CONVERTED" ("ID")
PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "AMS_DATA" ;
--------------------------------------------------------
-- Constraints for Table CONVERTED
--------------------------------------------------------
ALTER TABLE "AMS"."CONVERTED" ADD CONSTRAINT "CONVERTED_PK" PRIMARY KEY ("ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "AMS_DATA" ENABLE;
alter table "AMS"."CONVERTED" modify ("ID" not null enable);
這裏代替
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1448708152
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1053K| 31M| 4296K (25)| 05:59:18 |
| 1 | UPDATE | CONVERTED | | | | |
| 2 | TABLE ACCESS FULL | CONVERTED | 1053K| 31M| 84276 (1)| 00:07:03 |
| 3 | SORT AGGREGATE | | 1 | 17 | | |
| 4 | FIRST ROW | | 1 | 17 | 3 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN (MIN/MAX)| INDEX1 | 1 | 17 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("T2"."OBID"=:B1 AND "T2"."ID"<:B2)
17 rows selected
讓我們來看看執行計劃從DBMS_XPLAN.DISPLAY請和DDL的表和翻新 –
信息添加任何索引。歡呼 – totalitarian
OBID有多少個唯一值? –