Read a snapshot before restoring a database¶
Use an isolated account: an account snapshot can contain more than this test database. Read the old rows without changing live data, then restore only the test database. This short verification complements the existing rollback tutorial.
CREATE DATABASE cookbook_snapshot;
CREATE TABLE cookbook_snapshot.orders(id INT PRIMARY KEY);
INSERT INTO cookbook_snapshot.orders VALUES(1),(2);
SELECT SLEEP(1);
CREATE SNAPSHOT cookbook_snapshot_point FOR ACCOUNT;
DELETE FROM cookbook_snapshot.orders WHERE id=2;
SELECT COUNT(*) AS live_rows FROM cookbook_snapshot.orders;
SELECT COUNT(*) AS snapshot_rows
FROM cookbook_snapshot.orders {snapshot='cookbook_snapshot_point'};
RESTORE DATABASE cookbook_snapshot{snapshot='cookbook_snapshot_point'};
SELECT COUNT(*) AS restored_rows FROM cookbook_snapshot.orders;
DROP SNAPSHOT cookbook_snapshot_point;
DROP DATABASE cookbook_snapshot;
Before restore, the live count is 1 and the snapshot count is 2. After restore, the live count is 2. RESTORE DATABASE replaces current test-database state; do not use a production database for this exercise. See the v4.2.4 source test.