You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/models/model_kinds.md
+8-40Lines changed: 8 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -935,13 +935,7 @@ SQLMesh achieves this by adding a `valid_from` and `valid_to` column to your mod
935
935
936
936
Therefore, you can use these models to not only tell you what the latest value is for a given record but also what the values were anytime in the past. Note that maintaining this history does come at a cost of increased storage and compute and this may not be a good fit for sources that change frequently since the history could get very large.
937
937
938
-
**Note**: SCD Type 2 models support [restatements](../plans.md#restatement-plans) with specific limitations:
939
-
940
-
- **Full restatements**: The entire table will be recreated from scratch when no start date is specified
941
-
- **Partial restatements**: You can specify a start date to restate data from a certain point onwards to the latest interval. The end date will always be set to the latest interval's end date, regardless of what end date you specify
942
-
- **Partial sections**: Restatements of specific sections (discontinued ranges) of the table are not supported
943
-
944
-
Data restatement is disabled for models of this kind by default (`disable_restatement true`). To enable restatements, set `disable_restatement false` in your model configuration.
938
+
**Note**: Partial data [restatement](../plans.md#restatement-plans) is not supported for this model kind, which means that the entire table will be recreated from scratch if restated. This may lead to data loss, so data restatement is disabled for models of this kind by default.
945
939
946
940
There are two ways to tracking changes: By Time (Recommended) or By Column.
947
941
@@ -1289,11 +1283,11 @@ This is the most accurate representation of the menu based on the source data pr
1289
1283
1290
1284
### Processing Source Table with Historical Data
1291
1285
1292
-
The most common case for SCD Type 2 is creating history for a table that it doesn't have it already.
1286
+
The most common case for SCD Type 2 is creating history for a table that it doesn't have it already.
1293
1287
In the example of the restaurant menu, the menu just tells you what is offered right now, but you want to know what was offered over time.
1294
1288
In this case, the default setting of `None` for `batch_size` is the best option.
1295
1289
1296
-
Another use case though is processing a source table that already has history in it.
1290
+
Another use case though is processing a source table that already has history in it.
1297
1291
A common example of this is a "daily snapshot" table that is created by a source system that takes a snapshot of the data at the end of each day.
1298
1292
If your source table has historical records, like a "daily snapshot" table, then set `batch_size` to `1` to process each interval (each day if a `@daily` cron) in sequential order.
1299
1293
That way the historical records will be properly captured in the SCD Type 2 table.
@@ -1439,14 +1433,11 @@ GROUP BY
1439
1433
id
1440
1434
```
1441
1435
1442
-
### SCD Type 2 Restatements
1436
+
### Reset SCD Type 2 Model (clearing history)
1443
1437
1444
1438
SCD Type 2 models are designed by default to protect the data that has been captured because it is not possible to recreate the history once it has been lost.
1445
1439
However, there are cases where you may want to clear the history and start fresh.
1446
-
1447
-
#### Enabling Restatements
1448
-
1449
-
To enable restatements for an SCD Type 2 model, set `disable_restatement` to `false` in the model definition:
1440
+
For this use use case you will want to start by setting `disable_restatement` to `false` in the model definition.
1450
1441
1451
1442
```sql linenums="1" hl_lines="5"
1452
1443
MODEL (
@@ -1458,39 +1449,16 @@ MODEL (
1458
1449
);
1459
1450
```
1460
1451
1461
-
#### Full Restatements (Clearing All History)
1462
-
1463
-
To clear all history and recreate the entire table from scratch:
1452
+
Plan/apply this change to production.
1453
+
Then you will want to [restate the model](../plans.md#restatement-plans).
1464
1454
1465
1455
```bash
1466
1456
sqlmesh plan --restate-model db.menu_items
1467
1457
```
1468
1458
1469
1459
!!! warning
1470
1460
1471
-
This will remove **all** historical data on the model which in most situations cannot be recovered.
1472
-
1473
-
#### Partial Restatements (From a Specific Date)
1474
-
1475
-
You can restate data from a specific start date onwards. This will:
1476
-
- Delete all records with `valid_from >= start_date`
1477
-
- Reprocess the data from the start date to the latest interval
1478
-
1479
-
```bash
1480
-
sqlmesh plan --restate-model db.menu_items --start "2023-01-15"
1481
-
```
1482
-
1483
-
!!! note
1484
-
1485
-
If you specify an end date for SCD Type 2 restatements, it will be ignored and automatically set to the latest interval's end date.
1486
-
1487
-
```bash
1488
-
# This end date will be ignored and set to the latest interval
1489
-
sqlmesh plan --restate-model db.menu_items --start "2023-01-15" --end "2023-01-20"
1490
-
```
1491
-
1492
-
1493
-
#### Re-enabling Protection
1461
+
This will remove the historical data on the model which in most situations cannot be recovered.
1494
1462
1495
1463
Once complete you will want to remove `disable_restatement` on the model definition which will set it back to `true` and prevent accidental data loss.
0 commit comments