Skip to content

Commit df9b031

Browse files
committed
set the data recovery clones solution for demo
1 parent c4414a9 commit df9b031

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

solutions/data_recovery_clones.sql

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
-- =====================================================
2+
-- SNOWFLAKE ZERO-COPY CLONE DATA RECOVERY SOLUTION
3+
-- =====================================================
4+
-- This demo showcases Snowflake's Zero-Copy Cloning for instant
5+
-- data backup and recovery in a smart factory environment.
6+
7+
-- Target Table: FACTORY_PIPELINE_DEMO.PUBLIC.RAW_SENSOR_DATA
8+
-- Data Range: 2024-05-13 10:00:00 to 11:27:54 UTC
9+
-- =====================================================
10+
11+
-- Set context
12+
USE DATABASE FACTORY_PIPELINE_DEMO;
13+
USE SCHEMA PUBLIC;
14+
15+
-- =====================================================
16+
-- PART 1: SETUP - Create a backup clone BEFORE the demo
17+
-- =====================================================
18+
-- In a real scenario, this would be done proactively
19+
-- For demo purposes, we'll create it just before the "oops" moment
20+
21+
-- Create a backup clone of the current data
22+
CREATE OR REPLACE TABLE RAW_SENSOR_DATA_BACKUP
23+
CLONE RAW_SENSOR_DATA;
24+
25+
-- Verify the backup was created successfully
26+
SELECT 'BACKUP CREATED' as status, COUNT(*) as record_count
27+
FROM RAW_SENSOR_DATA_BACKUP;
28+
29+
-- =====================================================
30+
-- PART 2: BASELINE - Show current data state
31+
-- =====================================================
32+
33+
-- Show current data summary
34+
SELECT
35+
'BEFORE OOPS' as checkpoint,
36+
COUNT(*) as total_records,
37+
COUNT(DISTINCT machine_id) as unique_machines,
38+
MIN(timestamp) as earliest_reading,
39+
MAX(timestamp) as latest_reading,
40+
AVG(temperature) as avg_temperature,
41+
COUNT(CASE WHEN status_code = 'CRIT' THEN 1 END) as critical_alerts
42+
FROM RAW_SENSOR_DATA;
43+
44+
-- Show sample of recent data for specific machines
45+
SELECT
46+
machine_id,
47+
timestamp,
48+
temperature,
49+
vibration,
50+
pressure,
51+
status_code
52+
FROM RAW_SENSOR_DATA
53+
WHERE machine_id IN ('M001', 'M002')
54+
ORDER BY timestamp DESC
55+
LIMIT 10;
56+
57+
-- =====================================================
58+
-- PART 3: THE "OOPS" MOMENT - Simulate accidental data corruption
59+
-- =====================================================
60+
61+
-- Delete recent sensor readings for M001 and M002 (simulates accidental deletion)
62+
-- Delete data from the last hour of operations (11:00 onwards)
63+
DELETE FROM RAW_SENSOR_DATA
64+
WHERE machine_id IN ('M001', 'M002')
65+
AND timestamp >= '2024-05-13T11:00:00Z';
66+
67+
-- =====================================================
68+
-- PART 4: SHOW THE DAMAGE
69+
-- =====================================================
70+
71+
-- Let's see the impact of our "oops" moment
72+
SELECT
73+
'AFTER OOPS' as checkpoint,
74+
COUNT(*) as total_records,
75+
COUNT(DISTINCT machine_id) as unique_machines,
76+
MIN(timestamp) as earliest_reading,
77+
MAX(timestamp) as latest_reading,
78+
AVG(temperature) as avg_temperature,
79+
COUNT(CASE WHEN status_code = 'CRIT' THEN 1 END) as critical_alerts
80+
FROM RAW_SENSOR_DATA;
81+
82+
-- M001 and M002 recent data is missing
83+
SELECT
84+
'MISSING DATA CHECK' as status,
85+
machine_id,
86+
COUNT(*) as remaining_records,
87+
MAX(timestamp) as latest_timestamp
88+
FROM RAW_SENSOR_DATA
89+
WHERE machine_id IN ('M001', 'M002')
90+
GROUP BY machine_id
91+
ORDER BY machine_id;
92+
93+
-- Verify no data exists after 11:00 for M001 and M002
94+
SELECT
95+
machine_id,
96+
timestamp,
97+
temperature,
98+
status_code
99+
FROM RAW_SENSOR_DATA
100+
WHERE machine_id IN ('M001', 'M002')
101+
AND timestamp >= '2024-05-13T11:00:00Z'
102+
ORDER BY timestamp DESC;
103+
104+
-- =====================================================
105+
-- PART 5: INSTANT RECOVERY - ZERO-COPY CLONE
106+
-- =====================================================
107+
108+
-- Show what data exists in our backup clone
109+
SELECT
110+
'BACKUP CLONE DATA' as source,
111+
COUNT(*) as total_records,
112+
COUNT(DISTINCT machine_id) as unique_machines,
113+
COUNT(CASE WHEN machine_id IN ('M001', 'M002') THEN 1 END) as m001_m002_records
114+
FROM RAW_SENSOR_DATA_BACKUP;
115+
116+
-- Show the missing data still exists in our backup
117+
SELECT
118+
'RECOVERED FROM BACKUP' as source,
119+
machine_id,
120+
timestamp,
121+
temperature,
122+
vibration,
123+
pressure,
124+
status_code
125+
FROM RAW_SENSOR_DATA_BACKUP
126+
WHERE machine_id IN ('M001', 'M002')
127+
AND timestamp >= '2024-05-13T11:00:00Z'
128+
ORDER BY timestamp DESC
129+
LIMIT 10;
130+
131+
-- INSTANT RECOVERY using Zero-Copy Clone
132+
-- Replace the damaged table with our backup clone
133+
CREATE OR REPLACE TABLE RAW_SENSOR_DATA
134+
CLONE RAW_SENSOR_DATA_BACKUP;
135+
136+
-- Verify recovery
137+
SELECT
138+
'AFTER CLONE RECOVERY' as checkpoint,
139+
COUNT(*) as total_records,
140+
COUNT(DISTINCT machine_id) as unique_machines,
141+
COUNT(CASE WHEN machine_id IN ('M001', 'M002') THEN 1 END) as m001_m002_records
142+
FROM RAW_SENSOR_DATA;
143+
144+
-- Confirm M001 and M002 data is restored
145+
SELECT
146+
'RESTORED DATA CHECK' as status,
147+
machine_id,
148+
COUNT(*) as total_records,
149+
MAX(timestamp) as latest_timestamp
150+
FROM RAW_SENSOR_DATA
151+
WHERE machine_id IN ('M001', 'M002')
152+
GROUP BY machine_id
153+
ORDER BY machine_id;
154+
155+
-- =====================================================
156+
-- PART 6: CLEANUP AND FINAL VERIFICATION
157+
-- =====================================================
158+
159+
-- Final data quality check
160+
SELECT
161+
'FINAL STATE' as checkpoint,
162+
COUNT(*) as total_records,
163+
COUNT(DISTINCT machine_id) as unique_machines,
164+
MIN(timestamp) as earliest_reading,
165+
MAX(timestamp) as latest_reading,
166+
AVG(temperature) as avg_temperature,
167+
COUNT(CASE WHEN status_code = 'CRIT' THEN 1 END) as critical_alerts,
168+
COUNT(CASE WHEN temperature > 200 THEN 1 END) as corrupted_temps
169+
FROM RAW_SENSOR_DATA;
170+
171+
-- Show that all machines have data across the full time range
172+
SELECT
173+
machine_id,
174+
COUNT(*) as record_count,
175+
MIN(timestamp) as earliest_reading,
176+
MAX(timestamp) as latest_reading,
177+
AVG(temperature) as avg_temp
178+
FROM RAW_SENSOR_DATA
179+
GROUP BY machine_id
180+
ORDER BY machine_id;
181+
182+
-- Optional: Clean up demo artifacts
183+
-- DROP TABLE IF EXISTS RAW_SENSOR_DATA_BACKUP;
184+
-- DROP TABLE IF EXISTS RAW_SENSOR_DATA_BACKUP_20250115;

0 commit comments

Comments
 (0)