Skip to content

chore(coderd/database): optimize AuditLogs queries #18600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 1, 2025

Conversation

kacpersaw
Copy link
Contributor

Closes #17689

This PR optimizes the audit logs query performance by extracting the count operation into a separate query and replacing the OR-based workspace_builds with conditional joins.

Query changes

  • Extracted count query to separate one
  • Replaced single workspace_builds join with OR conditions with separate conditional joins
  • Added conditional joins
    • wb_build for workspace_build audit logs (which is a direct lookup)
    • wb_workspace for workspace create audit logs (via workspace)

Optimized AuditLogsOffset query:
https://explain.dalibo.com/plan/4g1hbedg4a564bg8

New CountAuditLogs query:
https://explain.dalibo.com/plan/ga2fbcecb9efbce3

workspace_builds.build_number = 1
)
LEFT JOIN organizations ON audit_logs.organization_id = organizations.id
SELECT sqlc.embed(audit_logs),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd a comment that CountAuditLogs also needs to be extended whenever we extend the filtering on GetAuditLogsOffset

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe better - add a test that compares the filter object for this method and count method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a new test which compares WHERE clause for both queries :)

Copy link
Member

@ethanndickson ethanndickson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I've based my implementation of the connection log queries off this PR without any trouble: #18629

coderd/audit.go Outdated
@@ -64,7 +64,21 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
filter.Username = ""
}

dblogs, err := api.Database.GetAuditLogsOffset(ctx, filter)
// Use the same filters to count the number of audit logs
count, err := api.Database.CountAuditLogs(ctx, database.CountAuditLogsParams{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's going to be very easy for someone to forget to update this in both places. Maybe you should make the searchquery.AuditLogs package return both of these params types so they're constructed in the same place

Or add a comment in both places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to searchquery

Comment on lines 23 to 30
LEFT JOIN users ON audit_logs.user_id = users.id
LEFT JOIN organizations ON audit_logs.organization_id = organizations.id
-- First join on workspaces to get the initial workspace create
-- to workspace build 1 id. This is because the first create is
-- is a different audit log than subsequent starts.
LEFT JOIN workspaces ON
audit_logs.resource_type = 'workspace' AND
audit_logs.resource_id = workspaces.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent formatting in this whole file seems messed up now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@kacpersaw kacpersaw merged commit 695de6e into main Jul 1, 2025
34 checks passed
@kacpersaw kacpersaw deleted the kacpersaw/slow-audit-logs branch July 1, 2025 05:31
@github-actions github-actions bot locked and limited conversation to collaborators Jul 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Audit log slows to a crawl when nearing 1 million entries
4 participants