如果所有更新都具有相同的形状,您可以将它们加载到临时表中并使用它来批量bag.pand
更新表,如下所示:
CREATE TABLE bag.mutaties
( zkey varchar NOT NULL PRIMARY KEY
, zdate timestamp NOT NULL
);
COPY bag.mutaties(zkey,zdate) FROM 'the_big_file';
update bag.pand dst
FROM bag.mutaties src
set mutatiedatum = src.zdate
WHERE dst.key = src.zkey
;
以上假设日期采用 ISO 'yyyy-mm-dd hh:mm:ss' 格式。
如果您无法创建具有此日期格式的数据文件,您可以将现有的“dd-mm-yyyy”日期读入字符串并在更新语句中将它们转换(类似于您的一次一行更新:
CREATE TABLE bag.mutaties
( zkey varchar NOT NULL PRIMARY KEY
, dutchdate varchar NOT NULL
);
COPY bag.mutaties(zkey,dutchdate) FROM 'the_big_file';
update bag.pand dst
FROM bag.mutaties src
set mutatiedatum = to_date(src.dutchdate, 'DD-MM-YYYY HH:MI:SS')
WHERE dst.key = src.key
;
额外奖励更新:
-- This will read in the existing SQL-SCRIPT (!!)
-- and transform it into a table with {key,datetimestamp}
CREATE TABLE bag.tekstmutaties
( id SERIAL NOT NULL PRIMARY KEY
, typ INTEGER NOT NULL DEFAULT 0
, num INTEGER NOT NULL DEFAULT 0
, tekst varchar
);
-- Read in the existing script file
-- (this needs to be in /tmp/ to avoid permission problems)
COPY bag.tekstmutaties(tekst) FROM '/tmp/bagmut.txt';
-- Remove bagger
delete from bag.tekstmutaties
where LEFT(tekst,1) NOT IN ( 's' , 'w')
;
-- Extract the timestamp
UPDATE bag.tekstmutaties
SET typ = 1
, tekst = regexp_replace( tekst, E' *set.*to_date..', '', 'ig')
WHERE LEFT(tekst,1) IN ( 's')
;
UPDATE bag.tekstmutaties
SET tekst = LEFT( tekst , 19)
WHERE typ = 1
;
-- Extract the key
UPDATE bag.tekstmutaties
SET typ = 2
, tekst = regexp_replace( tekst, E' *where key..', '', 'ig')
WHERE LEFT(tekst,1) IN ( 'w')
;
UPDATE bag.tekstmutaties
SET tekst = regexp_replace( tekst , '[^0-9A-Z_]' , '' , 'g' )
WHERE typ = 2
;
-- number the records
UPDATE bag.tekstmutaties
SET num = id
WHERE TYP=1
;
-- number the records
UPDATE bag.tekstmutaties uu
SET num = src.val
FROM (
SELECT id, lag(id) OVER (ORDER BY id) AS val
FROM bag.tekstmutaties
) src
WHERE uu.TYP=2
AND src.id = uu.id
;
SELECT * FROM bag.tekstmutaties ORDER BY id;
-- The final table with the {key,timestamp} pairs
CREATE TABLE bag.mutaties
( zkey varchar NOT NULL PRIMARY KEY
, zdate timestamp NOT NULL
);
-- Fill it with self-join of teksttable
INSERT INTO bag.mutaties (zkey, zdate)
SELECT k.tekst AS zkey
, to_date(d.tekst, E'DD-MM-YYYY HH:MI:SS' ) AS zdate
FROM bag.tekstmutaties k
JOIN bag.tekstmutaties d ON k.num = d.num
WHERE k.typ=2
AND d.typ = 1
;
SELECT * FROM bag.mutaties;
-- now **after verification** you can use the mutaties-table
-- to batch-update the bag.pand table
EXPLAIN ANALYZE
update bag.pand dst
FROM mutaties src
set mutatiedatum = src.zdate
WHERE dst.key = src.key
;