I want to have a transaction, in typescript, to refresh some data in a table. To do so the following steps need to apply:
Don't do it that way. Instead:
CREATE TABLE new LIKE real;
populate `new`
RENAME TABLE real TO old, new TO real;
DROP TABLE old;
If the CREATE
or populate
fail, then abandon the task; no harm done. (Well, need to DROP TABLE real
.)
The RENAME TABLE
is very fast, and atomic, and unlikely to fail.
This approach has the advantage that, to other queries, the real
table is available and fully populated the entire time.