Skip to content

Query Inside Loop (ARCH5064)

Key go.orm.query-inside-loop · Severity warning · Category orm

Issuing an ad-hoc find, aggregate, or repository call inside a loop means one round-trip per element. Hoist the query out of the loop.

for _, id := range ids {
var u User
db.First(&u, id)
results = append(results, u)
}
var users []User
db.Where("id IN ?", ids).Find(&users)

Turn per-iteration queries into a single batched query with an IN clause.