Skip to main content
Version: Python

How to reduce the update frequency of ticking tables

This guide will show you how to reduce the update frequency of ticking tables.

When a table updates, all of the children of the table, which depend upon the table as a data source, must be updated. For fast-changing data, this can mean a lot of computing to keep child tables up to date. Table snapshots allow the update frequency of a table to be reduced, which results in fewer updates of child tables. This can be useful when processing fast-changing data on limited hardware.

Syntax

The snapshot operation produces an in-memory copy of a table (source), which refreshes every time another table (trigger) ticks.

result = trigger.snapshot(source)
result = trigger.snapshot(source, do_init)
note

The trigger table is often a time table, a special type of table that adds new rows at a regular, user-defined interval. The sole column of a time table is Timestamp.

caution

Columns from the trigger table appear in the result table. If the trigger and source tables have columns with the same name, an error will be raised. To avoid this problem, rename conflicting columns.

Sample at a regular interval

In this example, the source table updates every 0.5 seconds with new data. The trigger table updates every 5 seconds, triggering a new snapshot of the source table (result). This design pattern is useful for reducing the amount of data that must be processed.

from deephaven import time_table
import random

source = time_table("00:00:0.5").update(formulas=["X = (int) random.randint(0, 100)", "Y = sqrt(X)"])

trigger = time_table("00:00:05").rename_columns(cols=["TriggerTimestamp = Timestamp"])

result = trigger.snapshot(source_table=source)

Create a static snapshot

This example creates a static snapshot of the data table at the instant snapshot is called. Here, empty_table creates the trigger table. Since empty_table creates a table (staticData) that never changes, the snapshot does not update. The do_init=True argument causes snapshot to create a snapshot without requiring the trigger table to first tick. The trigger table never changes so the snapshot will not change.

The second argument to snapshot, do_init, can be either True or False:

  • True means the snapshot will be created both when the trigger table ticks and when initially called.
  • False means the snapshot will be created only when the trigger table ticks. The default value is false.

First, create the dynamic source table. Then when desired, freeze the dynamic table and take a snapshot, creating a static table.

caution

empty_table(0).snapshot(source_table=source, do_init=True) will return a static snapshot when it is called. If there is no data in source, the snapshot will be empty. When running this example, wait until the source table populates before running the snapshot. In the image below, 10 seconds elapsed before executing the snapshot.

from deephaven import time_table, empty_table
import random

source = time_table("00:00:01").update(formulas=["X = (int) random.randint(0, 100)", "Y = sqrt(X)"])

Wait the desired seconds for the source table to populate values, then execute the snapshot method.

static_data = empty_table(0).snapshot(source_table=source, do_init=True)