In Temporal, for a single workflow ID, there can be multiple executions over time.
Right now, when we query workflows, we get all executions for each workflow ID.
However, the requirement is to get only the latest execution for each workflow ID (based on the most recent start time), whether it is running or completed.
The issue is that Temporal does not provide a direct way to filter and return only the latest execution per workflow ID.
| Workflow ID | Run ID | Start Time | Status |
|---|---|---|---|
| A | r1 | 10:00 AM | Completed |
| A | r2 | 10:05 AM | Failed |
| A | r3 | 10:10 AM | Running |
| B | r4 | 10:02 AM | Completed |
| B | r5 | 10:08 AM | Running |
Current Output (what we get)
All executions:
-
A → r1, r2, r3
-
B → r4, r5
Expected Output (what we want)
Only latest per workflow:
-
A → r3 (latest)
-
B → r5 (latest)
Why this is a problem
-
Since each workflow ID can have multiple executions, the total number of records becomes very large.
-
When we query this data, it takes more time because we are fetching unnecessary older executions as well.
-
After fetching, we still need to manually filter (deduplicate) the data on the client side to keep only the latest execution per workflow ID.
-
This makes the whole process:
-
inefficient
-
slower
-
and extra work on the client side
-