The below update statement is taking lot of time. Can you please help me to tune this query? I will prefer re-structuring the query rather than using any HINT. My only constraint is, I can’t use index since there is no index in the table
UPDATE table_main a
SET column_to_update =
(SELECT DISTINCT b.column_from_update
FROM table1 b
WHERE b.key1 = a.key1
AND b.type1 = a.type1);
Please find the plan for the query:
Plan
UPDATE STATEMENT ALL_ROWSCost: 386 Bytes: 1,369,332 Cardinality: 44,172
4 UPDATE Table_main
1 TABLE ACCESS FULL TABLE table_main Cost: 386 Bytes: 1,369,332 Cardinality: 44,172
3 HASH UNIQUE Cost: 283 Bytes: 28 Cardinality: 1
2 TABLE ACCESS FULL TABLE table1 Cost: 282 Bytes: 28 Cardinality: 1
Table_main volume :44K
Table1 volume : 53k
Are you updating ALL rows ? Because that is what is currently happening
Maybe try this for starters
UPDATE table_main a
SET column_to_update =
(SELECT DISTINCT b.column_from_update
FROM table1 b
WHERE b.key1 = a.key1
AND b.type1 = a.type1)
where (a.key1,a.type1) in
( select key1,type1 from table1 )
The DISTINCT is interesting because if you were to get more than 1 distinct value, the update crashes anyway. Which means they must all be the same. You could explore the inner query being:
SELECT b.column_from_update
FROM table1 b
WHERE b.key1 = a.key1
AND b.type1 = a.type1
and rownum = 1
but you can only do this if you can guarantee the data is valid (ie, you would *not* get an error if there were indeed more than 1 distinct value).
The other thing to experiment with is a join-update, ie
UPDATE
( select a.key1, a.type1, a.column_to_update old_val, b.column_from_update new_val
from table_main a,
(SELECT DISTINCT key1, type1, column_from_update
FROM table1
) b
WHERE b.key1 = a.key1
AND b.type1 = a.type1
)
SET old_val = new_val