meta/sql: batch clone sends one INSERT per row
What would you like to be added:
Batch clone on SQL engines should write the cloned rows with multi-row INSERT statements.
dbMeta.doBatchClone collects the rows of each table (node, edge, chunk, symlink, xattr) in slices and calls mustInsert(s, beans...). With variadic beans xorm runs insertStruct once per bean, so despite the chunks of 200 in mustInsert every row is still a separate statement: a batch of N files costs about 3N round trips inside one transaction. Passing the slice itself (s.Insert(beans)) makes xorm build multi-row statements, which is what insertRows in sql_bak.go has done since #5397.
Why is this needed:
We clone read-only datasets into user workspaces, so clone time is on the path of opening a workspace. Measured on JuiceFS 1.4.1 with PostgreSQL 17 (managed service in the same availability zone, ~0.37 ms round trip), 10,000 small files:
| Layout | --threads 4 |
--threads 64 |
|---|---|---|
| one directory | 14.1 s | 14.1 s |
| 100 directories × 100 files | 8.0 s | 6.5 s |
A flat directory goes through a single BatchClone call (up to DirBatchNum["db"] = 40960 entries), so --threads does not help and the transaction is dominated by ~30,000 sequential INSERTs. The same clones on Redis take 0.4–0.8 s.
doBatchUnlink (#6537) has the same pattern for sustained, delfile and trash edge rows; I can follow up on it separately.
I have a patch with a test and will open a PR.
Source: juicedata/juicefs