Skip to content

Commit 0a893bd

Browse files
authored
Use cursor pagination for backfill query to improve performance. (#700)
1 parent 23c69d6 commit 0a893bd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pkg/dotc1z/grants.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,21 @@ func backfillGrantExpansionColumn(ctx context.Context, db *goqu.Database, tableN
366366
// GrantExpandable annotation (if present), and populate the expansion column.
367367
// Non-expandable grants get an empty-blob sentinel to avoid re-processing,
368368
// which is cleaned up to NULL at the end.
369+
//
370+
// Uses cursor-based pagination (g.id > ?) so each query jumps to unprocessed
371+
// rows via the primary key index instead of rescanning from the start.
372+
var lastID int64
369373
for {
370374
rows, err := db.QueryContext(ctx, fmt.Sprintf(
371375
`SELECT g.id, g.data FROM %s g
372376
JOIN %s sr ON g.sync_id = sr.sync_id
373-
WHERE g.expansion IS NULL
377+
WHERE g.id > ?
378+
AND g.expansion IS NULL
374379
AND sr.supports_diff = 0
380+
ORDER BY g.id
375381
LIMIT 1000`,
376382
tableName, syncRuns.Name(),
377-
))
383+
), lastID)
378384
if err != nil {
379385
return err
380386
}
@@ -402,6 +408,8 @@ func backfillGrantExpansionColumn(ctx context.Context, db *goqu.Database, tableN
402408
break
403409
}
404410

411+
lastID = batch[len(batch)-1].id
412+
405413
tx, err := db.BeginTx(ctx, nil)
406414
if err != nil {
407415
return err

0 commit comments

Comments
 (0)