When an Index Reads More Than No Index
A filter such as “every document with this SKU” is cheap in a database that keeps its data close. On object storage you pay for it. The library has to read ids, then read attributes to see which rows match, and each of those reads is a request that moves bytes. One way to make a filter cheaper is to skip the attributes of rows that can’t match.
Itzma 0.4.0 adds attribute indexes to do that. On some filters they help a lot. On others they make things worse.
What gets built
You declare a field as indexed when you create the namespace. A separate
call, index_attrs, then builds one object per segment. It holds a table
for each indexed field, sorted by value, listing the rows that have that
value. A set field is indexed by element.
A filter reads the object only if its predicate touches an indexed field. The index narrows what gets fetched and never decides the answer. Every candidate still runs through the whole predicate, and a row the index rules out keeps its place in the merge, so a newer copy of an id still hides an older one that matched.
The library always fetches the object whole. That’s simple, but not free.
Where it wins and where it loses
The library ships a synthetic demo for this. It has 50,000 rows with four indexed fields: a unique one, one with 1,000 distinct values, one with 10, and a price. A filler attribute, not indexed and incompressible, makes reading a row’s attributes cost real bytes. Values are shuffled, so matching rows are spread all over. Each filter runs twice over the same rows in an in-memory store that counts reads, once without the index and once with it, and both runs have to return the same ids.
Without the index, every one of the four filters made 395 requests and read 17.2 MB. With it:
- 1 row (the unique field): 202 requests, 3.2 MB.
- 50 rows (1,000 distinct values): 246 requests, 6.8 MB.
- 5,000 rows (10 distinct values): 397 requests, 19.3 MB.
- 2,595 rows (a price range): 397 requests, 19.3 MB.
The same four filters over the same 50,000 synthetic rows. Past the dashed line, the index costs more than it saves.
Without an index the cost is flat, because the filter reads the same things whatever it’s looking for.
For one row, the index cuts the bytes to less than a fifth. At 50 rows it still saves more than half. At a tenth of the rows, and on the price range, it costs two more requests and about 2.2 MB more than doing nothing.
The index only saves something when it lets a read be skipped. With thousands of matches spread through the whole namespace, there’s nothing left to skip, so the index is pure overhead. Both broad filters land on the same figures with it, which is what you’d expect if each one read everything and then paid for the index on top.
Fetching the object whole hurts here too. It carries the tables for all four indexed fields, so a filter on one field also downloads the other three. A field you rarely filter on still adds bytes to the filters you run often.
What it costs besides bytes
- You choose the indexed fields when you create the namespace, and you can’t change them later.
- Neither
compactnormaintainbuilds the index. You have to scheduleindex_attrsyourself, and the index is only as fresh as the last call. - A namespace created with an indexed field can’t be opened by Itzma 0.3.x.
When I’d use it
I’d index a field when the usual filter on it matches a handful of rows: an order number, a user id, a SKU. I wouldn’t index a status flag or a category with ten values spread evenly over the data. In between, I’d measure on my own data first, because where the line falls depends on how big the other attributes are and how matching rows cluster.
These figures are request and byte counts from one synthetic dataset on an
in-memory store. They say nothing about latency over a network, and they only
cover filter, not filtered vector queries. The demo is in the repository if you want to run it on a different shape of data.