Only Table Swap
Imagine you have a scenario where you have a large table which stores referential information. Imagine that this table stores millions of rows of read-only data, and needs to be updated every so often (maybe every day).
First approach: Every day, run a job that truncates the existing table and then insert the new rows.
Pros:
Cons:
Second approach: Every day, run a job that creates a different table, inserts the new rows into that table, and then swaps the table.
Pros:
Cons:
To do the swap, we will do the following.
START TRANSACTION;
CREATE TABLE ${new-table} LIKE ${table-to-replace};
INSERT INTO ${table-to-replace} (${columns}) VALUES (${rows});
RENAME TABLE ${table-to-replace} TO ${table-to-drop}, ${new-table} TO ${table-to-replace};
DROP TABLE ${table-to-replace};
COMMIT;
If any of the above fails, nothing will happen; then we can figure out why the operation failed.