N Plus One Query (ARCH5050)
هذا المحتوى غير متوفر بلغتك بعد.
Key go.orm.n-plus-one-query · Severity warning · Category orm
Running a separate query per element inside a loop (N+1) is the classic ORM performance killer. Fetch all related data with a single JOIN or preload.
Incorrect
Section titled “Incorrect”var users []Userdb.Find(&users)for i := range users { db.Where("user_id = ?", users[i].ID).Find(&users[i].Orders)}Correct
Section titled “Correct”var users []Userdb.Preload("Orders").Find(&users)Fetch related data in a single query with Preload or a JOIN instead of querying per element in a loop.