optimize queries

This commit is contained in:
Andre Medeiros 2022-05-21 17:15:24 -04:00
parent 65f8402d88
commit fc94bef379

View file

@ -230,16 +230,16 @@ func enqueueUsers(ctx context.Context, logger *logrus.Logger, statsd *statsd.Cli
err := pool.BeginFunc(ctx, func(tx pgx.Tx) error { err := pool.BeginFunc(ctx, func(tx pgx.Tx) error {
stmt := ` stmt := `
WITH batch AS ( UPDATE users
SELECT id SET next_check_at = $2
WHERE id IN (
SELECT id
FROM users FROM users
WHERE next_check_at < $1 WHERE next_check_at < $1
ORDER BY next_check_at ORDER BY next_check_at
FOR UPDATE SKIP LOCKED
LIMIT 100 LIMIT 100
) )
UPDATE users
SET next_check_at = $2
WHERE users.id IN(SELECT id FROM batch)
RETURNING users.id` RETURNING users.id`
rows, err := tx.Query(ctx, stmt, now, next) rows, err := tx.Query(ctx, stmt, now, next)
if err != nil { 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 { err := pool.BeginFunc(ctx, func(tx pgx.Tx) error {
stmt := ` stmt := `
WITH batch AS ( UPDATE subreddits
SELECT id SET next_check_at = $2
WHERE subreddits.id IN(
SELECT id
FROM subreddits FROM subreddits
WHERE next_check_at < $1 WHERE next_check_at < $1
ORDER BY next_check_at ORDER BY next_check_at
FOR UPDATE SKIP LOCKED
LIMIT 100 LIMIT 100
) )
UPDATE subreddits
SET next_check_at = $2
WHERE subreddits.id IN(SELECT id FROM batch)
RETURNING subreddits.id` RETURNING subreddits.id`
rows, err := tx.Query(ctx, stmt, now, next) rows, err := tx.Query(ctx, stmt, now, next)
if err != nil { 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 { err := pool.BeginFunc(ctx, func(tx pgx.Tx) error {
stmt := ` 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 UPDATE accounts
SET next_stuck_notification_check_at = $2 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` RETURNING accounts.id`
rows, err := tx.Query(ctx, stmt, now, next) rows, err := tx.Query(ctx, stmt, now, next)
if err != nil { 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 { err := pool.BeginFunc(ctx, func(tx pgx.Tx) error {
stmt := ` stmt := `
WITH account AS (
SELECT id
FROM accounts
WHERE
next_notification_check_at < $1
ORDER BY next_notification_check_at
LIMIT 2500
)
UPDATE accounts UPDATE accounts
SET next_notification_check_at = $2 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` RETURNING accounts.id`
rows, err := tx.Query(ctx, stmt, now, next) rows, err := tx.Query(ctx, stmt, now, next)
if err != nil { if err != nil {