Add secondary indexes to accelerate cache lookups

This commit is contained in:
Jan Doubravský
2026-06-05 18:17:55 +02:00
parent 286a5f207d
commit 757a8f4eba
8 changed files with 213 additions and 4 deletions
+20
View File
@@ -218,6 +218,26 @@ engine = CachingEngine(
engine.refresh() # also reloads any expired TTL tables on demand
```
## Secondary indexes
To accelerate lookups, you can declare **secondary indexes** per table — they are created on the in-memory SQLite copy so `WHERE`/`JOIN` filters on those columns run as indexed searches instead of full scans:
```python
engine = CachingEngine(
base_engine,
indexes={
"VW_P_PRATVALUES": ["PRODUCT_PRODUCTNR"], # single-column index
"VW_ELEMENTS": [["ELEMENT_ID", "ELEMENTVARIANT_ID"], "ELEMENTVARIANT_NAME"],
},
)
```
Each value is a list of index definitions: a string is a single-column index, a nested list is a composite index.
- **Index columns are pulled into the cache automatically** (like delta key columns), so the index exists from the first load even if your queries don't select those columns.
- Indexes are **recreated after every (re)load** — full loads, TTL reloads, and `invalidate()` + re-fetch all rebuild them — so they're always present, and they persist in `cache.db` across restarts.
- Delta-tracked tables already get a unique index on their key columns; secondary indexes are independent and can be combined with `delta` or `ttl`.
## Persistence
The in-memory cache is persisted to `cache.db` on disk: