Skip to content

[Solution Provided] File iteration can take too long for too many files #10902

Open

Description

@MahdiBM

What happened?

Let's preface this with: This is not a typical "AI slop" issue. Claude did most of the job though.

Furthermore, this issue is solved for me personally locally. The goal of this issue is to ensure another user doesn't bump into this. The solution is just a simple DB index.


I noticed syncthing is taking too long, I tasked Claude with finding what is going on and all.

Claude mentioned it's an issue with the DB usage, at which point I asked it to go ahead and see if some indexes and all can solve my specific issue.

The solution came out to be this index:
https://github.com/syncthing/syncthing/compare/main...MahdiBM:mmbm-faster-file-iter?expand=1

  Before After
Mini → MacBook median 40.5 s, max 65.6 s median 1.3 s, max 2.7 s
MacBook → Mini median 9.6 s, max 55.1 s median 1.3 s, max 5.5 s
sync-preparing (MacBook / Mini) 25–35 s / 8–24 s median 9 ms / 3 ms

I'm guessing it happens because the synced folders are too big in terms of file count specifically.

Below is Opus 5.5 Max effort's explanations.
I've read it and it sounds about correct.
I have not investigated the whole issue myself as it'd be out of my time budget.
I believe this is not a waste of your time.

## What happened?

- Folder: ~1.06M items (915k files, 143k dirs, 828 symlinks, ~42 GB), shared between two Macs, both send-receive.
- A change on one Mac took 40–65 s to be applied on the other (median 41 s over 33 files in a 7-minute window), and more when pulls failed and were retried.
- The receiving folder sat in "Preparing to Sync" for 25–35 s on every puller iteration, even with only 1–2 files needed; a pull usually ran 2–3 iterations.
- A goroutine profile (`/rest/debug/support`) taken during "Preparing to Sync" had the puller in `processNeeded` → `AllNeededGlobalFiles` → `sqlite3_step`.
- The query plan of the local need query (`neededGlobalFilesLocal`) on that database:
  `SCAN n` / `SEARCH g USING INDEX files_name_idx_only (name_idx=?)`: it walks all ~1.06M names because nothing indexes `local_flags`.
- Same query on a copy of the database, with the same SQLite (3.53.4 via mattn/go-sqlite3 v1.14.50): 0.75 s at normal priority, 5.6–6.9 s under `taskpolicy -b`. Syncthing runs as a launchd daemon with `ProcessType Background` here.
- After adding `CREATE INDEX files_needed ON files (local_flags) WHERE local_flags & 32 != 0` to both folder databases: plan `SCAN g USING INDEX files_needed`, query 0–6 ms, index 4 KiB, "Preparing to Sync" median 9 ms / 3 ms, changes applied a median of 1.3 s after the sender's index update.
- Expected: finding the needed files should not require walking the whole folder. `20-files.sql` says the Need bit "allows for very efficient lookup of files needing handling on this device", but no index backs it.

## Relevant log output

Query plans instead of logs:

before: SCAN n
        SEARCH g USING INDEX files_name_idx_only (name_idx=?)
        SEARCH fi USING INTEGER PRIMARY KEY (rowid=?)
        SEARCH bl USING PRIMARY KEY (blocklist_hash=?) LEFT-JOIN
        USE TEMP B-TREE FOR ORDER BY
after:  SCAN g USING INDEX files_needed
        SEARCH fi USING INTEGER PRIMARY KEY (rowid=?)
        SEARCH bl USING PRIMARY KEY (blocklist_hash=?) LEFT-JOIN
        SEARCH n USING INTEGER PRIMARY KEY (rowid=?)
        USE TEMP B-TREE FOR ORDER BY

Syncthing version

syncthing v2.1.6-rc.3 "Hafnium Hornet" (go1.27.1 darwin-arm64) [email protected] 2026-09-22 12:50:39 UTC

Platform & operating system

macOS 27.0.1

Browser version

Relevant log output

Activity

  1. added
    bug
    on Sep 30, 2026
  2. added theissue type on Sep 30, 2026
  3. added
    needs-triage
    on Sep 30, 2026
  4. added 20 commits that reference this issue on Oct 15, 2026
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

    bugneeds-triage

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions