perf, add index and transaction around create campaign

Signed-off-by: RonniSkansing <rskansing@gmail.com>
This commit is contained in:
RonniSkansing
2026-09-05 16:32:28 +02:00
parent 2670c40132
commit 09fc4bdfba
2 changed files with 45 additions and 6 deletions
@@ -2,6 +2,7 @@ package database
import (
"github.com/google/uuid"
"gorm.io/gorm"
)
const (
@@ -20,3 +21,11 @@ type RecipientGroupRecipient struct {
func (RecipientGroupRecipient) TableName() string {
return RECIPIENT_GROUP_RECIPIENT_TABLE
}
// Migrate adds an index leading with recipient_group_id. The existing unique index
// leads with recipient_id, so counting or listing a group's members
// (WHERE recipient_group_id = ?) could not use it and scanned the recipients per
// group, making the group list quadratic in recipients times groups.
func (RecipientGroupRecipient) Migrate(db *gorm.DB) error {
return db.Exec(`CREATE INDEX IF NOT EXISTS idx_rgr_group ON recipient_group_recipients(recipient_group_id, recipient_id)`).Error
}