Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kacpersaw merged 8 commits intomainfromkacpersaw/slow-audit-logs
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 singleworkspace_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 organizationsONaudit_logs.organization_id=organizations.id
SELECTsqlc.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 thatCountAuditLogs also needs to be extended whenever we extend the filtering onGetAuditLogsOffset

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
ContributorAuthor

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

@ethanndicksonethanndickson 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


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 thesearchquery.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
ContributorAuthor

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 usersONaudit_logs.user_id=users.id
LEFT JOIN organizationsONaudit_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 workspacesON
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
ContributorAuthor

Choose a reason for hiding this comment

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

Fixed

@kacpersawkacpersaw merged commit695de6e intomainJul 1, 2025
29 checks passed
@kacpersawkacpersaw deleted the kacpersaw/slow-audit-logs branchJuly 1, 2025 05:31
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsJul 1, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@ethanndicksonethanndicksonethanndickson approved these changes

@ibetitsmikeibetitsmikeibetitsmike left review comments

@deansheatherdeansheatherdeansheather approved these changes

Assignees

@kacpersawkacpersaw

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Audit log slows to a crawl when nearing 1 million entries
4 participants
@kacpersaw@deansheather@ethanndickson@ibetitsmike

[8]ページ先頭

©2009-2025 Movatter.jp