mirror of
https://github.com/coder/coder.git
synced 2025-07-21 01:28:49 +00:00
chore(coderd/database): optimize AuditLogs queries (#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 commit is contained in:
@ -343,6 +343,7 @@ func TestSearchAudit(t *testing.T) {
|
||||
Name string
|
||||
Query string
|
||||
Expected database.GetAuditLogsOffsetParams
|
||||
ExpectedCountParams database.CountAuditLogsParams
|
||||
ExpectedErrorContains string
|
||||
}{
|
||||
{
|
||||
@ -372,6 +373,9 @@ func TestSearchAudit(t *testing.T) {
|
||||
Expected: database.GetAuditLogsOffsetParams{
|
||||
ResourceTarget: "foo",
|
||||
},
|
||||
ExpectedCountParams: database.CountAuditLogsParams{
|
||||
ResourceTarget: "foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "RequestID",
|
||||
@ -386,7 +390,7 @@ func TestSearchAudit(t *testing.T) {
|
||||
// Do not use a real database, this is only used for an
|
||||
// organization lookup.
|
||||
db := dbmem.New()
|
||||
values, errs := searchquery.AuditLogs(context.Background(), db, c.Query)
|
||||
values, countValues, errs := searchquery.AuditLogs(context.Background(), db, c.Query)
|
||||
if c.ExpectedErrorContains != "" {
|
||||
require.True(t, len(errs) > 0, "expect some errors")
|
||||
var s strings.Builder
|
||||
@ -397,6 +401,7 @@ func TestSearchAudit(t *testing.T) {
|
||||
} else {
|
||||
require.Len(t, errs, 0, "expected no error")
|
||||
require.Equal(t, c.Expected, values, "expected values")
|
||||
require.Equal(t, c.ExpectedCountParams, countValues, "expected count values")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user