Troubleshoot query performance

This guide covers the two most common performance problems in Deephaven queries: a high update-cycle ratio and insufficient memory. On ticking tables, both problems show up as results that refresh late or a server that slows down as data arrives. It pays to catch them early. The guide first shows how to tell which problem a query has. It then explains the concepts the troubleshooting steps rely on, followed by the steps that address each problem.

Identify the problem

Before you change a query, find out which problem it has. Deephaven's performance tables record what the engine does over time:

  • The Server State table samples memory use and update-cycle durations. server_state builds it from the Server State Log.
    • Its UGPTimeRatio column is the ratio for each sampling interval. If the ratio stays close to 1, see High update-cycle ratio.
    • Its UsedMemRatio column is the fraction of the server's maximum memory in use, and its GcTimeRatio column is the fraction of each sampling interval spent collecting garbage. If UsedMemRatio stays close to 1 while GcTimeRatio stays high, see Insufficient memory.
  • The Update Performance Log records how long each operation spends processing updates. When the ratio is high, the example that finds slow operations shows how to calculate each operation's share of the time and find the slowest operations.

Concepts

It's important to understand some Deephaven-specific concepts before continuing. Each subsection below describes a concept used in the troubleshooting steps that follow.

Update-cycle ratio

Deephaven performs calculations in update cycles. By default, the engine starts a new update cycle every second. When a cycle has more work than fits in that interval, it runs longer and the next cycle starts late. Update cycles do the query's work, such as adding columns and computing aggregations.

The ratio is the fraction of time the engine spends running update cycles, rather than waiting idle for the next one. For example, if the engine spends 200 ms of each second running update cycles, the ratio is 0.2 (20%).

Identify the problem shows where to find the ratio for the whole engine and how to compute each operation's share of the time.

In-memory, formula, and memoized columns

Table operations create three common column types. Each trades memory against computation:

  • In-memory: the column stores a value for every row, computed immediately, so downstream operations read stored values without recalculating. Memory use grows with the number of rows. The High update-cycle ratio section recommends them.
  • Formula: the column stores only its formula and computes values each time they are read, so it uses almost no memory but repeats the calculation. The Insufficient memory section recommends them.
  • Memoized: the column caches one result for each distinct set of input values and reuses it when those inputs repeat. With few distinct inputs it uses less memory than an in-memory column; with many, it can use more. The Insufficient memory section covers when to use them.

The Select and create columns guide covers these column types and the operations that produce them in more depth.

Row change notification

A row change notification is the update the engine sends downstream when a table's rows change. When a table changes:

  • Each operation that listens to it receives the notification and processes it.
  • The operation computes values for added rows, and recomputes modified rows only when their input columns changed. Examples include formulas in update and aggregations in agg_by.
  • If its result changed, the operation notifies its own downstream operations.

Processing a notification costs time even when nothing is recomputed.

Static tables never produce row change notifications, because they don't change. Ticking tables produce one in each update cycle in which rows are added, removed, or modified. Each update cycle in which a table changes is called a tick. Performant queries on ticking tables minimize the number of row change notifications that occur.

Tick amplification

Tick amplification occurs when the row change notification an operation sends downstream marks more cells as modified than the notification it received. Every downstream operation that reads a modified cell recomputes its result for that row, so amplification multiplies the work later in the query and raises the ratio. It has two causes:

  • Some operations, such as ungroup after group_by, can't tell which cells changed. They must mark every cell that could have changed as modified, even when only one value changed.
  • Other operations, such as joins, really do change more cells. One changed right table row changes every left table row that pairs with it.

Avoid tick amplification lists the common sources, shows an example, and describes how to address each.

Troubleshooting steps

High update-cycle ratio

When the ratio nears 1, update cycles take up most or all of the time between cycle starts, so ticking results refresh less often. The following subsections describe some steps to reduce the ratio in your queries. Filter before joins, under Insufficient memory, also lowers the ratio when a query uses keyed joins.

Create in-memory columns

When calculations are complex or expensive, use in-memory columns so the engine performs them once. Two selection operations create in-memory columns:

In-memory columns cost memory in proportion to the table's size. See Choose the right column selection method for more information.

Reduce tick frequency

You can use snapshot_when to reduce the frequency at which a table ticks. Keep in mind that snapshotting a table pulls all of its data into memory. How to dial down the update frequency of ticking tables shows examples.

Reorder join operations

In queries on ticking tables, join the tables that tick most often last.

Each tick of a joined table sends row change notifications to every operation downstream of the join, even operations whose inputs did not change.

