From fc94bef379e232764ec9e2ca543467ec33bb75ab Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 21 May 2022 17:15:24 -0400 Subject: [PATCH] optimize queries --- internal/cmd/scheduler.go | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/internal/cmd/scheduler.go b/internal/cmd/scheduler.go index 9cc16aa..99d3beb 100644 --- a/internal/cmd/scheduler.go +++ b/internal/cmd/scheduler.go @@ -230,16 +230,16 @@ func enqueueUsers(ctx context.Context, logger *logrus.Logger, statsd *statsd.Cli err := pool.BeginFunc(ctx, func(tx pgx.Tx) error { stmt := ` - WITH batch AS ( - SELECT id + UPDATE users + SET next_check_at = $2 + WHERE id IN ( + SELECT id FROM users WHERE next_check_at < $1 ORDER BY next_check_at + FOR UPDATE SKIP LOCKED LIMIT 100 ) - UPDATE users - SET next_check_at = $2 - WHERE users.id IN(SELECT id FROM batch) RETURNING users.id` rows, err := tx.Query(ctx, stmt, now, next) if err != nil { @@ -296,16 +296,16 @@ func enqueueSubreddits(ctx context.Context, logger *logrus.Logger, statsd *stats err := pool.BeginFunc(ctx, func(tx pgx.Tx) error { stmt := ` - WITH batch AS ( - SELECT id + UPDATE subreddits + SET next_check_at = $2 + WHERE subreddits.id IN( + SELECT id FROM subreddits WHERE next_check_at < $1 ORDER BY next_check_at + FOR UPDATE SKIP LOCKED LIMIT 100 ) - UPDATE subreddits - SET next_check_at = $2 - WHERE subreddits.id IN(SELECT id FROM batch) RETURNING subreddits.id` rows, err := tx.Query(ctx, stmt, now, next) if err != nil { @@ -366,17 +366,16 @@ func enqueueStuckAccounts(ctx context.Context, logger *logrus.Logger, statsd *st err := pool.BeginFunc(ctx, func(tx pgx.Tx) error { stmt := ` - WITH batch AS ( - SELECT id - FROM accounts - WHERE - next_stuck_notification_check_at < $1 - ORDER BY next_stuck_notification_check_at - LIMIT 500 - ) UPDATE accounts SET next_stuck_notification_check_at = $2 - WHERE accounts.id IN(SELECT id FROM batch) + WHERE accounts.id IN( + SELECT id + FROM accounts + WHERE next_stuck_notification_check_at < $1 + ORDER BY next_stuck_notification_check_at + FOR UPDATE SKIP LOCKED + LIMIT 500 + ) RETURNING accounts.id` rows, err := tx.Query(ctx, stmt, now, next) if err != nil { @@ -437,17 +436,16 @@ func enqueueAccounts(ctx context.Context, logger *logrus.Logger, statsd *statsd. err := pool.BeginFunc(ctx, func(tx pgx.Tx) error { stmt := ` - WITH account AS ( - SELECT id - FROM accounts - WHERE - next_notification_check_at < $1 - ORDER BY next_notification_check_at - LIMIT 2500 - ) UPDATE accounts SET next_notification_check_at = $2 - WHERE accounts.id IN(SELECT id FROM account) + WHERE accounts.id IN( + SELECT id + FROM accounts + WHERE next_notification_check_at < $1 + ORDER BY next_notification_check_at + FOR UPDATE SKIP LOCKED + LIMIT 2500 + ) RETURNING accounts.id` rows, err := tx.Query(ctx, stmt, now, next) if err != nil {