From 546da4ee7d542ae3be60bd23a21b9713a3a550db Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Thu, 16 Jan 2020 20:41:13 -0600 Subject: [PATCH] Removing unneeded query to improve group performance. As mentioned in #1702, the query in `insertTargetIntoGroup` isn't needed, since both instances where it's used we already know that the target isn't in the group. This means it's safe to remove that query, improving performance dramatically. Before: ``` BenchmarkPostGroup100-4 81 12629754 ns/op 2989993 B/op 52918 allocs/op BenchmarkPostGroup1000-4 6 189527792 ns/op 29891818 B/op 528082 allocs/op BenchmarkPostGroup10000-4 1 6203645806 ns/op 299253648 B/op 5282859 allocs/op BenchmarkPutGroup100-4 100 10221833 ns/op 2589165 B/op 46078 allocs/op BenchmarkPutGroup1000-4 7 162692432 ns/op 25812440 B/op 458425 allocs/op BenchmarkPutGroup10000-4 1 7596445084 ns/op 260385808 B/op 4581569 allocs/op ``` After: ``` BenchmarkPostGroup100-4 133 8848973 ns/op 2354605 B/op 42322 allocs/op BenchmarkPostGroup1000-4 9 112557410 ns/op 23549206 B/op 422184 allocs/op BenchmarkPostGroup10000-4 1 3414209403 ns/op 235635952 B/op 4222090 allocs/op BenchmarkPutGroup100-4 147 8094333 ns/op 2271297 B/op 40777 allocs/op BenchmarkPutGroup1000-4 9 125092124 ns/op 22635067 B/op 405421 allocs/op BenchmarkPutGroup10000-4 1 5712591900 ns/op 228592920 B/op 4051316 allocs/op ``` --- models/group.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/models/group.go b/models/group.go index 27965632..9a5ef39e 100644 --- a/models/group.go +++ b/models/group.go @@ -327,13 +327,10 @@ func insertTargetIntoGroup(tx *gorm.DB, t Target, gid int64) error { }).Error(err) return err } - err = tx.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error - if err == gorm.ErrRecordNotFound { - err = tx.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error - if err != nil { - log.Error(err) - return err - } + err = tx.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error + if err != nil { + log.Error(err) + return err } if err != nil { log.WithFields(logrus.Fields{