chore: remove excess join in GetQuotaConsumedForUser query (#15338)

Filter is applied in original workspace query. We do not need to join
`workspaces` twice. Use build_number instead of `created_at`
for determining the last build.
This commit is contained in:
Steven Masley
2024-11-05 08:12:56 -06:00
committed by GitHub
parent 886dcbec84
commit 2d00b50eb6
4 changed files with 115 additions and 24 deletions

View File

@ -6746,25 +6746,19 @@ FROM
INNER JOIN
workspaces on wb.workspace_id = workspaces.id
WHERE
-- Only return workspaces that match the user + organization.
-- Quotas are calculated per user per organization.
NOT workspaces.deleted AND
workspaces.owner_id = $1 AND
workspaces.organization_id = $2
ORDER BY
wb.workspace_id,
wb.created_at DESC
wb.build_number DESC
)
SELECT
coalesce(SUM(daily_cost), 0)::BIGINT
FROM
workspaces
INNER JOIN latest_builds ON
latest_builds.workspace_id = workspaces.id
WHERE
NOT deleted AND
-- We can likely remove these conditions since we check above.
-- But it does not hurt to be defensive and make sure future query changes
-- do not break anything.
workspaces.owner_id = $1 AND
workspaces.organization_id = $2
latest_builds
`
type GetQuotaConsumedForUserParams struct {