Count Vs Exists (ARCH5063)
Esta página aún no está disponible en tu idioma.
Key go.orm.count-vs-exists · Severity info · Category orm
Using count > 0 to test for row presence makes the DB count every matching row. Use an exists query that can stop at the first row instead.
Incorrect
Section titled “Incorrect”var count int64db.Model(&User{}).Where("email = ?", email).Count(&count)if count > 0 { // present}Correct
Section titled “Correct”var exists booldb.Model(&User{}). Select("1"). Where("email = ?", email). Limit(1). Find(&exists)if exists { // present}For presence checks use a Limit(1) short-circuit query instead of Count, which scans all matching rows.