Skip to content

bug(python): No Batch Size for optimize #2325

Closed

Description

@Pedrexus

LanceDB version

0.21.2 (python)

What happened?

Hello, all. Thanks for the nice library.

I am trying to run .optimize with a dataset that contains 180M rows (~2Tb), but I keep running into memory overflows (I have 128Gb RAM). I tried using the .compact_files, which works with a batch_size=1024, but it warns me saying it's deprecated and the docs says there is no cleanup in .compact_files.

How to proceed?

Activity

  1. added
    bug
    typescript
    on Apr 9, 2025
  2. changed the title [-]bug(node): No Batch Size for optimize[/-] [+]bug(python): No Batch Size for optimize[/+] on Apr 10, 2025
  3. Pedrexus commented on Apr 10, 2025

    @Pedrexus
    Author

    On a related note, I executed .compact_files on the whole db, but now the size has doubled (from 2Tb to 4Tb). I also see that inside the data/ folder there are ".lance#1" files.

  4. QianZhu commented on Apr 11, 2025

    @QianZhu
    Contributor

    We are working on a feature at the moment (a few months out) to use batch sizes in bytes instead of number of rows. At that point it shouldn't be necessary to specify the batch size for these automatic operations. Once that merges we will deprecate things like compact_files but not before then.

    On a related note, I executed .compact_files on the whole db, but now the size has doubled (from 2Tb to 4Tb). I also see that inside the data/ folder there are ".lance#1" files.

    compact_files will not trigger cleanup (which is part of optimize). You will need to run optimize after you do the compaction to trigger the cleanup.

    Also, .lance#1 indicates that some kind of write failed (we write to temporary files and then rename them when the write completes). These kinds of garbage files will eventually be removed by cleanup but by default only if they are older than 7 days (it can be hard to distinguish between a failed temporary file and a temporary file currently in use somewhere).

  5. ggozad commented on Jul 30, 2026

    @ggozad

    Hey, I am wondering if we can revive this issue, we are being affected in the context of using it with haiku.rag:

    Still reproduces on 0.36.0, with a trigger not described here yet: it is not compaction in general, it is every pass after the first.

    A table of large binary values never reaches target_rows_per_fragment (~1M rows), so the fragment produced by the first compaction stays a candidate forever and every later optimize() re-merges all of it. Peak tracks table size, not the delta. Output of the
    attached repro, 6000 rows x 1.5 MB large_binary, macOS arm64:

    built 6000 rows x 1.5MB = 8.8GB
    optimize #1 (many small fragments): 11.2s peak rss 9.5GB
    optimize #2 (after append):         10.8s peak rss 48.7GB
    

    Same magnitude on 0.34.0. 19 GB of payload peaks at 67 GB on pass #2. Row size is not the driver: 20 rows x 445 MB behaves like 6000 x 1.5 MB at equal total bytes. The scan path already adapts by bytes, down to one row per batch on the 445 MB rows.

    compact_files(target_rows_per_fragment=500) bounds it to 5.4 GB at num_threads=1, and the post-append pass then reports fragments_removed=2, fragments_added=1.

    Ideally, the byte-based compaction per the original reply to the issue would do the job.
    In the meantime, would it be possible to expose CompactionOptions on AsyncTable.optimize, which currently accepts only cleanup_older_than, delete_unverified and retrain? The compact_files mitigation is sync-only and needs pylance, so on the async API there is no way to bound a compaction at all. The Rust side already takes the options.

    lance_repro.py

  6. reopened this on Aug 6, 2026
  7. ggozad commented on Aug 6, 2026

    @ggozad

    @Xuanwo thanks for re-opening.
    Note also that #3870 was opened against this issue by @lancedb-gatefixer with a note that it fixes this issue. However, it does bound the peak, but it leaves the caller having to derive a row target from bytes.
    The byte-based sizing @QianZhu described in their first reply would actually close it.

  8. Xuanwo commented on Aug 6, 2026

    @Xuanwo
    Member

    Yep, "the byte-based sizing" is still a problem that need more work at lance side.

  9. ggozad commented on Aug 20, 2026

    @ggozad

    Measured the knobs #3870 exposes against the reproducer from this issue, using
    lance 10.0.0 through pylance so the binding is out of the picture. 40 rows per
    round of 34.5 MB large_binary, peak RSS growth during compaction:

    max_bytes_per_file does not bound compaction memory. At 2 GB it pinned
    the largest data file at 2.70 GB, but peak still tracked the table: 3.44 GB at
    round 2 rising to 13.88 GB at round 7 against a 9.43 GB table. The option
    reaches WriteParams, so it caps what compaction writes, while the planner
    still selects source fragments by target_rows_per_fragment -- a row count
    that 280 fat rows never approach. Since that is the knob the PR leads with, it
    is worth saying plainly that it does not solve the case this issue is about.

    target_rows_per_fragment is what bounds it, and num_threads is not
    involved. With churn, over 8 rounds to a 10.78 GB table:

    arm peak over rounds 4-8
    default 5.80 -> 24.98 GB, climbing
    target 40, num_threads=1 1.58, 1.39, 1.06, 1.36, 1.37 GB
    target 40, num_threads default (12) 1.68, 1.40, 0.71, 1.34, 1.10 GB

    Thread count makes no difference; the row target alone flattens it, and disk
    lands exactly on the live data size in every bounded arm. The ceiling is
    predictable as target_rows x bytes_per_row, so a caller can size it -- but
    only by deriving a row count from bytes, which is the original ask here.

    One documentation point: the target bounds steady state, not the first pass
    over existing data -- a fragment already larger than it is rewritten whole --
    so it is not a memory cap.

    Separately, max_source_bytes returns an empty plan and only logs a warning
    when the first task alone exceeds the budget, so on an already-degenerate
    fragment a byte budget means compaction silently never runs again. A budget
    that splits the task, or a hard error, would be safer than a warning that
    leaves compaction permanently stalled.

    On byte-based sizing generally: we also measured lance blob columns as an
    alternative, since moving the payload out of line should make compaction
    cheap. With default options it is still linear (peak ~0.93x table); it only
    flattens with an explicit target_rows_per_fragment, same as inline. So the
    row target is the single mechanism that bounds compaction today regardless of
    layout, which is why exposing it matters.

  10. added a commit that references this issue on Oct 26, 2026
    3352bf4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtypescript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions