0

I have a situation where I am doing a data fix from back up.

Table MAIN TABLE (PrimaryKey, Value) and Table BACKUP(PRIMARYKEY, Value).

I want to find all the records in MAIN Table with value=0 , then go fetch the value for the same primary key from table BACKUP and update the MAIN Table.

  1. There are 20 millions records with value=0
  2. Updates and fetch are both done using primary key

Questions

  1. Stored procedure? Script?

  2. Fetch and update are done on the same table? Any concerns?

  3. How much time do you think it will take- ball park figure. how to test?

Solution I was thinking :

Open a cursor on Table Main with my condition(value=0) and then go fetch value from BACKUP and then update. Commit every 10K updates in a loop

Any thoughts?

4

2 回答 2

1

你可以试试 Oracle 的MERGE.

确保在将查询应用于主表之前在测试表中进行测试。

MERGE INTO main_table m
USING backup_table b
ON (m.primary_key = b.primary_key)
WHEN MATCHED THEN
    UPDATE SET m.value = b.value 
    WHERE m.value = 0;
于 2019-12-12T15:49:41.497 回答
-1
UPDATE MAIN_TABLE 
SET main.value=back.value
FROM MAIN_TABLE as main
JOIN BACKUP_TABLE as back ON main.pk=back.pk
WHERE main.value=0

这是我发现如何做到这一点的地方: https ://chartio.com/resources/tutorials/how-to-update-from-select-in-sql-server/

于 2019-12-12T15:52:41.490 回答