Skip to content

chore(coderd/database): optimize AuditLogs queries (cherry-pick #18600) #19193

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

Conversation

ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Aug 6, 2025

A customer who recently upgraded their deployment to 2.24 is seeing their audit log queries take longer than a minute to load (resulting in a gateway timeout). As such, Support's requested we backport this fix to 2.24 (in the next patch), as it does not require a database migration.

Original PR Description (#18600):

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

This comment was marked as outdated.

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
@ethanndickson ethanndickson force-pushed the ethan/2.24-audit-log-optimization-backport branch from 97a3fc2 to a788e2f Compare August 6, 2025 03:12
@ethanndickson ethanndickson changed the title chore: backport audit log query optimization to 2.24 chore(coderd/database): optimize AuditLogs queries (cherry-pick #18600) Aug 7, 2025
@ethanndickson ethanndickson merged commit 5ff7496 into release/2.24 Aug 7, 2025
41 checks passed
@ethanndickson ethanndickson deleted the ethan/2.24-audit-log-optimization-backport branch August 7, 2025 12:21
@github-actions github-actions bot locked and limited conversation to collaborators Aug 7, 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.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy