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: src/current/v25.2/create-table-as.md
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,8 @@ When the results of a query are reused multiple times within a larger query, a v
22
22
23
23
A view is also advisable when the results must be up-to-date; a view always retrieves the current data from the tables that the view query mentions.
24
24
25
+
{% include_cached new-in.html version="v25.2" %} Use `CREATE TABLE t AS ...` with the [`AS OF SYSTEM TIME` clause](#populate-create-table-as-with-historical-data-using-as-of-system-time) to leverage [historical reads]({% link {{ page.version.version }}/as-of-system-time.md %}) to reduce contention and improve performance.
26
+
25
27
{{site.data.alerts.callout_info}}
26
28
The default rules for [column families]({% link {{ page.version.version }}/column-families.md %}) apply.
27
29
{{site.data.alerts.end}}
@@ -287,6 +289,34 @@ You can define the [column families]({% link {{ page.version.version }}/column-f
287
289
(1 row)
288
290
~~~
289
291
292
+
### Populate `CREATE TABLE AS` with historical data using `AS OF SYSTEM TIME`
293
+
294
+
{% include_cached new-in.html version="v25.2" %} CockroachDB supports creating a table using historical data using the [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}) clause. You can use this to create a new table based on the state of an existing table as of a specific [timestamp]({% link {{ page.version.version }}/timestamp.md %}) in the past. This is useful for:
295
+
296
+
- Generating static datasets for reporting or analytical workloads without increasing contention on production tables or otherwise impacting performance.
297
+
- Analyzing data changes over time.
298
+
- Preserving historical data for regulatory or investigative purposes.
299
+
300
+
{{site.data.alerts.callout_info}}
301
+
The timestamp must be within the [garbage collection (GC) window]({% link {{ page.version.version }}/architecture/storage-layer.md %}#garbage-collection) of the source table for the data to be available.
302
+
{{site.data.alerts.end}}
303
+
304
+
The syntax for creating a new table from a historical table at a given timestamp is as follows; this example creates a new table at the most recent timestamp that can perform a [follower read]({% link {{ page.version.version }}/follower-reads.md %}).
305
+
306
+
{% include_cached copy-clipboard.html %}
307
+
~~~sql
308
+
CREATETABLEanalysis_vehicle_location_histories
309
+
ASSELECT*FROM vehicle_location_histories
310
+
AS OF SYSTEM TIME follower_read_timestamp();
311
+
~~~
312
+
313
+
~~~
314
+
NOTICE: CREATE TABLE ... AS does not copy over indexes, default expressions, or constraints; the new table has a hidden rowid primary key column
315
+
CREATE TABLE AS
316
+
~~~
317
+
318
+
For more information about historical reads at a given timestamp, refer to [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}).
319
+
290
320
## See also
291
321
292
322
-[Selection Queries]({% link {{ page.version.version }}/selection-queries.md %})
@@ -299,3 +329,6 @@ You can define the [column families]({% link {{ page.version.version }}/column-f
299
329
-[`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key)
300
330
-[`ALTER TABLE`]({% link {{ page.version.version }}/alter-table.md %})
301
331
-[Online Schema Changes]({% link {{ page.version.version }}/online-schema-changes.md %})
332
+
-[`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %})
333
+
-[Follower Reads]({% link {{ page.version.version }}/follower-reads.md %})
334
+
-[Disaster Recovery Planning]({% link {{ page.version.version }}/disaster-recovery-planning.md %})
Copy file name to clipboardExpand all lines: src/current/v25.2/disaster-recovery-planning.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -304,6 +304,8 @@ To give yourself more time to recover and clean up the corrupted data, put your
304
304
305
305
If you are within the [garbage collection window]({% link {{ page.version.version }}/configure-replication-zones.md %}#gc-ttlseconds), run [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}) queries and use [`CREATE TABLE AS … SELECT * FROM`]({% link {{ page.version.version }}/create-table-as.md %}) to create comparison data and run differentials to find the offending rows to fix.
306
306
307
+
{% include_cached new-in.html version="v25.2" %} Alternatively, you can use [`CREATE TABLE AS ... AS OF SYSTEM TIME`]({% link {{ page.version.version }}/create-table-as.md %}#populate-create-table-as-with-historical-data-using-as-of-system-time) to generate a copy of the data as of a specific [timestamp]({% link {{ page.version.version }}/timestamp.md %}). This also requires being within the garbage collection window.
308
+
307
309
If you are outside of the garbage collection window, you will need to use a [backup]({% link {{ page.version.version }}/backup.md %}) to run comparisons.
0 commit comments