Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/dotc1z/c1file.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ type C1File struct {
readOnly bool
encoderConcurrency int

// Cached sync run for listConnectorObjects (avoids N+1 queries)
cachedViewSyncRun *syncRun
cachedViewSyncOnce sync.Once
cachedViewSyncErr error

// Slow query tracking
slowQueryLogTimes map[string]time.Time
slowQueryLogTimesMu sync.Mutex
Expand Down
12 changes: 2 additions & 10 deletions pkg/dotc1z/sql_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,12 @@ func listConnectorObjects[T proto.Message](ctx context.Context, c *C1File, table
case reqSyncID != "":
q = q.Where(goqu.C("sync_id").Eq(reqSyncID))
default:
var latestSyncRun *syncRun
var err error
latestSyncRun, err = c.getFinishedSync(ctx, 0, connectorstore.SyncTypeFull)
// Use cached sync run to avoid N+1 queries during pagination
latestSyncRun, err := c.getCachedViewSyncRun(ctx)
if err != nil {
return nil, "", err
}

if latestSyncRun == nil {
latestSyncRun, err = c.getLatestUnfinishedSync(ctx, connectorstore.SyncTypeAny)
if err != nil {
return nil, "", err
}
}

if latestSyncRun != nil {
q = q.Where(goqu.C("sync_id").Eq(latestSyncRun.ID))
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/dotc1z/sync_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ type syncRun struct {
ParentSyncID string
}

// getCachedViewSyncRun returns the cached sync run for read operations.
// This avoids N+1 queries when paginating through listConnectorObjects.
// The result is computed once and cached for the lifetime of the C1File.
func (c *C1File) getCachedViewSyncRun(ctx context.Context) (*syncRun, error) {
ctx, span := tracer.Start(ctx, "C1File.getCachedViewSyncRun")
defer span.End()

c.cachedViewSyncOnce.Do(func() {
Copy link
Contributor

@ennyjfrick ennyjfrick Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use sync.OnceValue here instead maybe

// First try to get a finished full sync
c.cachedViewSyncRun, c.cachedViewSyncErr = c.getFinishedSync(ctx, 0, connectorstore.SyncTypeFull)
if c.cachedViewSyncErr != nil {
return
}

// If no finished sync, try to get an unfinished one
if c.cachedViewSyncRun == nil {
c.cachedViewSyncRun, c.cachedViewSyncErr = c.getLatestUnfinishedSync(ctx, connectorstore.SyncTypeAny)
}
})

return c.cachedViewSyncRun, c.cachedViewSyncErr
}

func (c *C1File) getLatestUnfinishedSync(ctx context.Context, syncType connectorstore.SyncType) (*syncRun, error) {
ctx, span := tracer.Start(ctx, "C1File.getLatestUnfinishedSync")
defer span.End()
Expand Down
Loading