Skip to content

Commit 24a6d95

Browse files
committed
refactor: optimize DeleteOldConnectionLogs 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 5a442c3 commit 24a6d95

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

coderd/database/queries.sql.go

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

coderd/database/queries/connectionlogs.sql

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,17 @@ WHERE
239239
-- @authorize_filter
240240
;
241241

242-
-- name: DeleteOldConnectionLogs :one
242+
-- name: DeleteOldConnectionLogs :execrows
243243
WITH old_logs AS (
244244
SELECT id
245245
FROM connection_logs
246246
WHERE connect_time < @before_time::timestamp with time zone
247247
ORDER BY connect_time ASC
248248
LIMIT @limit_count
249-
),
250-
deleted_rows AS (
251-
DELETE FROM connection_logs
252-
USING old_logs
253-
WHERE connection_logs.id = old_logs.id
254-
RETURNING connection_logs.id
255249
)
256-
SELECT COUNT(deleted_rows.id) AS deleted_count FROM deleted_rows;
250+
DELETE FROM connection_logs
251+
USING old_logs
252+
WHERE connection_logs.id = old_logs.id;
257253

258254
-- name: UpsertConnectionLog :one
259255
INSERT INTO connection_logs (

0 commit comments

Comments
 (0)