Consider three tables:

  • static_table is a static table.
  • table_a ticks once every 5 seconds.
  • table_b ticks every update cycle (once per second by default).

You need to do two things with these three tables:

  • Create a new column, NewColumn, from the OldColumn column that table_a contributes.
  • Join all three of them together.

You could do this in two ways. Option 1 joins table_b first:

Option 2 joins table_b last:

Option 2 does less work on every table_b tick. table_b ticks five times as often as table_a, and in option 2 fewer operations process those ticks:

  • In option 1, every table_b tick passes through the second natural_join, the update operation, and everything downstream of them. Each of these operations processes the row change notification. None of them recomputes NewColumn, because its input OldColumn comes from table_a and didn't change.
  • In option 2, the first natural_join and the update operation sit upstream of the table_b join. They process row change notifications only when table_a ticks, once every 5 seconds. Each table_b tick notifies only the final join and the operations downstream of it.

Use dynamic filters

Dynamic filters reduce computational load by filtering a table based on the values in another table. where_in and where_not_in apply dynamic filters.

Define a table that holds the values of interest, such as the highest-volume stocks or all stocks in a specific sector, and filter on it to reduce the number of rows your query processes. When the values in that table change, the filtered result updates, and the changes propagate to downstream operations. Each time a value enters or leaves the values table, the filter re-checks the source rows it previously rejected or kept, so dynamic filters save the most when the set of values changes less often than the table it filters.

Avoid tick amplification

Common sources of tick amplification include:

  • Grouping and ungrouping
    • In the output of ungroup, adding or changing one row marks every existing row in its group as modified.
  • Cross joins without the on parameter
    • Even when the left table is static, a change to one right table row changes one result row for every row in the left table.
  • Keyed joins, such as join with the on parameter and natural_join
    • Only left table rows whose key matches the changed right table row are affected, so the amplification grows with the number of left table rows for each key.

For grouping and ungrouping, you can typically avoid tick amplification with partitioned tables. The following example adds a column Y to the ticking table t1 in two ways:

  • Group/ungroup: group_by groups the rows by A, update adds Y to each group, and ungroup expands the groups back into rows.
  • Partition/merge: partition_by splits t1 into a partitioned table with one constituent table for each value of A, proxy applies update to each constituent, and merge combines the constituents back into one table.

A listener on each table prints the number of rows added or modified in each update:

Initially, the output shows one change for each approach:

After the query runs for a while, the group/ungroup count keeps growing while the partition/merge count stays at one. For example, after about 20 ticks (100 seconds), the output looks like this:

The group/ungroup version amplifies ticks because ungroup marks every row of a changed group as modified. The partition/merge version produces the same rows without amplification. The engine updates each constituent independently, so adding or changing one row affects only that row in the merged result.

For joins, the extra changes are real changes to the result, so partitioning the tables cannot avoid them without changing the result. Instead, reduce how often the right table ticks and filter the left table before the join.

Insufficient memory

Every Deephaven server has a configured maximum amount of memory. When memory usage approaches that maximum, query performance can degrade significantly. If a query requires more memory than is available, it fails with a Java OutOfMemoryError, and the server can become unstable.

With ticking tables, a query's memory use usually grows over time. How fast depends on how quickly the data grows, which operations the query uses, and its table types. Large datasets often need a lot of memory, but the following steps can reduce it.

Create formula columns

Formula columns trade computation for memory. Deephaven calculates values when they are needed, such as when a downstream operation reads them. Deephaven offers two operations to create formula columns in tables:

Formula columns have trade-offs:

  • They let you create tables far larger than available memory could otherwise hold.
  • They lead to slower performance if formulas are complex or downstream operations read their values often.
  • They can lead to inconsistent results if a formula is not deterministic.

See Choose the right column selection method for more information.

Create memoized columns

Memoized columns save memory when a calculation has few distinct input values. Only lazy_update creates them. Two limits apply:

  • The cache never shrinks, so on a ticking table, memoized columns keep saving memory only while the number of distinct inputs stays small.
  • lazy_update doesn't cache vectorized calls. A vectorized query-string call passes a chunk of rows to a Python function at once instead of one row at a time. For those calls, the formula runs for every cell, as it does in update_view.

See Cache results with lazy_update.

Filter before joins

Filtering tables before joining them reduces two costs:

  • Memory: the size of a join operation's state depends on the number of rows in the left and right tables and on the number of unique keys in the key columns, so filtering first makes it smaller.
  • Update-cycle ratio: in a keyed join, filtering the left table first leaves fewer left table rows to match each changed right table row.