Skip to content

Commit 27c90dd

Browse files
committed
refactor: optimize DeleteExpiredAPIKeys to use :execrows
Use :execrows instead of :one to simplify the query by removing the extra CTE wrapper. This lets PostgreSQL return the row count directly via RowsAffected() instead of requiring an explicit COUNT(*) scan.
1 parent cc61ea6 commit 27c90dd

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

coderd/database/queries.sql.go

Lines changed: 13 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/apikeys.sql

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,20 @@ DELETE FROM
8585
WHERE
8686
user_id = $1;
8787

88-
-- name: DeleteExpiredAPIKeys :one
88+
-- name: DeleteExpiredAPIKeys :execrows
8989
WITH expired_keys AS (
9090
SELECT id
9191
FROM api_keys
9292
-- expired keys only
9393
WHERE expires_at < @before::timestamptz
9494
LIMIT @limit_count
95-
),
96-
deleted_rows AS (
97-
DELETE FROM
98-
api_keys
99-
USING
100-
expired_keys
101-
WHERE
102-
api_keys.id = expired_keys.id
103-
RETURNING api_keys.id
104-
)
105-
SELECT COUNT(deleted_rows.id) AS deleted_count FROM deleted_rows;
106-
;
95+
)
96+
DELETE FROM
97+
api_keys
98+
USING
99+
expired_keys
100+
WHERE
101+
api_keys.id = expired_keys.id;
107102

108103
-- name: ExpirePrebuildsAPIKeys :exec
109104
-- Firstly, collect api_keys owned by the prebuilds user that correlate

0 commit comments

Comments
 (0)