Query a 4 GB CSV
in 1.7 ms

A secondary index for huge CSV files. The CSV stays exactly as it is — a flat, hash-ordered index sits beside it. No import, no duplicate, no database.

pip install csvidx
cargo install csvidx

Rust · Python bindings · MIT · 100M-row build: 12.3 s

One user’s orders, out of 100 million rows

The index reads only the 8 rows that match. Everything else on this chart reads the whole file.

100M rows, 4.18 GiB, warm cache, key matching 8 rows. Same file, same query. From Python it is idx.get(user_id) → a pandas DataFrame; numbers verified against grep row counts. Honest edge: a key with 2% of the file loses to DuckDB on a cold cache — the index pays off while the query takes a small share of rows.

Build once, query forever

Appends are cheap too: new rows go to a small tail file, the main index is never rewritten.

# build: 100M rows in ~12 s, or in 256 MiB of RAM with --mem-limit-mib csvidx build orders.csv --key user_id csvidx get orders.csv 8841207 8841207,user_08841207,2025-03-14T09:12:04,128.50,shipped,"gift wrap, ribbon" 8841207,user_08841207,2025-04-02T18:41:33,42.00,delivered,"" # after appending to the CSV: new rows land in orders.csv.idx.tail (0.54 s for 86 MiB) csvidx update orders.csv

A flat array, sorted by hash

No tree, no page structure to maintain. Record i lives at byte 64 + 16·i, so binary search over the mmap is the whole story.

orders.csv.idxlittle-endian
0x000064Bheader · CRC32 · indexed_up_to
0x004016Bkey_hash (xxh3) · row_offset
0x005016Bkey_hash · row_offset
0x006016B… sorted by (hash, offset)
…n·16Bequal keys are adjacent, in file order
  • Binary search over mmap

    Nothing is loaded on open — the index opens in ~0.1 ms and pages come in as the search touches them.

  • Appends go to a tail

    .idx.tail — new rows land in a small side file; compact merges it in 3 s when it reaches 10%.

  • A fence for cold caches

    .idx.fence — first hash of every 4 KiB page, one page read per query instead of ~20.

  • Fixed-memory builds

    External sort: the same index byte-for-byte from 64 MiB of RAM to all-in-memory.

The rows you asked for, as a DataFrame

import csvidx idx = csvidx.Index("orders.csv") # opens in ~0.1 ms: mmap, nothing loaded df = idx.get(8841207) # → pandas.DataFrame, rows in file order tbl = idx.get_arrow([1, 2, 3]) # → pyarrow.Table n = idx.count([1, 2, 3]) # per-key counts, without reading rows

Column types come from the found rows via pd.read_csv, so results match read_csv + a filter. Lookups release the GIL and run in parallel.

What it does not do

Append-only files

Edits in the middle of the file are not detected. Suspect edits — run csvidx verify.

Exact match, one column

Byte-for-byte: "007" ≠ 7. Ranges are impossible — the index is ordered by hash.

Small share of rows

A query returning a few percent of the file is random reads; a scan wins past that point.