Skip to content

Commit 3e9e553

Browse files
committed
db: add composite indexes for patch list and ci_check queries
The default patch list query filters on (project_id, archived, state_id) and sorts by date DESC. The existing index only covers (project_id, archived, date DESC) which forces the database to scan through all patches for a project and filter state_id row by row. On large projects this results in query times exceeding 18 seconds. Replace the index with (project_id, archived, state_id, date DESC) so the planner can seek directly to the relevant states. Also add a (patch_id, state) covering index on ci_check for the check counts GROUP BY query, avoiding table lookups for every matching row. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 35f3af7 commit 3e9e553

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Patchwork - automated patch tracking system
2+
// Copyright (C) The Patchwork Contributors (see CONTRIBUTORS)
3+
//
4+
// SPDX-License-Identifier: GPL-2.0-or-later
5+
6+
package migrations
7+
8+
import (
9+
"context"
10+
11+
"github.com/uptrace/bun"
12+
)
13+
14+
func init() {
15+
Register(up0003, down0003)
16+
}
17+
18+
func up0003(ctx context.Context, tx bun.Tx) error {
19+
_, err := tx.NewDropIndex().
20+
Index("idx_patch_project_id_archived_date_desc").
21+
IfExists().
22+
Exec(ctx)
23+
if err != nil {
24+
return err
25+
}
26+
27+
_, err = tx.NewCreateIndex().
28+
Table("patch").
29+
Index("idx_patch_project_id_archived_state_id_date_desc").
30+
ColumnExpr("project_id, archived, state_id, date DESC").
31+
IfNotExists().
32+
Exec(ctx)
33+
if err != nil {
34+
return err
35+
}
36+
37+
_, err = tx.NewCreateIndex().
38+
Table("ci_check").
39+
Index("idx_ci_check_patch_id_state").
40+
ColumnExpr("patch_id, state").
41+
IfNotExists().
42+
Exec(ctx)
43+
return err
44+
}
45+
46+
func down0003(ctx context.Context, tx bun.Tx) error {
47+
_, err := tx.NewDropIndex().
48+
Index("idx_ci_check_patch_id_state").
49+
IfExists().
50+
Exec(ctx)
51+
if err != nil {
52+
return err
53+
}
54+
55+
_, err = tx.NewDropIndex().
56+
Index("idx_patch_project_id_archived_state_id_date_desc").
57+
IfExists().
58+
Exec(ctx)
59+
if err != nil {
60+
return err
61+
}
62+
63+
_, err = tx.NewCreateIndex().
64+
Table("patch").
65+
Index("idx_patch_project_id_archived_date_desc").
66+
ColumnExpr("project_id, archived, date DESC").
67+
IfNotExists().
68+
Exec(ctx)
69+
return err
70+
}

‎pkg/db/models.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ type SeriesRef struct {
207207
}
208208

209209
type Patch struct {
210-
bun.BaseModel `bun:"table:patch" unique:"msgid,project_id;series_id,number" index:"archived,state_id,delegate_id,date,project_id,submitter_id,name;project_id,archived,date DESC" json:"-"`
210+
bun.BaseModel `bun:"table:patch" unique:"msgid,project_id;series_id,number" index:"archived,state_id,delegate_id,date,project_id,submitter_id,name;project_id,archived,state_id,date DESC" json:"-"`
211211

212212
ID int `bun:"id,pk,autoincrement" json:"id"`
213213
Msgid string `bun:"msgid,notnull" json:"msgid"`
@@ -348,7 +348,7 @@ type BundlePatch struct {
348348
}
349349

350350
type Check struct {
351-
bun.BaseModel `bun:"table:ci_check,alias:ci_check" unique:"patch_id,context,user_id" json:"-"`
351+
bun.BaseModel `bun:"table:ci_check,alias:ci_check" unique:"patch_id,context,user_id" index:"patch_id,state" json:"-"`
352352

353353
ID int `bun:"id,pk,autoincrement" json:"id"`
354354
PatchID int `bun:"patch_id,notnull" json:"-" fk:"patch.id,cascade"`

0 commit comments

Comments
 (0)