USE 58 - Lazy load current records#174
Merged
Merged
Conversation
Why these changes are being introduced: Current TIMDEX records has been a consistent source of complexity and performance concerns. And, one of the defining features of TDA, so it's worth getting right. The previous approach was to materialize lightweight metadata records about all current versions of each record in memory as a DuckDB temp table. This made repeated read queries pulling only current records more efficient, but was unnecessarily loading that data into memory for operations like writing data or reading a specific run (not current record associated). It turns out that reading current records is somewhat rare, and when it does happen, it's usually a one-off request as part of a larger operation like re-indexing a source in TIM. How this addresses that need: The new approach is a hybrid between a view only (lazy evaluation) and a temporary table in memory (eager evaluation). By default, a view is created, which either a) does not get used or b) is used but only 1-2 times per session and the lazy evaluation of a view is okay. Alternatively, TIMDEXDataset can be initialized with 'preload_current_records=True' if it's known that a multiple requests for current records will be needed in the session and it's worth the time and memory hit upfront. Side effects of this change: * For most operations in the TIMDEX ETL pipeline, which don't use current records, the load time and memory usage is fairly dramatically decreased. Relevant ticket(s): - https://mitlibraries.atlassian.net/browse/USE-58 - prep work for new methods
ghukill
commented
Oct 17, 2025
| """ | ||
| ) | ||
|
|
||
| def _create_current_records_view(self, conn: DuckDBPyConnection) -> None: |
Contributor
Author
There was a problem hiding this comment.
The changes to this method are the key here.
We breakout the SQL query that identifies current records into a string. Then depending on the preload_current_records flag, we either:
- create a view directly from that
- or, create a temporary table and a view based on the temporary table
ghukill
commented
Oct 17, 2025
| from information_schema.tables | ||
| where table_catalog = 'temp' | ||
| and table_name = 'current_records' | ||
| and table_type = 'LOCAL TEMPORARY' |
Contributor
Author
There was a problem hiding this comment.
A table type of LOCAL TEMPORARY is indication of a DuckDB temporary table.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose and background context
Why these changes are being introduced:
Current TIMDEX records has been a consistent source of complexity and performance concerns. And, one of the defining features of TDA, so it's worth getting right.
The previous approach was to materialize lightweight metadata records about all current versions of each record in memory as a DuckDB temp table. This made repeated read queries pulling only current records more efficient, but was unnecessarily loading that data into memory for operations like writing data or reading a specific run (not current record associated).
It turns out that reading current records is somewhat rare, and when it does happen, it's usually a one-off request as part of a larger operation like re-indexing a source in TIM.
How this addresses that need:
The new approach is a hybrid between a view only (lazy evaluation) and a temporary table in memory (eager evaluation).
By default, a view is created, which either a) does not get used or b) is used but only 1-2 times per session and the lazy evaluation of a view is okay.
Alternatively,
TIMDEXDatasetcan now be initialized withpreload_current_records=Trueif it's known that a multiple requests for current records will be needed in the session and it's worth the time and memory hit upfront. In this scenario, a temporary, in-memory table is created as before.Effect:
TIMDEXDatasetis much quicker, benefitting operations like writing to the dataset that has nothing to do with current recordsHow can a reviewer manually see the effects of these changes?
1- Set AWS credentials for production (more data)
2- Set env vars:
3- Start Ipython shell
4- Preliminary setup:
For both of the next steps, we'll be finding a single the current version of a single
almarecord which is know to have 30+ versions; a "needle in a haystack" query.5- Init a
TIMDEXDatasetwithout pre-loading current records and perform read:6- Init a
TIMDEXDatasetwith pre-loading current records and perform read:Includes new or updated dependencies?
NO
Changes expectations for external applications?
YES: For most operations in the TIMDEX ETL pipeline, which don't use current records, the load time and memory usage is fairly dramatically decreased.
What are the relevant tickets?